Mon, 30 Jul 2012 03:38:02 +0300
Added if() support
| 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. | |
| 3 | 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. | |
| 0 | 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 | ||
|
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
32
diff
changeset
|
48 | #define ITERATE_STRING(u) \ |
|
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
32
diff
changeset
|
49 | for (unsigned int u = 0; u < strlen (text); u++) |
| 0 | 50 | |
| 51 | // ============================================================================ | |
| 52 | // vdynformat: Try to write to a formatted string with size bytes first, if | |
| 53 | // that fails, double the size and keep recursing until it works. | |
| 54 | char* vdynformat (const char* c, va_list v, unsigned int size) { | |
| 55 | char* buffer = new char[size]; | |
| 56 | int r = vsnprintf (buffer, size-1, c, v); | |
| 57 | if (r > (int)size-1 || r < 0) { | |
| 58 | delete buffer; | |
| 59 | buffer = vdynformat (c, v, size*2); | |
| 60 | } | |
| 61 | return buffer; | |
| 62 | } | |
| 63 | ||
| 64 | // ============================================================================ | |
| 65 | str::str () { | |
| 66 | text = new char[1]; | |
| 67 | clear(); | |
| 68 | alloclen = strlen (text); | |
| 69 | } | |
| 70 | ||
| 71 | str::str (const char* c) { | |
| 72 | text = new char[1]; | |
| 73 | clear (); | |
| 74 | alloclen = strlen (text); | |
| 75 | append (c); | |
| 76 | } | |
| 77 | ||
| 78 | str::str (char c) { | |
| 79 | text = new char[1]; | |
| 80 | clear (); | |
| 81 | alloclen = strlen (text); | |
| 82 | append (c); | |
| 83 | } | |
| 84 | ||
| 85 | // ============================================================================ | |
| 86 | void str::clear () { | |
| 87 | delete text; | |
| 88 | text = new char[1]; | |
| 89 | text[0] = '\0'; | |
| 90 | curs = 0; | |
| 91 | } | |
| 92 | ||
| 93 | unsigned int str::len () {return strlen(text);} | |
| 94 | ||
| 95 | // ============================================================================ | |
| 96 | void str::resize (unsigned int len) { | |
| 97 | unsigned int oldlen = strlen (text); | |
| 98 | char* oldtext = new char[oldlen]; | |
| 99 | strncpy (oldtext, text, oldlen); | |
| 100 | ||
| 101 | delete text; | |
| 102 | text = new char[len+1]; | |
|
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
103 | for (unsigned int u = 0; u < len+1; u++) |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
104 | text[u] = 0; |
| 0 | 105 | strncpy (text, oldtext, len); |
| 106 | ||
| 107 | alloclen = len; | |
| 108 | } | |
| 109 | ||
| 110 | // ============================================================================ | |
| 111 | void str::dump () { | |
| 112 | for (unsigned int u = 0; u <= alloclen; u++) | |
|
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
113 | printf ("\t%u. %u (%c)\n", u, text[u], text[u]); |
| 0 | 114 | } |
| 115 | ||
| 116 | // ============================================================================ | |
| 117 | // Adds a new character at the end of the string. | |
| 118 | void str::append (char c) { | |
| 119 | // Out of space, thus resize | |
| 120 | if (curs == alloclen) | |
| 121 | resize (alloclen+1); | |
| 122 | text[curs] = c; | |
| 123 | curs++; | |
| 124 | } | |
| 125 | ||
| 126 | void str::append (const char* c) { | |
| 127 | resize (alloclen + strlen (c)); | |
| 128 | ||
| 129 | for (unsigned int u = 0; u < strlen (c); u++) { | |
| 130 | if (c[u] != 0) | |
| 131 | append (c[u]); | |
| 132 | } | |
| 133 | } | |
| 134 | ||
| 135 | void str::append (str c) { | |
| 136 | append (c.chars()); | |
| 137 | } | |
| 138 | ||
| 139 | // ============================================================================ | |
| 140 | void str::appendformat (const char* c, ...) { | |
| 141 | va_list v; | |
| 142 | ||
| 143 | va_start (v, c); | |
| 144 | char* buf = vdynformat (c, v, 256); | |
| 145 | va_end (v); | |
| 146 | ||
| 147 | append (buf); | |
| 148 | } | |
| 149 | ||
| 150 | void str::appendformat (str c, ...) { | |
| 151 | va_list v; | |
| 152 | ||
| 153 | va_start (v, c); | |
| 154 | char* buf = vdynformat (c.chars(), v, 256); | |
| 155 | va_end (v); | |
| 156 | ||
| 157 | append (buf); | |
| 158 | } | |
| 159 | ||
| 160 | // ============================================================================ | |
| 161 | char* str::chars () { | |
| 162 | return text; | |
| 163 | } | |
| 164 | ||
| 165 | // ============================================================================ | |
| 166 | unsigned int str::first (const char* c, unsigned int a) { | |
| 167 | unsigned int r = 0; | |
| 168 | unsigned int index = 0; | |
| 169 | for (; a < alloclen; a++) { | |
| 170 | if (text[a] == c[r]) { | |
| 171 | if (r == 0) | |
| 172 | index = a; | |
| 173 | ||
| 174 | r++; | |
| 175 | if (r == strlen (c)) | |
| 176 | return index; | |
| 177 | } else { | |
| 178 | if (r != 0) { | |
| 179 | // If the string sequence broke at this point, we need to | |
| 180 | // check this character again, for a new sequence just | |
| 181 | // might start right here. | |
| 182 | a--; | |
| 183 | } | |
| 184 | ||
| 185 | r = 0; | |
| 186 | } | |
| 187 | } | |
| 188 | ||
| 189 | return len (); | |
| 190 | } | |
| 191 | ||
| 192 | // ============================================================================ | |
| 193 | unsigned int str::last (const char* c, int a) { | |
| 194 | if (a == -1) | |
| 195 | a = len(); | |
| 196 | ||
| 197 | int max = strlen (c)-1; | |
| 198 | ||
| 199 | int r = max; | |
| 200 | for (; a >= 0; a--) { | |
| 201 | if (text[a] == c[r]) { | |
| 202 | r--; | |
| 203 | if (r == -1) | |
| 204 | return a; | |
| 205 | } else { | |
| 206 | if (r != max) | |
| 207 | a++; | |
| 208 | ||
| 209 | r = max; | |
| 210 | } | |
| 211 | } | |
| 212 | ||
| 213 | return len (); | |
| 214 | } | |
| 215 | ||
| 216 | // ============================================================================ | |
| 217 | str str::substr (unsigned int a, unsigned int b) { | |
| 218 | if (a > len()) a = len(); | |
| 219 | if (b > len()) b = len(); | |
| 220 | ||
| 221 | if (b == a) | |
| 222 | return ""; | |
| 223 | ||
| 224 | if (b < a) { | |
| 225 | printf ("str::substr: indices %u and %u given, should be the other way around, swapping..\n", a, b); | |
| 226 | ||
| 227 | // Swap the variables | |
| 228 | unsigned int c = a; | |
| 229 | a = b; | |
| 230 | b = c; | |
| 231 | } | |
| 232 | ||
| 233 | char* s = new char[b-a]; | |
| 234 | strncpy (s, text+a, b-a); | |
| 235 | return str(s); | |
| 236 | } | |
| 237 | ||
| 238 | // ============================================================================ | |
| 239 | void str::remove (unsigned int idx, unsigned int dellen) { | |
| 240 | str s1 = substr (0, idx); | |
| 241 | str s2 = substr (idx + dellen, len()); | |
| 242 | ||
| 243 | clear(); | |
| 244 | ||
| 245 | append (s1); | |
| 246 | append (s2); | |
| 247 | } | |
| 248 | ||
| 249 | // ============================================================================ | |
| 250 | void str::trim (int dellen) { | |
| 251 | if (!dellen) | |
| 252 | return; | |
| 253 | ||
|
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
254 | unsigned int delpos; |
|
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
255 | if (dellen > 0) { |
|
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
256 | delpos = len()-dellen; |
|
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
257 | text[delpos] = 0; |
|
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
258 | curs -= dellen; |
|
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
259 | } else { |
|
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
260 | str s = substr (-dellen, len()); |
|
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
261 | clear(); |
|
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
262 | append (s); |
|
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
263 | } |
| 0 | 264 | } |
| 265 | ||
| 266 | // ============================================================================ | |
| 267 | void str::replace (const char* o, const char* n, unsigned int a) { | |
| 268 | unsigned int idx; | |
| 269 | ||
| 270 | while ((idx = first (o, a)) != len()) { | |
| 271 | str s1 = substr (0, idx); | |
| 272 | str s2 = substr (idx + strlen (o), len()); | |
| 273 | ||
| 274 | clear(); | |
| 275 | ||
| 276 | append (s1); | |
| 277 | append (n); | |
| 278 | append (s2); | |
| 279 | } | |
| 280 | } | |
| 281 | ||
| 282 | void str::insert (char* c, unsigned int pos) { | |
| 283 | str s1 = substr (0, pos); | |
| 284 | str s2 = substr (pos, len()); | |
| 285 | ||
| 286 | clear(); | |
| 287 | append (s1); | |
| 288 | append (c); | |
| 289 | append (s2); | |
| 290 | } | |
| 291 | ||
| 292 | void str::reverse () { | |
| 293 | char* tmp = new char[alloclen]; | |
| 294 | strcpy (tmp, text); | |
| 295 | ||
| 296 | clear(); | |
| 297 | curs = 0; | |
| 298 | resize (alloclen); | |
| 299 | for (int i = alloclen-1; i >= 0; i--) | |
| 300 | append (tmp[i]); | |
| 301 | } | |
| 302 | ||
| 303 | void str::repeat (unsigned int n) { | |
| 304 | char* tmp = new char[alloclen]; | |
| 305 | strcpy (tmp, text); | |
| 306 | ||
| 307 | for (; n > 0; n--) | |
| 308 | append (tmp); | |
| 309 | } | |
| 310 | ||
| 311 | // ============================================================================ | |
| 312 | bool str::isnumber () { | |
|
30
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
313 | ITERATE_STRING (u) { |
|
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
314 | // Minus sign as the first character is allowed for negatives |
|
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
315 | if (!u && text[u] == '-') |
|
30
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
316 | continue; |
|
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
317 | |
|
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
318 | if (text[u] < '0' || text[u] > '9') |
|
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
319 | return false; |
|
30
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
320 | } |
|
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
321 | return true; |
| 0 | 322 | } |
| 323 | ||
| 324 | // ============================================================================ | |
| 325 | bool str::isword () { | |
| 326 | ITERATE_STRING (u) { | |
|
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
327 | // lowercase letters |
|
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
328 | if (text[u] >= 'a' || text[u] <= 'z') |
|
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
329 | continue; |
| 0 | 330 | |
|
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
331 | // uppercase letters |
|
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
332 | if (text[u] >= 'A' || text[u] <= 'Z') |
|
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
333 | continue; |
| 0 | 334 | |
| 335 | return false; | |
| 336 | } | |
| 337 | return true; | |
| 338 | } | |
| 339 | ||
| 340 | // ============================================================================ | |
| 341 | int str::compare (const char* c) { | |
| 342 | return strcmp (text, c); | |
| 343 | } | |
| 344 | ||
| 345 | int str::compare (str c) { | |
| 346 | return compare (c.chars()); | |
| 347 | } | |
| 348 | ||
|
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
349 | int str::icompare (const char* c) { |
|
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
350 | return icompare (str ((char*)c)); |
|
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
351 | } |
|
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
352 | |
|
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
353 | int str::icompare (str b) { |
|
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
354 | return strcmp (tolower().chars(), b.tolower().chars()); |
|
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
355 | } |
|
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
356 | |
| 0 | 357 | // ============================================================================ |
|
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
358 | str str::tolower () { |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
359 | str n = text; |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
360 | |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
361 | for (uint u = 0; u < len(); u++) { |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
362 | if (n[u] > 'A' && n[u] < 'Z') |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
363 | n.text[u] += ('a' - 'A'); |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
364 | } |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
365 | |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
366 | return n; |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
367 | } |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
368 | |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
369 | // ============================================================================ |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
370 | str str::toupper () { |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
371 | str n = text; |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
372 | |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
373 | for (uint u = 0; u < len(); u++) { |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
374 | if (n[u] > 'a' && n[u] < 'z') |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
375 | n.text[u] -= ('A' - 'a'); |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
376 | } |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
377 | |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
378 | return n; |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
379 | } |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
380 | |
|
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
381 | // ============================================================================ |
|
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
382 | unsigned int str::count (char* c) { |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
383 | unsigned int r = 0; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
384 | unsigned int tmp = 0; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
385 | ITERATE_STRING (u) { |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
386 | if (text[u] == c[r]) { |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
387 | r++; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
388 | if (r == strlen (c)) { |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
389 | r = 0; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
390 | tmp++; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
391 | } |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
392 | } else { |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
393 | if (r != 0) |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
394 | u--; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
395 | r = 0; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
396 | } |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
397 | } |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
398 | |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
399 | return tmp; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
400 | } |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
401 | |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
402 | // ============================================================================ |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
403 | #if 0 |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
404 | str** str::split (char* del) { |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
405 | unsigned int arrcount = count (del) + 1; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
406 | str** arr = new str* [arrcount]; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
407 | |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
408 | unsigned int a = 0; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
409 | unsigned int index = 0; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
410 | while (1) { |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
411 | unsigned int b = first (del, a+1); |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
412 | printf ("next: %u (<-> %u)\n", b, len()); |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
413 | |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
414 | if (b == len()) |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
415 | break; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
416 | |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
417 | str* x = new str; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
418 | x->append (substr (a, b)); |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
419 | arr[index] = x; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
420 | index++; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
421 | a = b; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
422 | } |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
423 | |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
424 | return arr; |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
425 | } |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
426 | #endif |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
427 | |
|
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
428 | // ============================================================================ |
| 0 | 429 | // OPERATORS |
| 430 | str str::operator + (str& c) { | |
| 431 | append (c); | |
| 432 | return *this; | |
| 433 | } | |
| 434 | ||
| 435 | str& str::operator += (char c) { | |
| 436 | append (c); | |
| 437 | return *this; | |
| 438 | } | |
| 439 | ||
| 440 | str& str::operator += (const char* c) { | |
| 441 | append (c); | |
| 442 | return *this; | |
| 443 | } | |
| 444 | ||
| 445 | str& str::operator += (const str c) { | |
| 446 | append (c); | |
| 447 | return *this; | |
| 448 | } | |
| 449 | ||
| 450 | char str::operator [] (unsigned int pos) { | |
| 451 | return text[pos]; | |
| 452 | } | |
| 453 | ||
| 454 | str::operator char* () const { | |
| 455 | return text; | |
| 456 | } | |
| 457 | ||
| 458 | str::operator int () const {return atoi(text);} | |
| 459 | str::operator unsigned int () const {return atoi(text);} |