]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CString.cpp
Allow the game to run without sounds.
[quix0rs-blobwars.git] / src / CString.cpp
1 /*
2 Copyright (C) 2005 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         setText("");
26 }
27
28 String::String(char *text)
29 {
30         int size = strlen(text);
31
32         this->text = new char[size + 1];
33
34         strcpy(this->text, text);
35
36         this->length = size;
37 }
38
39 String::~String()
40 {
41         if (this->text != NULL)
42         {
43                 delete[] this->text;
44                 this->text = NULL;
45         }
46 }
47
48 /*
49 Got this off Google... not sure I entirely trust it but it seems
50 to be okay.
51 */
52 void String::trim()
53 {
54         if (text == NULL)
55         {
56                 return;
57         }
58
59         char *tempText = new char[length + 1];
60
61         char *c = text;
62
63         while (*c == ' ')
64         {
65                 c++; // LOL!!! :)
66         }
67
68         strcpy(tempText, c);
69
70         int len = strlen(tempText);
71         
72         while ((len > 0) && ((tempText[len - 1] == ' ') || (tempText[len - 1] == '\0')))
73         {
74                 tempText[--len] = 0;
75         }
76
77         delete[] this->text;
78
79         this->text = tempText;
80 }
81
82 void String::operator= (char *text)
83 {
84         if (this->text != NULL)
85         {
86                 delete[] this->text;
87                 this->text = NULL;
88         }
89
90         if (text == NULL)
91         {
92                 printf("WARNING: String - Can't set NULL!\n");
93                 return;
94         }
95
96         int size = strlen(text);
97
98         this->text = new char[size + 1];
99
100         strcpy(this->text, text);
101
102         this->length = size;
103 }
104
105 bool String::operator== (char *text)
106 {
107         if (strcmp(this->text, text) == 0)
108         {
109                 return true;
110         }
111
112         return false;
113 }
114
115 bool String::operator== (String string)
116 {
117         if (strcmp(this->text, string.getText()) == 0)
118         {
119                 return true;
120         }
121
122         return false;
123 }
124
125 bool String::operator!= (char *text)
126 {
127         if (strcmp(this->text, text) != 0)
128         {
129                 return true;
130         }
131
132         return false;
133 }
134
135 bool String::operator!= (String string)
136 {
137         if (strcmp(this->text, string.getText()) != 0)
138         {
139                 return true;
140         }
141
142         return false;
143 }
144
145 void String::setText(char *text, ...)
146 {
147         strcpy(tmpString, "");
148         
149         va_list argp;
150         va_start(argp, text);
151         vsprintf(tmpString, text, argp);
152         va_end(argp);
153         
154         int size = strlen(tmpString);
155
156         this->text = new char[size + 1];
157
158         strcpy(this->text,  tmpString);
159 }
160
161 char *String::getText()
162 {
163         if (text == NULL)
164         {
165                 printf("WARNING: String::getText() - text is NULL!\n");
166                 return NULL;
167         }
168
169         return text;
170 }
171
172 int String::getLength()
173 {
174         return length;
175 }
176
177 char String::tmpString[1024];