Fri, 13 Jul 2012 17:20:51 +0300
Initial commit
0 | 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 Skulltag Development Team nor the names of its | |
15 | * contributors may be used to endorse or promote products derived from this | |
16 | * software without 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 "str.h" | |
46 | #include "common.h" | |
47 | ||
48 | #define ITERATE_STRING(u) for (unsigned int u = 0; u < strlen (text); u++) | |
49 | ||
50 | // ============================================================================ | |
51 | // vdynformat: Try to write to a formatted string with size bytes first, if | |
52 | // that fails, double the size and keep recursing until it works. | |
53 | char* vdynformat (const char* c, va_list v, unsigned int size) { | |
54 | char* buffer = new char[size]; | |
55 | int r = vsnprintf (buffer, size-1, c, v); | |
56 | if (r > (int)size-1 || r < 0) { | |
57 | delete buffer; | |
58 | buffer = vdynformat (c, v, size*2); | |
59 | } | |
60 | return buffer; | |
61 | } | |
62 | ||
63 | // ============================================================================ | |
64 | str::str () { | |
65 | text = new char[1]; | |
66 | clear(); | |
67 | alloclen = strlen (text); | |
68 | } | |
69 | ||
70 | str::str (const char* c) { | |
71 | text = new char[1]; | |
72 | clear (); | |
73 | alloclen = strlen (text); | |
74 | append (c); | |
75 | } | |
76 | ||
77 | str::str (char c) { | |
78 | text = new char[1]; | |
79 | clear (); | |
80 | alloclen = strlen (text); | |
81 | append (c); | |
82 | } | |
83 | ||
84 | // ============================================================================ | |
85 | void str::clear () { | |
86 | delete text; | |
87 | text = new char[1]; | |
88 | text[0] = '\0'; | |
89 | curs = 0; | |
90 | } | |
91 | ||
92 | unsigned int str::len () {return strlen(text);} | |
93 | ||
94 | // ============================================================================ | |
95 | void str::resize (unsigned int len) { | |
96 | unsigned int oldlen = strlen (text); | |
97 | char* oldtext = new char[oldlen]; | |
98 | strncpy (oldtext, text, oldlen); | |
99 | ||
100 | delete text; | |
101 | text = new char[len+1]; | |
102 | strncpy (text, oldtext, len); | |
103 | ||
104 | alloclen = len; | |
105 | } | |
106 | ||
107 | // ============================================================================ | |
108 | void str::dump () { | |
109 | for (unsigned int u = 0; u <= alloclen; u++) | |
110 | printf ("\t%u. %d (%c)\n", u, text[u], text[u]); | |
111 | } | |
112 | ||
113 | // ============================================================================ | |
114 | // Adds a new character at the end of the string. | |
115 | void str::append (char c) { | |
116 | // Out of space, thus resize | |
117 | if (curs == alloclen) | |
118 | resize (alloclen+1); | |
119 | ||
120 | text[curs] = c; | |
121 | curs++; | |
122 | } | |
123 | ||
124 | void str::append (const char* c) { | |
125 | resize (alloclen + strlen (c)); | |
126 | ||
127 | for (unsigned int u = 0; u < strlen (c); u++) { | |
128 | if (c[u] != 0) | |
129 | append (c[u]); | |
130 | } | |
131 | } | |
132 | ||
133 | void str::append (str c) { | |
134 | append (c.chars()); | |
135 | } | |
136 | ||
137 | // ============================================================================ | |
138 | void str::appendformat (const char* c, ...) { | |
139 | va_list v; | |
140 | ||
141 | va_start (v, c); | |
142 | char* buf = vdynformat (c, v, 256); | |
143 | va_end (v); | |
144 | ||
145 | append (buf); | |
146 | } | |
147 | ||
148 | void str::appendformat (str c, ...) { | |
149 | va_list v; | |
150 | ||
151 | va_start (v, c); | |
152 | char* buf = vdynformat (c.chars(), v, 256); | |
153 | va_end (v); | |
154 | ||
155 | append (buf); | |
156 | } | |
157 | ||
158 | // ============================================================================ | |
159 | char* str::chars () { | |
160 | return text; | |
161 | } | |
162 | ||
163 | // ============================================================================ | |
164 | unsigned int str::first (const char* c, unsigned int a) { | |
165 | unsigned int r = 0; | |
166 | unsigned int index = 0; | |
167 | for (; a < alloclen; a++) { | |
168 | if (text[a] == c[r]) { | |
169 | if (r == 0) | |
170 | index = a; | |
171 | ||
172 | r++; | |
173 | if (r == strlen (c)) | |
174 | return index; | |
175 | } else { | |
176 | if (r != 0) { | |
177 | // If the string sequence broke at this point, we need to | |
178 | // check this character again, for a new sequence just | |
179 | // might start right here. | |
180 | a--; | |
181 | } | |
182 | ||
183 | r = 0; | |
184 | } | |
185 | } | |
186 | ||
187 | return len (); | |
188 | } | |
189 | ||
190 | // ============================================================================ | |
191 | unsigned int str::last (const char* c, int a) { | |
192 | if (a == -1) | |
193 | a = len(); | |
194 | ||
195 | int max = strlen (c)-1; | |
196 | ||
197 | int r = max; | |
198 | for (; a >= 0; a--) { | |
199 | if (text[a] == c[r]) { | |
200 | r--; | |
201 | if (r == -1) | |
202 | return a; | |
203 | } else { | |
204 | if (r != max) | |
205 | a++; | |
206 | ||
207 | r = max; | |
208 | } | |
209 | } | |
210 | ||
211 | return len (); | |
212 | } | |
213 | ||
214 | // ============================================================================ | |
215 | str str::substr (unsigned int a, unsigned int b) { | |
216 | if (a > len()) a = len(); | |
217 | if (b > len()) b = len(); | |
218 | ||
219 | if (b == a) | |
220 | return ""; | |
221 | ||
222 | if (b < a) { | |
223 | printf ("str::substr: indices %u and %u given, should be the other way around, swapping..\n", a, b); | |
224 | ||
225 | // Swap the variables | |
226 | unsigned int c = a; | |
227 | a = b; | |
228 | b = c; | |
229 | } | |
230 | ||
231 | char* s = new char[b-a]; | |
232 | strncpy (s, text+a, b-a); | |
233 | return str(s); | |
234 | } | |
235 | ||
236 | // ============================================================================ | |
237 | void str::remove (unsigned int idx, unsigned int dellen) { | |
238 | str s1 = substr (0, idx); | |
239 | str s2 = substr (idx + dellen, len()); | |
240 | ||
241 | clear(); | |
242 | ||
243 | append (s1); | |
244 | append (s2); | |
245 | } | |
246 | ||
247 | // ============================================================================ | |
248 | void str::trim (int dellen) { | |
249 | if (!dellen) | |
250 | return; | |
251 | ||
252 | str s; | |
253 | // If dellen is positive, trim from the end, | |
254 | // if negative, trim from beginning. | |
255 | if (dellen > 0) | |
256 | s = substr (0, len()-dellen); | |
257 | else | |
258 | s = substr (-dellen, len()); | |
259 | ||
260 | clear(); | |
261 | append (s); | |
262 | } | |
263 | ||
264 | // ============================================================================ | |
265 | void str::replace (const char* o, const char* n, unsigned int a) { | |
266 | unsigned int idx; | |
267 | ||
268 | while ((idx = first (o, a)) != len()) { | |
269 | str s1 = substr (0, idx); | |
270 | str s2 = substr (idx + strlen (o), len()); | |
271 | ||
272 | clear(); | |
273 | ||
274 | append (s1); | |
275 | append (n); | |
276 | append (s2); | |
277 | } | |
278 | } | |
279 | ||
280 | void str::insert (char* c, unsigned int pos) { | |
281 | str s1 = substr (0, pos); | |
282 | str s2 = substr (pos, len()); | |
283 | ||
284 | clear(); | |
285 | append (s1); | |
286 | append (c); | |
287 | append (s2); | |
288 | } | |
289 | ||
290 | void str::reverse () { | |
291 | char* tmp = new char[alloclen]; | |
292 | strcpy (tmp, text); | |
293 | ||
294 | clear(); | |
295 | curs = 0; | |
296 | resize (alloclen); | |
297 | for (int i = alloclen-1; i >= 0; i--) | |
298 | append (tmp[i]); | |
299 | } | |
300 | ||
301 | void str::repeat (unsigned int n) { | |
302 | char* tmp = new char[alloclen]; | |
303 | strcpy (tmp, text); | |
304 | ||
305 | for (; n > 0; n--) | |
306 | append (tmp); | |
307 | } | |
308 | ||
309 | // ============================================================================ | |
310 | bool str::isnumber () { | |
311 | return contentcheck (SCCF_NUMBER); | |
312 | } | |
313 | ||
314 | // ============================================================================ | |
315 | bool str::isword () { | |
316 | return contentcheck (SCCF_WORD); | |
317 | } | |
318 | ||
319 | bool str::contentcheck (int flags) { | |
320 | ITERATE_STRING (u) { | |
321 | if (flags & SCCF_WORD) { | |
322 | // lowercase letters | |
323 | if (text[u] >= 'a' || text[u] <= 'z') | |
324 | continue; | |
325 | ||
326 | // uppercase letters | |
327 | if (text[u] >= 'A' || text[u] <= 'Z') | |
328 | continue; | |
329 | } | |
330 | ||
331 | if (flags & SCCF_NUMBER) { | |
332 | if (text[u] < '0' || text[u] > '9') | |
333 | return false; | |
334 | } | |
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 | // ============================================================================ | |
351 | // OPERATORS | |
352 | str str::operator + (str& c) { | |
353 | append (c); | |
354 | return *this; | |
355 | } | |
356 | ||
357 | str& str::operator += (char c) { | |
358 | append (c); | |
359 | return *this; | |
360 | } | |
361 | ||
362 | str& str::operator += (const char* c) { | |
363 | append (c); | |
364 | return *this; | |
365 | } | |
366 | ||
367 | str& str::operator += (const str c) { | |
368 | append (c); | |
369 | return *this; | |
370 | } | |
371 | ||
372 | char str::operator [] (unsigned int pos) { | |
373 | return text[pos]; | |
374 | } | |
375 | ||
376 | str::operator char* () const { | |
377 | return text; | |
378 | } | |
379 | ||
380 | str::operator int () const {return atoi(text);} | |
381 | str::operator unsigned int () const {return atoi(text);} |