| author | David Anderson <dvander@alliedmods.net> |
| Thu Oct 26 06:19:47 2006 +0000 (2006-10-26) | |
| changeset 264 | 188e8220fdc3 |
| parent 262 | 5c2892f5b935 |
| child 284 | 33f790f43a1a |
| permissions | -rwxr-xr-x |
1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2005 Metamod:Source Development Team
3 * No warranties of any kind
4 *
5 * License: zlib/libpng
6 *
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
9 */
11 /* AMX Mod X
12 *
13 * by the AMX Mod X Development Team
14 */
16 #ifndef _INCLUDE_CSTRING_H
17 #define _INCLUDE_CSTRING_H
19 #include <string.h>
20 #include <stdio.h>
22 namespace SourceHook
23 {
24 class String
25 {
26 public:
27 String()
28 {
29 v = NULL;
30 a_size = 0;
31 //assign("");
32 }
34 ~String()
35 {
36 if (v)
37 delete [] v;
38 }
40 String(const char *src)
41 {
42 v = NULL;
43 a_size = 0;
44 assign(src);
45 }
47 String(const String &src)
48 {
49 v = NULL;
50 a_size = 0;
51 assign(src.c_str());
52 }
54 bool operator ==(const String &other)
55 {
56 return (compare(other.c_str()) == 0);
57 }
59 bool operator ==(const char *other)
60 {
61 return (compare(other) == 0);
62 }
64 const char *c_str() { return v?v:""; }
66 const char *c_str() const { return v?v:""; }
68 void append(const char *t)
69 {
70 Grow(size() + strlen(t) + 1);
71 strcat(v, t);
72 }
74 void append(const char c)
75 {
76 size_t len = size();
77 Grow(len + 2);
78 v[len] = c;
79 v[len + 1] = '\0';
80 }
82 void append(String &d)
83 {
84 append(d.c_str());
85 }
87 void assign(const String &src)
88 {
89 assign(src.c_str());
90 }
92 void assign(const char *d)
93 {
94 if (!d)
95 {
96 clear();
97 } else {
98 Grow(strlen(d) + 1, false);
99 strcpy(v, d);
100 }
101 }
103 void clear()
104 {
105 if (v)
106 v[0] = '\0';
107 }
109 int compare (const char *d) const
110 {
111 if (!v)
112 return strcmp("", d);
113 else
114 return strcmp(v, d);
115 }
117 //Added this for amxx inclusion
118 bool empty() const
119 {
120 if (!v)
121 return true;
123 if (v[0] == '\0')
124 return true;
126 return false;
127 }
129 size_t size() const
130 {
131 if (v)
132 return strlen(v);
133 else
134 return 0;
135 }
137 int find(const char c, int index = 0) const
138 {
139 int len = static_cast<int>(size());
140 if (len < 1)
141 return npos;
142 if (index >= len || index < 0)
143 return npos;
144 int i = 0;
145 for (i=index; i<len; i++)
146 {
147 if (v[i] == c)
148 {
149 return i;
150 }
151 }
153 return npos;
154 }
156 bool is_space(int c) const
157 {
158 if (c == '\f' || c == '\n' ||
159 c == '\t' || c == '\r' ||
160 c == '\v' || c == ' ')
161 {
162 return true;
163 }
165 return false;
166 }
168 void trim()
169 {
170 if (!v)
171 return;
173 unsigned int i = 0;
174 unsigned int j = 0;
175 size_t len = strlen(v);
177 if (len == 1)
178 {
179 if (is_space(v[i]))
180 {
181 clear();
182 return;
183 }
184 }
186 unsigned char c0 = v[0];
188 if (is_space(c0))
189 {
190 for (i=0; i<len; i++)
191 {
192 if (!is_space(v[i]) || (is_space(v[i]) && ((unsigned char)i==len-1)))
193 {
194 erase(0, i);
195 break;
196 }
197 }
198 }
200 len = strlen(v);
202 if (len < 1)
203 {
204 return;
205 }
207 if (is_space(v[len-1]))
208 {
209 for (i=len-1; i>=0; i--)
210 {
211 if (!is_space(v[i])
212 || (is_space(v[i]) && i==0))
213 {
214 erase(i+1, j);
215 break;
216 }
217 j++;
218 }
219 }
221 if (len == 1)
222 {
223 if (is_space(v[0]))
224 {
225 clear();
226 return;
227 }
228 }
229 }
231 void erase(unsigned int start, int num = npos)
232 {
233 if (!v)
234 return;
235 unsigned int i = 0;
236 size_t len = size();
237 //check for bounds
238 if (num == npos || start+num > len-start)
239 num = len - start;
240 //do the erasing
241 bool copyflag = false;
242 for (i=0; i<len; i++)
243 {
244 if (i>=start && i<start+num)
245 {
246 if (i+num < len)
247 {
248 v[i] = v[i+num];
249 } else {
250 v[i] = 0;
251 }
252 copyflag = true;
253 } else if (copyflag) {
254 if (i+num < len)
255 {
256 v[i] = v[i+num];
257 } else {
258 v[i] = 0;
259 }
260 }
261 }
262 len -= num;
263 v[len] = 0;
264 }
266 String substr(unsigned int index, int num = npos)
267 {
268 if (!v)
269 {
270 String b("");
271 return b;
272 }
274 String ns;
276 size_t len = size();
278 if (index >= len || !v)
279 return ns;
281 if (num == npos)
282 {
283 num = len - index;
284 } else if (index+num >= len) {
285 num = len - index;
286 }
288 unsigned int i = 0;
289 unsigned int nslen = num + 2;
291 ns.Grow(nslen);
293 for (i=index; i<index+num; i++)
294 ns.append(v[i]);
296 return ns;
297 }
299 void toLower()
300 {
301 if (!v)
302 return;
303 unsigned int i = 0;
304 size_t len = strlen(v);
305 for (i=0; i<len; i++)
306 {
307 if (v[i] >= 65 && v[i] <= 90)
308 v[i] &= ~(1<<5);
309 }
310 }
312 String & operator = (const String &src)
313 {
314 assign(src);
315 return *this;
316 }
318 String & operator = (const char *src)
319 {
320 assign(src);
321 return *this;
323 }
325 char operator [] (unsigned int index)
326 {
327 if (index > size() || !v)
328 {
329 return -1;
330 } else {
331 return v[index];
332 }
333 }
335 int at(int a)
336 {
337 if (a < 0 || a >= (int)size() || !v)
338 return -1;
340 return v[a];
341 }
343 bool at(int at, char c)
344 {
345 if (at < 0 || at >= (int)size() || !v)
346 return false;
348 v[at] = c;
350 return true;
351 }
353 private:
354 void Grow(unsigned int d, bool copy=true)
355 {
356 if (d <= a_size)
357 return;
358 char *n = new char[d + 1];
359 if (copy && v)
360 strcpy(n, v);
361 if (v)
362 delete [] v;
363 else
364 strcpy(n, "");
365 v = n;
366 a_size = d + 1;
367 }
369 char *v;
370 unsigned int a_size;
371 public:
372 static const int npos = -1;
373 };
375 }; //NAMESPACE
377 #endif //_INCLUDE_CSTRING_H