~ubuntu-branches/ubuntu/oneiric/blobandconquer/oneiric

« back to all changes in this revision

Viewing changes to src/common/CString.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Guus Sliepen
  • Date: 2008-06-15 12:04:29 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080615120429-5ss7cbb4z9mpywj5
Tags: 0.95-1
New upstream release. Closes: #486310

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (C) 2006 Parallel Realities
3
 
 
4
 
This program is free software; you can redistribute it and/or
5
 
modify it under the terms of the GNU General Public License
6
 
as published by the Free Software Foundation; either version 2
7
 
of the License, or (at your option) any later version.
8
 
 
9
 
This program is distributed in the hope that it will be useful,
10
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 
 
13
 
See the GNU General Public License for more details.
14
 
 
15
 
You should have received a copy of the GNU General Public License
16
 
along with this program; if not, write to the Free Software
17
 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
 
 
19
 
*/
20
 
 
21
 
#include "../headers.h"
22
 
 
23
 
String::String()
24
 
{
25
 
        text = NULL;
26
 
        
27
 
        setText("");
28
 
}
29
 
 
30
 
String::String(const char *text)
31
 
{
32
 
        int size = strlen(text);
33
 
 
34
 
        this->text = new char[size + 1];
35
 
 
36
 
        strncpy(this->text, text, size);
37
 
        this->text[size] = '\0';
38
 
 
39
 
        length = strlen(this->text);
40
 
}
41
 
 
42
 
String::~String()
43
 
{
44
 
        if (this->text != NULL)
45
 
        {
46
 
                delete[] this->text;
47
 
                this->text = NULL;
48
 
        }
49
 
}
50
 
 
51
 
/*
52
 
Got this off Google... not sure I entirely trust it but it seems
53
 
to be okay.
54
 
*/
55
 
void String::trim()
56
 
{
57
 
        if (text == NULL)
58
 
        {
59
 
                return;
60
 
        }
61
 
        
62
 
        if ((strcmp(text, "") == 0) || (strlen(text) == 0))
63
 
        {
64
 
                return;
65
 
        }
66
 
        
67
 
        char *tempText = new char[length + 1];
68
 
 
69
 
        char *c = text;
70
 
 
71
 
        while ((*c <= 32) || (*c >= 127))
72
 
        {
73
 
                c++; // ROFL!!! :)
74
 
                
75
 
                if (strlen(c) == 0)
76
 
                {
77
 
                        setText("");
78
 
                        return;
79
 
                }
80
 
        }
81
 
        
82
 
        strcpy(tempText, c);
83
 
 
84
 
        int len = strlen(tempText);
85
 
        
86
 
        while ((len > 0) && ((tempText[len - 1] <= 32) || (tempText[len - 1] >= 127)))
87
 
        {
88
 
                tempText[--len] = 0;
89
 
        }
90
 
        
91
 
        delete[] this->text;
92
 
 
93
 
        this->text = tempText;
94
 
        
95
 
        length = strlen(this->text);
96
 
}
97
 
 
98
 
void String::toLowerCase()
99
 
{
100
 
        for (unsigned int i = 0 ; i < strlen(text) ; i++)
101
 
        {
102
 
                if ((text[i] >= SDLK_a - 32) && (text[i] <= SDLK_z - 32))
103
 
                {
104
 
                        text[i] += 32;
105
 
                }
106
 
        }
107
 
}
108
 
 
109
 
void String::toUpperCase()
110
 
{
111
 
        for (unsigned int i = 0 ; i < strlen(text) ; i++)
112
 
        {
113
 
                if ((text[i] >= SDLK_a) && (text[i] <= SDLK_z))
114
 
                {
115
 
                        text[i] -= 32;
116
 
                }
117
 
        }
118
 
}
119
 
 
120
 
void String::setText(const char *text, ...)
121
 
{
122
 
        if (this->text != NULL)
123
 
        {
124
 
                delete[] this->text;
125
 
                this->text = NULL;
126
 
        }
127
 
        
128
 
        strcpy(tmpString, "");
129
 
        
130
 
        va_list argp;
131
 
        va_start(argp, text);
132
 
        vsprintf(tmpString, text, argp);
133
 
        va_end(argp);
134
 
        
135
 
        int size = strlen(tmpString);
136
 
 
137
 
        this->text = new char[size + 1];
138
 
 
139
 
        strncpy(this->text,  tmpString, size);
140
 
        this->text[size] = '\0';
141
 
        
142
 
        length = strlen(this->text);
143
 
}
144
 
 
145
 
void String::captialise()
146
 
{
147
 
        bool uppercase = true;
148
 
        char *c = text;
149
 
        
150
 
        while (*c != '\0')
151
 
        {
152
 
                if ((*c >= SDLK_a) && (*c <= SDLK_z))
153
 
                {
154
 
                        if (uppercase)
155
 
                        {
156
 
                                *c -= 32;
157
 
                                uppercase = false;
158
 
                        }
159
 
                }
160
 
                else if (*c == SDLK_SPACE)
161
 
                {
162
 
                        uppercase = true;
163
 
                }
164
 
                
165
 
                c++;
166
 
        }
167
 
}
168
 
 
169
 
void String::operator= (const char *text)
170
 
{
171
 
        if (text == this->text)
172
 
        {
173
 
                return;
174
 
        }
175
 
        
176
 
        if (this->text != NULL)
177
 
        {
178
 
                delete[] this->text;
179
 
                this->text = NULL;
180
 
        }
181
 
 
182
 
        if (text == NULL)
183
 
        {
184
 
                printf("WARNING: String - Can't set NULL!\n");
185
 
                exit(1);
186
 
                return;
187
 
        }
188
 
 
189
 
        int size = strlen(text);
190
 
 
191
 
        this->text = new char[size + 1];
192
 
 
193
 
        strncpy(this->text, text, size);
194
 
        this->text[size] = '\0';
195
 
 
196
 
        length = strlen(this->text);
197
 
}
198
 
 
199
 
void String::operator= (String string)
200
 
{
201
 
        if (this->text != NULL)
202
 
        {
203
 
                delete[] this->text;
204
 
                this->text = NULL;
205
 
        }
206
 
        *this = string.text;
207
 
}
208
 
 
209
 
bool String::operator== (const char *text)
210
 
{
211
 
        if (strcmp(this->text, text) == 0)
212
 
        {
213
 
                return true;
214
 
        }
215
 
 
216
 
        return false;
217
 
}
218
 
 
219
 
bool String::operator== (String string)
220
 
{
221
 
        if (strcmp(this->text, string.getText()) == 0)
222
 
        {
223
 
                return true;
224
 
        }
225
 
 
226
 
        return false;
227
 
}
228
 
 
229
 
bool String::operator!= (const char *text)
230
 
{
231
 
        if (strcmp(this->text, text) != 0)
232
 
        {
233
 
                return true;
234
 
        }
235
 
 
236
 
        return false;
237
 
}
238
 
 
239
 
bool String::operator!= (String string)
240
 
{
241
 
        if (strcmp(this->text, string.getText()) != 0)
242
 
        {
243
 
                return true;
244
 
        }
245
 
 
246
 
        return false;
247
 
}
248
 
 
249
 
char *String::getText()
250
 
{
251
 
        if (text == NULL)
252
 
        {
253
 
                printf("WARNING: String::getText() - text is NULL!\n");
254
 
                return NULL;
255
 
        }
256
 
 
257
 
        return text;
258
 
}
259
 
 
260
 
int String::getLength()
261
 
{
262
 
        return length;
263
 
}
264
 
 
265
 
char String::tmpString[1024];