summaryrefslogtreecommitdiff
path: root/cad/src/experimental/CoNTub/C++/String.h
blob: d09500b7979ec067d6db7684f8c4026ca2d72e28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details. 
#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED

#include <iostream>
#include "string.h"

class String
{
    int len;
    char *contents;
 public:
    String() {
	contents = "";
	len = 0;
    }
    int length(void) {
	return len;
    }
    String(int p) {
	char tmp[50];
	sprintf(tmp, "%d", p);
	len = strlen(tmp);
	contents = new char[len + 1];
	strcpy(contents, tmp);
    }
    String(double p) {
	char tmp[50];
	sprintf(tmp, "%lf", p);
	len = strlen(tmp);
	contents = new char[len + 1];
	strcpy(contents, tmp);
    }
    String(char *s) {
	contents = s;
	len = strlen(s);
    }
    String operator+ (int x) {
	return *this + String(x);
    }
    String operator+ (double x) {
	return *this + String(x);
    }
    String operator+ (char *x) {
	return *this + String(x);
    }
    String operator+ (String x) {
	String s = String();
	s.len = len + x.len;
	s.contents = new char[s.len + 1];
	strcpy(s.contents, contents);
	strcpy(s.contents + len, x.contents);
	return s;
    }
    void operator+= (String x) {
	int newlen = len + x.len;
	char *newcontents = new char[newlen + 1];
	strcpy(newcontents, contents);
	strcpy(newcontents + len, x.contents);
	len = newlen;
	contents = newcontents;
    }
    void operator+= (char *y) {
	String x = String(y);
	int newlen = len + x.len;
	char *newcontents = new char[newlen + 1];
	strcpy(newcontents, contents);
	strcpy(newcontents + len, x.contents);
	len = newlen;
	contents = newcontents;
    }
    /* http://gethelp.devx.com/techtips/cpp_pro/10min/10min0400.asp */
    friend std::ostream& operator<< (std::ostream& s, const String& x) {
	s << x.contents;
	return s;
    }
};

#endif