| author | Scott Ehlert <ds@alliedmods.net> |
| Fri Aug 10 23:43:16 2007 +0000 (2007-08-10) | |
| changeset 346 | d0868271663a |
| parent 322 | 351e5c71c58a |
| child 523 | 6f1630519b81 |
| permissions | -rwxr-xr-x |
1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2007 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 int find_last_of(const char c, int index = npos) const
157 {
158 int len = static_cast<int>(size());
159 if (len < 1)
160 return npos;
161 if (index >= len || index < npos)
162 return npos;
163 int i;
164 if (index == npos)
165 i = len - 1;
166 else
167 i = index;
169 for (; i>=0; i--)
170 {
171 if (v[i] == c)
172 {
173 return i;
174 }
175 }
177 return npos;
178 }
180 bool is_space(int c) const
181 {
182 if (c == '\f' || c == '\n' ||
183 c == '\t' || c == '\r' ||
184 c == '\v' || c == ' ')
185 {
186 return true;
187 }
189 return false;
190 }
192 void trim()
193 {
194 if (!v)
195 return;
197 unsigned int i = 0;
198 unsigned int j = 0;
199 size_t len = strlen(v);
201 if (len == 1)
202 {
203 if (is_space(v[i]))
204 {
205 clear();
206 return;
207 }
208 }
210 unsigned char c0 = v[0];
212 if (is_space(c0))
213 {
214 for (i=0; i<len; i++)
215 {
216 if (!is_space(v[i]) || (is_space(v[i]) && ((unsigned char)i==len-1)))
217 {
218 erase(0, i);
219 break;
220 }
221 }
222 }
224 len = strlen(v);
226 if (len < 1)
227 {
228 return;
229 }
231 if (is_space(v[len-1]))
232 {
233 for (i=len-1; i>=0; i--)
234 {
235 if (!is_space(v[i])
236 || (is_space(v[i]) && i==0))
237 {
238 erase(i+1, j);
239 break;
240 }
241 j++;
242 }
243 }
245 if (len == 1)
246 {
247 if (is_space(v[0]))
248 {
249 clear();
250 return;
251 }
252 }
253 }
255 void erase(unsigned int start, int num = npos)
256 {
257 if (!v)
258 return;
259 unsigned int i = 0;
260 size_t len = size();
261 //check for bounds
262 if (num == npos || start+num > len-start)
263 num = len - start;
264 //do the erasing
265 bool copyflag = false;
266 for (i=0; i<len; i++)
267 {
268 if (i>=start && i<start+num)
269 {
270 if (i+num < len)
271 {
272 v[i] = v[i+num];
273 } else {
274 v[i] = 0;
275 }
276 copyflag = true;
277 } else if (copyflag) {
278 if (i+num < len)
279 {
280 v[i] = v[i+num];
281 } else {
282 v[i] = 0;
283 }
284 }
285 }
286 len -= num;
287 v[len] = 0;
288 }
290 String substr(unsigned int index, int num = npos) const
291 {
292 if (!v)
293 {
294 String b("");
295 return b;
296 }
298 String ns;
300 size_t len = size();
302 if (index >= len || !v)
303 return ns;
305 if (num == npos)
306 {
307 num = len - index;
308 } else if (index+num >= len) {
309 num = len - index;
310 }
312 unsigned int i = 0;
313 unsigned int nslen = num + 2;
315 ns.Grow(nslen);
317 for (i=index; i<index+num; i++)
318 ns.append(v[i]);
320 return ns;
321 }
323 void toLower()
324 {
325 if (!v)
326 return;
327 unsigned int i = 0;
328 size_t len = strlen(v);
329 for (i=0; i<len; i++)
330 {
331 if (v[i] >= 65 && v[i] <= 90)
332 v[i] &= ~(1<<5);
333 }
334 }
336 String & operator = (const String &src)
337 {
338 assign(src);
339 return *this;
340 }
342 String & operator = (const char *src)
343 {
344 assign(src);
345 return *this;
347 }
349 char operator [] (unsigned int index) const
350 {
351 if (index > size() || !v)
352 {
353 return -1;
354 } else {
355 return v[index];
356 }
357 }
359 int at(int a) const
360 {
361 if (a < 0 || a >= (int)size() || !v)
362 return -1;
364 return v[a];
365 }
367 bool at(int at, char c)
368 {
369 if (at < 0 || at >= (int)size() || !v)
370 return false;
372 v[at] = c;
374 return true;
375 }
377 private:
378 void Grow(unsigned int d, bool copy=true)
379 {
380 if (d <= a_size)
381 return;
382 char *n = new char[d + 1];
383 if (copy && v)
384 strcpy(n, v);
385 if (v)
386 delete [] v;
387 else
388 strcpy(n, "");
389 v = n;
390 a_size = d + 1;
391 }
393 char *v;
394 unsigned int a_size;
395 public:
396 static const int npos = -1;
397 };
399 }; //NAMESPACE
401 #endif //_INCLUDE_CSTRING_H