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