1 /* |
|
2 * botc source code |
|
3 * Copyright (C) 2012 Santeri `Dusk` Piippo |
|
4 * All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright notice, |
|
10 * this list of conditions and the following disclaimer. |
|
11 * 2. Redistributions in binary form must reproduce the above copyright notice, |
|
12 * this list of conditions and the following disclaimer in the documentation |
|
13 * and/or other materials provided with the distribution. |
|
14 * 3. Neither the name of the developer nor the names of its contributors may |
|
15 * be used to endorse or promote products derived from this software without |
|
16 * specific prior written permission. |
|
17 * 4. Redistributions in any form must be accompanied by information on how to |
|
18 * obtain complete source code for the software and any accompanying |
|
19 * software that uses the software. The source code must either be included |
|
20 * in the distribution or be available for no more than the cost of |
|
21 * distribution plus a nominal fee, and must be freely redistributable |
|
22 * under reasonable conditions. For an executable file, complete source |
|
23 * code means the source code for all modules it contains. It does not |
|
24 * include source code for modules or files that typically accompany the |
|
25 * major components of the operating system on which the executable file |
|
26 * runs. |
|
27 * |
|
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
38 * POSSIBILITY OF SUCH DAMAGE. |
|
39 */ |
|
40 |
|
41 #include <stdio.h> |
|
42 #include <stdlib.h> |
|
43 #include <string.h> |
|
44 #include <stdarg.h> |
|
45 #include "array.h" |
|
46 #include "str.h" |
|
47 #include "common.h" |
|
48 |
|
49 #define ITERATE_STRING(u) \ |
|
50 for (unsigned int u = 0; u < strlen (text); u++) |
|
51 |
|
52 // ============================================================================ |
|
53 // vdynformat: Try to write to a formatted string with size bytes first, if |
|
54 // that fails, double the size and keep recursing until it works. |
|
55 char* vdynformat (const char* c, va_list v, unsigned int size) { |
|
56 char* buffer = new char[size]; |
|
57 int r = vsnprintf (buffer, size-1, c, v); |
|
58 if (r > (int)size-1 || r < 0) { |
|
59 delete buffer; |
|
60 buffer = vdynformat (c, v, size*2); |
|
61 } |
|
62 return buffer; |
|
63 } |
|
64 |
|
65 // ============================================================================ |
|
66 str::str () { |
|
67 text = new char[1]; |
|
68 clear(); |
|
69 alloclen = strlen (text); |
|
70 } |
|
71 |
|
72 str::str (const char* c) { |
|
73 text = new char[1]; |
|
74 clear (); |
|
75 alloclen = strlen (text); |
|
76 append (c); |
|
77 } |
|
78 |
|
79 str::str (char c) { |
|
80 text = new char[1]; |
|
81 clear (); |
|
82 alloclen = strlen (text); |
|
83 append (c); |
|
84 } |
|
85 |
|
86 // ============================================================================ |
|
87 void str::clear () { |
|
88 delete text; |
|
89 text = new char[1]; |
|
90 text[0] = '\0'; |
|
91 curs = 0; |
|
92 } |
|
93 |
|
94 unsigned int str::len () {return strlen(text);} |
|
95 |
|
96 // ============================================================================ |
|
97 void str::resize (unsigned int len) { |
|
98 unsigned int oldlen = strlen (text); |
|
99 char* oldtext = new char[oldlen]; |
|
100 strncpy (oldtext, text, oldlen); |
|
101 |
|
102 delete text; |
|
103 text = new char[len+1]; |
|
104 for (unsigned int u = 0; u < len+1; u++) |
|
105 text[u] = 0; |
|
106 strncpy (text, oldtext, len); |
|
107 |
|
108 alloclen = len; |
|
109 } |
|
110 |
|
111 // ============================================================================ |
|
112 void str::dump () { |
|
113 for (unsigned int u = 0; u <= alloclen; u++) |
|
114 printf ("\t%u. %u (%c)\n", u, text[u], text[u]); |
|
115 } |
|
116 |
|
117 // ============================================================================ |
|
118 // Adds a new character at the end of the string. |
|
119 void str::append (char c) { |
|
120 // Out of space, thus resize |
|
121 if (curs == alloclen) |
|
122 resize (alloclen+1); |
|
123 text[curs] = c; |
|
124 curs++; |
|
125 } |
|
126 |
|
127 void str::append (const char* c) { |
|
128 resize (alloclen + strlen (c)); |
|
129 |
|
130 for (unsigned int u = 0; u < strlen (c); u++) { |
|
131 if (c[u] != 0) |
|
132 append (c[u]); |
|
133 } |
|
134 } |
|
135 |
|
136 void str::append (str c) { |
|
137 append (c.chars()); |
|
138 } |
|
139 |
|
140 // ============================================================================ |
|
141 void str::appendformat (const char* c, ...) { |
|
142 va_list v; |
|
143 |
|
144 va_start (v, c); |
|
145 char* buf = vdynformat (c, v, 256); |
|
146 va_end (v); |
|
147 |
|
148 append (buf); |
|
149 } |
|
150 |
|
151 void str::appendformat (str c, ...) { |
|
152 va_list v; |
|
153 |
|
154 va_start (v, c); |
|
155 char* buf = vdynformat (c.chars(), v, 256); |
|
156 va_end (v); |
|
157 |
|
158 append (buf); |
|
159 } |
|
160 |
|
161 // ============================================================================ |
|
162 char* str::chars () { |
|
163 return text; |
|
164 } |
|
165 |
|
166 // ============================================================================ |
|
167 unsigned int str::first (const char* c, unsigned int a) { |
|
168 unsigned int r = 0; |
|
169 unsigned int index = 0; |
|
170 for (; a < alloclen; a++) { |
|
171 if (text[a] == c[r]) { |
|
172 if (r == 0) |
|
173 index = a; |
|
174 |
|
175 r++; |
|
176 if (r == strlen (c)) |
|
177 return index; |
|
178 } else { |
|
179 if (r != 0) { |
|
180 // If the string sequence broke at this point, we need to |
|
181 // check this character again, for a new sequence just |
|
182 // might start right here. |
|
183 a--; |
|
184 } |
|
185 |
|
186 r = 0; |
|
187 } |
|
188 } |
|
189 |
|
190 return len (); |
|
191 } |
|
192 |
|
193 // ============================================================================ |
|
194 unsigned int str::last (const char* c, int a) { |
|
195 if (a == -1) |
|
196 a = len(); |
|
197 |
|
198 int max = strlen (c)-1; |
|
199 |
|
200 int r = max; |
|
201 for (; a >= 0; a--) { |
|
202 if (text[a] == c[r]) { |
|
203 r--; |
|
204 if (r == -1) |
|
205 return a; |
|
206 } else { |
|
207 if (r != max) |
|
208 a++; |
|
209 |
|
210 r = max; |
|
211 } |
|
212 } |
|
213 |
|
214 return len (); |
|
215 } |
|
216 |
|
217 // ============================================================================ |
|
218 str str::substr (unsigned int a, unsigned int b) { |
|
219 if (a > len()) a = len(); |
|
220 if (b > len()) b = len(); |
|
221 |
|
222 if (b == a) |
|
223 return ""; |
|
224 |
|
225 if (b < a) { |
|
226 printf ("str::substr: indices %u and %u given, should be the other way around, swapping..\n", a, b); |
|
227 |
|
228 // Swap the variables |
|
229 unsigned int c = a; |
|
230 a = b; |
|
231 b = c; |
|
232 } |
|
233 |
|
234 char* s = new char[b-a]; |
|
235 strncpy (s, text+a, b-a); |
|
236 return str(s); |
|
237 } |
|
238 |
|
239 // ============================================================================ |
|
240 void str::remove (unsigned int idx, unsigned int dellen) { |
|
241 str s1 = substr (0, idx); |
|
242 str s2 = substr (idx + dellen, len()); |
|
243 |
|
244 clear(); |
|
245 |
|
246 append (s1); |
|
247 append (s2); |
|
248 } |
|
249 |
|
250 // ============================================================================ |
|
251 void str::trim (int dellen) { |
|
252 if (!dellen) |
|
253 return; |
|
254 |
|
255 unsigned int delpos; |
|
256 if (dellen > 0) { |
|
257 delpos = len()-dellen; |
|
258 text[delpos] = 0; |
|
259 curs -= dellen; |
|
260 } else { |
|
261 str s = substr (-dellen, len()); |
|
262 clear(); |
|
263 append (s); |
|
264 } |
|
265 } |
|
266 |
|
267 // ============================================================================ |
|
268 void str::replace (const char* o, const char* n, unsigned int a) { |
|
269 unsigned int idx; |
|
270 |
|
271 while ((idx = first (o, a)) != len()) { |
|
272 str s1 = substr (0, idx); |
|
273 str s2 = substr (idx + strlen (o), len()); |
|
274 |
|
275 clear(); |
|
276 |
|
277 append (s1); |
|
278 append (n); |
|
279 append (s2); |
|
280 } |
|
281 } |
|
282 |
|
283 void str::insert (char* c, unsigned int pos) { |
|
284 str s1 = substr (0, pos); |
|
285 str s2 = substr (pos, len()); |
|
286 |
|
287 clear(); |
|
288 append (s1); |
|
289 append (c); |
|
290 append (s2); |
|
291 } |
|
292 |
|
293 void str::reverse () { |
|
294 char* tmp = new char[alloclen]; |
|
295 strcpy (tmp, text); |
|
296 |
|
297 clear(); |
|
298 curs = 0; |
|
299 resize (alloclen); |
|
300 for (int i = alloclen-1; i >= 0; i--) |
|
301 append (tmp[i]); |
|
302 } |
|
303 |
|
304 void str::repeat (unsigned int n) { |
|
305 char* tmp = new char[alloclen]; |
|
306 strcpy (tmp, text); |
|
307 |
|
308 for (; n > 0; n--) |
|
309 append (tmp); |
|
310 } |
|
311 |
|
312 // ============================================================================ |
|
313 bool str::isnumber () { |
|
314 ITERATE_STRING (u) { |
|
315 // Minus sign as the first character is allowed for negatives |
|
316 if (!u && text[u] == '-') |
|
317 continue; |
|
318 |
|
319 if (text[u] < '0' || text[u] > '9') |
|
320 return false; |
|
321 } |
|
322 return true; |
|
323 } |
|
324 |
|
325 // ============================================================================ |
|
326 bool str::isword () { |
|
327 ITERATE_STRING (u) { |
|
328 // lowercase letters |
|
329 if (text[u] >= 'a' || text[u] <= 'z') |
|
330 continue; |
|
331 |
|
332 // uppercase letters |
|
333 if (text[u] >= 'A' || text[u] <= 'Z') |
|
334 continue; |
|
335 |
|
336 return false; |
|
337 } |
|
338 return true; |
|
339 } |
|
340 |
|
341 // ============================================================================ |
|
342 int str::compare (const char* c) { |
|
343 return strcmp (text, c); |
|
344 } |
|
345 |
|
346 int str::compare (str c) { |
|
347 return compare (c.chars()); |
|
348 } |
|
349 |
|
350 int str::icompare (const char* c) { |
|
351 return icompare (str ((char*)c)); |
|
352 } |
|
353 |
|
354 int str::icompare (str b) { |
|
355 return strcmp (tolower().chars(), b.tolower().chars()); |
|
356 } |
|
357 |
|
358 // ============================================================================ |
|
359 str str::tolower () { |
|
360 str n = text; |
|
361 |
|
362 for (uint u = 0; u < len(); u++) { |
|
363 if (n[u] > 'A' && n[u] < 'Z') |
|
364 n.text[u] += ('a' - 'A'); |
|
365 } |
|
366 |
|
367 return n; |
|
368 } |
|
369 |
|
370 // ============================================================================ |
|
371 str str::toupper () { |
|
372 str n = text; |
|
373 |
|
374 for (uint u = 0; u < len(); u++) { |
|
375 if (n[u] > 'a' && n[u] < 'z') |
|
376 n.text[u] -= ('A' - 'a'); |
|
377 } |
|
378 |
|
379 return n; |
|
380 } |
|
381 |
|
382 // ============================================================================ |
|
383 unsigned int str::count (char* c) { |
|
384 unsigned int r = 0; |
|
385 unsigned int tmp = 0; |
|
386 ITERATE_STRING (u) { |
|
387 if (text[u] == c[r]) { |
|
388 r++; |
|
389 if (r == strlen (c)) { |
|
390 r = 0; |
|
391 tmp++; |
|
392 } |
|
393 } else { |
|
394 if (r != 0) |
|
395 u--; |
|
396 r = 0; |
|
397 } |
|
398 } |
|
399 |
|
400 return tmp; |
|
401 } |
|
402 |
|
403 // ============================================================================ |
|
404 array<str> str::split (str del) { |
|
405 array<str> res; |
|
406 unsigned int a = 0; |
|
407 |
|
408 // Find all separators and store the text left to them. |
|
409 while (1) { |
|
410 unsigned int b = first (del, a); |
|
411 |
|
412 if (b == len()) |
|
413 break; |
|
414 |
|
415 res.push (substr (a, b)); |
|
416 a = b + strlen (del); |
|
417 } |
|
418 |
|
419 // Add the string at the right of the last separator |
|
420 res.push (substr (a, len())); |
|
421 return res; |
|
422 } |
|
423 |
|
424 array<str> str::operator/ (str splitstring) {return split(splitstring);} |
|
425 array<str> str::operator/ (char* splitstring) {return split(splitstring);} |
|
426 array<str> str::operator/ (const char* splitstring) {return split(splitstring);} |
|