Mon, 18 Mar 2013 03:38:51 +0200
begin work on subfile caching
0 | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <string.h> | |
4 | #include <stdarg.h> | |
5 | #include <assert.h> | |
6 | // #include <initializer_list> | |
7 | #include "str.h" | |
18
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
8 | #include "common.h" |
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
9 | #include "misc.h" |
0 | 10 | |
11 | #define ITERATE_STRING(u) \ | |
12 | for (unsigned int u = 0; u < strlen (text); u++) | |
13 | ||
14 | // ============================================================================ | |
15 | // vdynformat: Try to write to a formatted string with size bytes first, if | |
16 | // that fails, double the size and keep recursing until it works. | |
17 | char* vdynformat (const char* csFormat, va_list vArgs, long lSize) { | |
18 | char* buffer = new char[lSize]; | |
19 | int r = vsnprintf (buffer, lSize - 1, csFormat, vArgs); | |
20 | if (r > (signed)(lSize - 1) || r < 0) { | |
21 | delete[] buffer; | |
22 | buffer = vdynformat (csFormat, vArgs, lSize * 2); | |
23 | } | |
24 | return buffer; | |
25 | } | |
26 | ||
27 | // ============================================================================ | |
28 | str::str () { | |
29 | text = new char[1]; | |
30 | clear(); | |
31 | alloclen = strlen (text); | |
32 | } | |
33 | ||
34 | str::str (const char* c) { | |
35 | text = new char[1]; | |
36 | clear (); | |
37 | alloclen = strlen (text); | |
38 | append (c); | |
39 | } | |
40 | ||
41 | str::str (char c) { | |
42 | text = new char[1]; | |
43 | clear (); | |
44 | alloclen = strlen (text); | |
45 | append (c); | |
46 | } | |
47 | ||
48 | str::~str () { | |
49 | // delete[] text; | |
50 | } | |
51 | ||
52 | // ============================================================================ | |
53 | void str::clear () { | |
54 | delete[] text; | |
55 | text = new char[1]; | |
56 | text[0] = '\0'; | |
57 | curs = 0; | |
58 | alloclen = 0; | |
59 | } | |
60 | ||
61 | // ============================================================================ | |
62 | void str::resize (unsigned int len) { | |
63 | unsigned int oldlen = strlen (text); | |
64 | char* oldtext = new char[oldlen]; | |
65 | strncpy (oldtext, text, oldlen); | |
66 | ||
67 | delete[] text; | |
68 | text = new char[len+1]; | |
69 | for (unsigned int u = 0; u < len+1; u++) | |
70 | text[u] = 0; | |
71 | strncpy (text, oldtext, len); | |
72 | delete[] oldtext; | |
73 | ||
74 | alloclen = len; | |
75 | } | |
76 | ||
77 | // ============================================================================ | |
78 | void str::dump () { | |
79 | for (unsigned int u = 0; u <= alloclen; u++) | |
80 | printf ("\t%u. %u (%c)\n", u, text[u], text[u]); | |
81 | } | |
82 | ||
83 | // ============================================================================ | |
84 | // Adds a new character at the end of the string. | |
85 | void str::append (char c) { | |
86 | // Out of space, thus resize | |
87 | if (curs == alloclen) | |
88 | resize (alloclen + 1); | |
89 | text[curs] = c; | |
90 | curs++; | |
91 | } | |
92 | ||
93 | void str::append (const char* c) { | |
94 | resize (alloclen + strlen (c)); | |
95 | ||
96 | for (unsigned int u = 0; u < strlen (c); u++) { | |
97 | if (c[u] != 0) | |
98 | append (c[u]); | |
99 | } | |
100 | } | |
101 | ||
102 | void str::append (str c) { | |
103 | append (c.chars()); | |
104 | } | |
105 | ||
106 | // ============================================================================ | |
107 | void str::appendformat (const char* c, ...) { | |
108 | va_list v; | |
109 | ||
110 | va_start (v, c); | |
111 | char* buf = vdynformat (c, v, 256); | |
112 | va_end (v); | |
113 | ||
114 | append (buf); | |
115 | delete[] buf; | |
116 | } | |
117 | ||
118 | void str::format (const char* fmt, ...) { | |
119 | clear (); | |
120 | ||
121 | va_list v; | |
122 | ||
123 | va_start (v, fmt); | |
124 | char* buf = vdynformat (fmt, v, 256); | |
125 | va_end (v); | |
126 | ||
127 | append (buf); | |
128 | delete[] buf; | |
129 | } | |
130 | ||
131 | str str::format (...) { | |
132 | va_list va; | |
133 | char* buf; | |
134 | ||
135 | va_start (va, this); | |
136 | buf = vdynformat (text, va, 256); | |
137 | va_end (va); | |
138 | ||
139 | str val = buf; | |
140 | delete[] buf; | |
141 | return val; | |
142 | } | |
143 | ||
144 | // ============================================================================ | |
145 | char* str::chars () { | |
146 | return text; | |
147 | } | |
148 | ||
149 | // ============================================================================ | |
150 | int str::first (const char* c, unsigned int a) { | |
151 | unsigned int r = 0; | |
152 | unsigned int index = 0; | |
153 | for (; a < alloclen; a++) { | |
154 | if (text[a] == c[r]) { | |
155 | if (r == 0) | |
156 | index = a; | |
157 | ||
158 | r++; | |
159 | if (r == strlen (c)) | |
160 | return index; | |
161 | } else { | |
162 | if (r != 0) { | |
163 | // If the string sequence broke at this point, we need to | |
164 | // check this character again, for a new sequence just | |
165 | // might start right here. | |
166 | a--; | |
167 | } | |
168 | ||
169 | r = 0; | |
170 | } | |
171 | } | |
172 | ||
173 | return -1; | |
174 | } | |
175 | ||
176 | // ============================================================================ | |
177 | int str::last (const char* c, int a) { | |
178 | if (a == -1) | |
179 | a = len(); | |
180 | ||
181 | int max = strlen (c)-1; | |
182 | ||
183 | int r = max; | |
184 | for (; a >= 0; a--) { | |
185 | if (text[a] == c[r]) { | |
186 | r--; | |
187 | if (r == -1) | |
188 | return a; | |
189 | } else { | |
190 | if (r != max) | |
191 | a++; | |
192 | ||
193 | r = max; | |
194 | } | |
195 | } | |
196 | ||
197 | return -1; | |
198 | } | |
199 | ||
200 | // ============================================================================ | |
201 | str str::substr (unsigned int a, unsigned int b) { | |
202 | if (a > len()) a = len(); | |
203 | if (b > len()) b = len(); | |
204 | ||
205 | if (b == a) | |
206 | return ""; | |
207 | ||
208 | if (b < a) { | |
209 | printf ("str::substring:: indices %u and %u given, should be the other way around, swapping..\n", a, b); | |
210 | ||
211 | // Swap the variables | |
212 | unsigned int c = a; | |
213 | a = b; | |
214 | b = c; | |
215 | } | |
216 | ||
217 | char* s = new char[b - a + 1]; | |
218 | strncpy (s, text + a, b - a); | |
219 | s[b - a] = '\0'; | |
220 | ||
221 | str other = s; | |
222 | delete[] s; | |
223 | return other; | |
224 | } | |
225 | ||
226 | // ============================================================================ | |
227 | void str::remove (unsigned int idx, unsigned int dellen) { | |
228 | str s1 = substr (0, idx); | |
229 | str s2 = substr (idx + dellen, -1); | |
230 | ||
231 | clear(); | |
232 | ||
233 | append (s1); | |
234 | append (s2); | |
235 | } | |
236 | ||
237 | // ============================================================================ | |
238 | str str::trim (int dellen) { | |
239 | if (dellen > 0) | |
240 | return substr (0, len() - dellen); | |
241 | return substr (-dellen, len()); | |
242 | } | |
243 | ||
244 | // ============================================================================ | |
245 | void str::replace (const char* o, const char* n, unsigned int a) { | |
246 | for (int idx; (idx = first (o, a)) != -1;) { | |
247 | str s1 = substr (0, idx); | |
248 | str s2 = substr (idx + strlen (o), len()); | |
249 | ||
250 | clear(); | |
251 | ||
252 | append (s1); | |
253 | append (n); | |
254 | append (s2); | |
255 | } | |
256 | } | |
257 | ||
258 | // ============================================================================ | |
259 | /* | |
260 | void str::strip (char c) { | |
261 | strip ({c}); | |
262 | } | |
263 | ||
264 | void str::strip (std::initializer_list<char> unwanted) { | |
265 | str cache = text; | |
266 | uint oldlen = len(); | |
267 | ||
268 | char* buf = new char[oldlen]; | |
269 | char* bufptr = buf; | |
270 | for (uint i = 0; i < oldlen; i++) { | |
271 | bool valid = true; | |
272 | for (const char* j = unwanted.begin(); j < unwanted.end() && valid; j++) | |
273 | if (text[i] == *j) | |
274 | valid = false; | |
275 | ||
276 | if (valid) | |
277 | *bufptr++ = text[i]; | |
278 | } | |
279 | ||
280 | *bufptr = '\0'; | |
281 | assert (bufptr <= buf + oldlen); | |
282 | ||
283 | clear(); | |
284 | append (buf); | |
285 | ||
286 | delete[] buf; | |
287 | } | |
288 | */ | |
289 | ||
290 | void str::insert (char* c, unsigned int pos) { | |
291 | str s1 = substr (0, pos); | |
292 | str s2 = substr (pos, len()); | |
293 | ||
294 | clear(); | |
295 | append (s1); | |
296 | append (c); | |
297 | append (s2); | |
298 | } | |
299 | ||
300 | str str::reverse () { | |
301 | char* buf = new char[len() + 1]; | |
302 | ||
303 | for (uint i = 0; i < len(); i++) | |
304 | buf[i] = text[len() - i - 1]; | |
305 | buf[len()] = '\0'; | |
306 | ||
307 | str other = buf; | |
308 | delete[] buf; | |
309 | return other; | |
310 | } | |
311 | ||
312 | str str::repeat (int n) { | |
313 | assert (n >= 0); | |
314 | ||
315 | str other; | |
316 | for (int i = 0; i < n; i++) | |
317 | other += text; | |
318 | return other; | |
319 | } | |
320 | ||
321 | // ============================================================================ | |
322 | bool str::isnumber () { | |
323 | ITERATE_STRING (u) { | |
324 | // Minus sign as the first character is allowed for negatives | |
325 | if (!u && text[u] == '-') | |
326 | continue; | |
327 | ||
328 | if (text[u] < '0' || text[u] > '9') | |
329 | return false; | |
330 | } | |
331 | return true; | |
332 | } | |
333 | ||
334 | // ============================================================================ | |
335 | bool str::isword () { | |
336 | ITERATE_STRING (u) { | |
337 | // lowercase letters | |
338 | if (text[u] >= 'a' || text[u] <= 'z') | |
339 | continue; | |
340 | ||
341 | // uppercase letters | |
342 | if (text[u] >= 'A' || text[u] <= 'Z') | |
343 | continue; | |
344 | ||
345 | return false; | |
346 | } | |
347 | return true; | |
348 | } | |
349 | ||
350 | int str::instanceof (const char* c, uint n) { | |
351 | unsigned int r = 0; | |
352 | unsigned int index = 0; | |
353 | unsigned int x = 0; | |
354 | for (uint a = 0; a < alloclen; a++) { | |
355 | if (text[a] == c[r]) { | |
356 | if (r == 0) | |
357 | index = a; | |
358 | ||
359 | r++; | |
360 | if (r == strlen (c)) { | |
361 | if (x++ == n) | |
362 | return index; | |
363 | r = 0; | |
364 | } | |
365 | } else { | |
366 | if (r != 0) | |
367 | a--; | |
368 | r = 0; | |
369 | } | |
370 | } | |
371 | ||
372 | return -1; | |
373 | } | |
374 | ||
375 | // ============================================================================ | |
376 | int str::compare (const char* c) { | |
377 | return strcmp (text, c); | |
378 | } | |
379 | ||
380 | int str::compare (str c) { | |
381 | return compare (c.chars()); | |
382 | } | |
383 | ||
384 | int str::icompare (const char* c) { | |
385 | return icompare (str ((char*)c)); | |
386 | } | |
387 | ||
388 | int str::icompare (str b) { | |
389 | return strcmp (tolower().chars(), b.tolower().chars()); | |
390 | } | |
391 | ||
392 | // ============================================================================ | |
393 | str str::tolower () { | |
394 | str n = text; | |
395 | ||
396 | for (uint u = 0; u < len(); u++) { | |
397 | if (n[u] >= 'A' && n[u] < 'Z') | |
398 | n.text[u] += ('a' - 'A'); | |
399 | } | |
400 | ||
401 | return n; | |
402 | } | |
403 | ||
404 | // ============================================================================ | |
405 | str str::toupper () { | |
406 | str n = text; | |
407 | ||
408 | for (uint u = 0; u < len(); u++) { | |
409 | if (n[u] >= 'a' && n[u] < 'z') | |
410 | n.text[u] -= ('a' - 'A'); | |
411 | } | |
412 | ||
413 | return n; | |
414 | } | |
415 | ||
416 | // ============================================================================ | |
417 | unsigned str::count (char c) { | |
418 | unsigned n = 0; | |
419 | ITERATE_STRING (u) | |
420 | if (text[u] == c) | |
421 | n++; | |
422 | return n; | |
423 | } | |
424 | ||
425 | unsigned str::count (char* c) { | |
426 | unsigned int r = 0; | |
427 | unsigned int tmp = 0; | |
428 | ITERATE_STRING (u) { | |
429 | if (text[u] == c[r]) { | |
430 | r++; | |
431 | if (r == strlen (c)) { | |
432 | r = 0; | |
433 | tmp++; | |
434 | } | |
435 | } else { | |
436 | if (r != 0) | |
437 | u--; | |
438 | r = 0; | |
439 | } | |
440 | } | |
441 | ||
442 | return tmp; | |
443 | } | |
444 | ||
445 | // ============================================================================ | |
446 | std::vector<str> str::split (str del) { | |
447 | std::vector<str> res; | |
448 | unsigned int a = 0; | |
449 | ||
450 | // Find all separators and store the text left to them. | |
451 | while (1) { | |
452 | int b = first (del, a); | |
453 | ||
454 | if (b == -1) | |
455 | break; | |
456 | ||
457 | res.push_back (substr (a, b)); | |
458 | a = b + strlen (del); | |
459 | } | |
460 | ||
461 | // Add the string at the right of the last separator | |
462 | res.push_back (substr (a, len())); | |
463 | return res; | |
464 | } | |
465 | ||
466 | std::vector<str> str::operator/ (str splitstring) {return split(splitstring);} | |
467 | std::vector<str> str::operator/ (char* splitstring) {return split(splitstring);} | |
18
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
468 | std::vector<str> str::operator/ (const char* splitstring) {return split(splitstring);} |
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
469 | |
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
470 | str& str::operator+= (vertex vrt) { |
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
471 | appendformat ("%s", vrt.getStringRep (false).chars()); |
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
472 | return *this; |
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
473 | } |