Merge remote-tracking branch 'upstream/edge' into upstream-master
[clinton/Smoothieware.git] / src / testframework / easyunit / simplestring.cpp
1 /*
2 EasyUnit : Simple C++ Unit testing framework
3 Copyright (C) 2004 Barthelemy Dagenais
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Barthelemy Dagenais
20 barthelemy@prologique.com
21 */
22
23 #include "simplestring.h"
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28
29 static const int DEFAULT_SIZE = 20;
30
31 SimpleString::SimpleString ()
32 : buffer(new char [1])
33 {
34 buffer [0] = '\0';
35 }
36
37
38 SimpleString::SimpleString (const char *otherBuffer)
39 : buffer (new char [strlen (otherBuffer) + 1])
40 {
41 strcpy (buffer, otherBuffer);
42 }
43
44 SimpleString::SimpleString (const SimpleString& other)
45 {
46 buffer = new char [other.size() + 1];
47 strcpy(buffer, other.buffer);
48 }
49
50
51 SimpleString SimpleString::operator= (const SimpleString& other)
52 {
53 delete buffer;
54 buffer = new char [other.size() + 1];
55 strcpy(buffer, other.buffer);
56 return *this;
57 }
58
59 SimpleString SimpleString::operator+ (const SimpleString& other)
60 {
61 SimpleString newS;
62 delete [] newS.buffer;
63 newS.buffer = new char[this->size()+other.size()+1];
64 strcpy(newS.buffer,this->asCharString());
65 newS.buffer= strcat(newS.buffer,other.asCharString());
66 return newS;
67 }
68
69 char *SimpleString::asCharString () const
70 {
71 return buffer;
72 }
73
74 int SimpleString::size() const
75 {
76 return strlen (buffer);
77 }
78
79 SimpleString::~SimpleString ()
80 {
81 delete [] buffer;
82 }
83
84 bool operator== (const SimpleString& left, const SimpleString& right)
85 {
86 return !strcmp (left.asCharString (), right.asCharString ());
87 }
88
89 bool operator!= (const SimpleString& left, const SimpleString& right)
90 {
91 return !(left == right);
92 }
93
94 SimpleString StringFrom (bool value)
95 {
96 char buffer [sizeof ("false") + 1];
97 sprintf (buffer, "%s", value ? "true" : "false");
98 return SimpleString(buffer);
99 }
100
101 SimpleString StringFrom (const char *value)
102 {
103 return SimpleString(value);
104 }
105
106 SimpleString StringFrom (long value)
107 {
108 char buffer [DEFAULT_SIZE];
109 sprintf (buffer, "%ld", value);
110
111 return SimpleString(buffer);
112 }
113
114 SimpleString StringFrom (int value)
115 {
116 char buffer [DEFAULT_SIZE];
117 sprintf (buffer, "%d", value);
118
119 return SimpleString(buffer);
120 }
121
122 SimpleString StringFrom (unsigned int value)
123 {
124 char buffer [DEFAULT_SIZE];
125 sprintf (buffer, "%u", value);
126
127 return SimpleString(buffer);
128 }
129
130 SimpleString StringFrom (double value)
131 {
132 char buffer [DEFAULT_SIZE];
133 sprintf (buffer, "%lf", value);
134
135 return SimpleString(buffer);
136 }
137
138 SimpleString StringFrom (float value)
139 {
140 char buffer [DEFAULT_SIZE];
141 sprintf (buffer, "%f", value);
142
143 return SimpleString(buffer);
144 }
145
146 SimpleString StringFrom (const SimpleString& value)
147 {
148 return SimpleString(value);
149 }
150
151
152
153