Mon, 18 Mar 2013 13:16:21 +0200
added Save As function
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. | |
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
|
85 | void str::append (const char c) { |
0 | 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 | ||
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
|
106 | 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
|
107 | 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
|
108 | } |
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
|
109 | |
0 | 110 | // ============================================================================ |
111 | void str::appendformat (const char* c, ...) { | |
112 | va_list v; | |
113 | ||
114 | va_start (v, c); | |
115 | char* buf = vdynformat (c, v, 256); | |
116 | va_end (v); | |
117 | ||
118 | append (buf); | |
119 | delete[] buf; | |
120 | } | |
121 | ||
122 | void str::format (const char* fmt, ...) { | |
123 | clear (); | |
124 | ||
125 | va_list v; | |
126 | ||
127 | va_start (v, fmt); | |
128 | char* buf = vdynformat (fmt, v, 256); | |
129 | va_end (v); | |
130 | ||
131 | append (buf); | |
132 | delete[] buf; | |
133 | } | |
134 | ||
135 | str str::format (...) { | |
136 | va_list va; | |
137 | char* buf; | |
138 | ||
139 | va_start (va, this); | |
140 | buf = vdynformat (text, va, 256); | |
141 | va_end (va); | |
142 | ||
143 | str val = buf; | |
144 | delete[] buf; | |
145 | return val; | |
146 | } | |
147 | ||
148 | // ============================================================================ | |
149 | char* str::chars () { | |
150 | return text; | |
151 | } | |
152 | ||
153 | // ============================================================================ | |
154 | int str::first (const char* c, unsigned int a) { | |
155 | unsigned int r = 0; | |
156 | unsigned int index = 0; | |
157 | for (; a < alloclen; a++) { | |
158 | if (text[a] == c[r]) { | |
159 | if (r == 0) | |
160 | index = a; | |
161 | ||
162 | r++; | |
163 | if (r == strlen (c)) | |
164 | return index; | |
165 | } else { | |
166 | if (r != 0) { | |
167 | // If the string sequence broke at this point, we need to | |
168 | // check this character again, for a new sequence just | |
169 | // might start right here. | |
170 | a--; | |
171 | } | |
172 | ||
173 | r = 0; | |
174 | } | |
175 | } | |
176 | ||
177 | return -1; | |
178 | } | |
179 | ||
180 | // ============================================================================ | |
181 | int str::last (const char* c, int a) { | |
182 | if (a == -1) | |
183 | a = len(); | |
184 | ||
185 | int max = strlen (c)-1; | |
186 | ||
187 | int r = max; | |
188 | for (; a >= 0; a--) { | |
189 | if (text[a] == c[r]) { | |
190 | r--; | |
191 | if (r == -1) | |
192 | return a; | |
193 | } else { | |
194 | if (r != max) | |
195 | a++; | |
196 | ||
197 | r = max; | |
198 | } | |
199 | } | |
200 | ||
201 | return -1; | |
202 | } | |
203 | ||
204 | // ============================================================================ | |
205 | str str::substr (unsigned int a, unsigned int b) { | |
206 | if (a > len()) a = len(); | |
207 | if (b > len()) b = len(); | |
208 | ||
209 | if (b == a) | |
210 | return ""; | |
211 | ||
212 | if (b < a) { | |
213 | printf ("str::substring:: indices %u and %u given, should be the other way around, swapping..\n", a, b); | |
214 | ||
215 | // Swap the variables | |
216 | unsigned int c = a; | |
217 | a = b; | |
218 | b = c; | |
219 | } | |
220 | ||
221 | char* s = new char[b - a + 1]; | |
222 | strncpy (s, text + a, b - a); | |
223 | s[b - a] = '\0'; | |
224 | ||
225 | str other = s; | |
226 | delete[] s; | |
227 | return other; | |
228 | } | |
229 | ||
230 | // ============================================================================ | |
231 | void str::remove (unsigned int idx, unsigned int dellen) { | |
232 | str s1 = substr (0, idx); | |
233 | str s2 = substr (idx + dellen, -1); | |
234 | ||
235 | clear(); | |
236 | ||
237 | append (s1); | |
238 | append (s2); | |
239 | } | |
240 | ||
241 | // ============================================================================ | |
242 | str str::trim (int dellen) { | |
243 | if (dellen > 0) | |
244 | return substr (0, len() - dellen); | |
245 | return substr (-dellen, len()); | |
246 | } | |
247 | ||
248 | // ============================================================================ | |
249 | void str::replace (const char* o, const char* n, unsigned int a) { | |
250 | for (int idx; (idx = first (o, a)) != -1;) { | |
251 | str s1 = substr (0, idx); | |
252 | str s2 = substr (idx + strlen (o), len()); | |
253 | ||
254 | clear(); | |
255 | ||
256 | append (s1); | |
257 | append (n); | |
258 | append (s2); | |
259 | } | |
260 | } | |
261 | ||
262 | // ============================================================================ | |
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
|
263 | // It works otherwise but I'm having trouble with the initializer_list |
0 | 264 | /* |
265 | void str::strip (char c) { | |
266 | strip ({c}); | |
267 | } | |
268 | ||
269 | void str::strip (std::initializer_list<char> unwanted) { | |
270 | str cache = text; | |
271 | uint oldlen = len(); | |
272 | ||
273 | char* buf = new char[oldlen]; | |
274 | char* bufptr = buf; | |
275 | for (uint i = 0; i < oldlen; i++) { | |
276 | bool valid = true; | |
277 | for (const char* j = unwanted.begin(); j < unwanted.end() && valid; j++) | |
278 | if (text[i] == *j) | |
279 | valid = false; | |
280 | ||
281 | if (valid) | |
282 | *bufptr++ = text[i]; | |
283 | } | |
284 | ||
285 | *bufptr = '\0'; | |
286 | assert (bufptr <= buf + oldlen); | |
287 | ||
288 | clear(); | |
289 | append (buf); | |
290 | ||
291 | delete[] buf; | |
292 | } | |
293 | */ | |
294 | ||
295 | void str::insert (char* c, unsigned int pos) { | |
296 | str s1 = substr (0, pos); | |
297 | str s2 = substr (pos, len()); | |
298 | ||
299 | clear(); | |
300 | append (s1); | |
301 | append (c); | |
302 | append (s2); | |
303 | } | |
304 | ||
305 | str str::reverse () { | |
306 | char* buf = new char[len() + 1]; | |
307 | ||
308 | for (uint i = 0; i < len(); i++) | |
309 | buf[i] = text[len() - i - 1]; | |
310 | buf[len()] = '\0'; | |
311 | ||
312 | str other = buf; | |
313 | delete[] buf; | |
314 | return other; | |
315 | } | |
316 | ||
317 | str str::repeat (int n) { | |
318 | assert (n >= 0); | |
319 | ||
320 | str other; | |
321 | for (int i = 0; i < n; i++) | |
322 | other += text; | |
323 | return other; | |
324 | } | |
325 | ||
326 | // ============================================================================ | |
327 | bool str::isnumber () { | |
328 | ITERATE_STRING (u) { | |
329 | // Minus sign as the first character is allowed for negatives | |
330 | if (!u && text[u] == '-') | |
331 | continue; | |
332 | ||
333 | if (text[u] < '0' || text[u] > '9') | |
334 | return false; | |
335 | } | |
336 | return true; | |
337 | } | |
338 | ||
339 | // ============================================================================ | |
340 | bool str::isword () { | |
341 | ITERATE_STRING (u) { | |
342 | // lowercase letters | |
343 | if (text[u] >= 'a' || text[u] <= 'z') | |
344 | continue; | |
345 | ||
346 | // uppercase letters | |
347 | if (text[u] >= 'A' || text[u] <= 'Z') | |
348 | continue; | |
349 | ||
350 | return false; | |
351 | } | |
352 | return true; | |
353 | } | |
354 | ||
355 | int str::instanceof (const char* c, uint n) { | |
356 | unsigned int r = 0; | |
357 | unsigned int index = 0; | |
358 | unsigned int x = 0; | |
359 | for (uint a = 0; a < alloclen; a++) { | |
360 | if (text[a] == c[r]) { | |
361 | if (r == 0) | |
362 | index = a; | |
363 | ||
364 | r++; | |
365 | if (r == strlen (c)) { | |
366 | if (x++ == n) | |
367 | return index; | |
368 | r = 0; | |
369 | } | |
370 | } else { | |
371 | if (r != 0) | |
372 | a--; | |
373 | r = 0; | |
374 | } | |
375 | } | |
376 | ||
377 | return -1; | |
378 | } | |
379 | ||
380 | // ============================================================================ | |
381 | int str::compare (const char* c) { | |
382 | return strcmp (text, c); | |
383 | } | |
384 | ||
385 | int str::compare (str c) { | |
386 | return compare (c.chars()); | |
387 | } | |
388 | ||
389 | int str::icompare (const char* c) { | |
390 | return icompare (str ((char*)c)); | |
391 | } | |
392 | ||
393 | int str::icompare (str b) { | |
394 | return strcmp (tolower().chars(), b.tolower().chars()); | |
395 | } | |
396 | ||
397 | // ============================================================================ | |
398 | str str::tolower () { | |
399 | str n = text; | |
400 | ||
401 | for (uint u = 0; u < len(); u++) { | |
402 | if (n[u] >= 'A' && n[u] < 'Z') | |
403 | n.text[u] += ('a' - 'A'); | |
404 | } | |
405 | ||
406 | return n; | |
407 | } | |
408 | ||
409 | // ============================================================================ | |
410 | str str::toupper () { | |
411 | str n = text; | |
412 | ||
413 | for (uint u = 0; u < len(); u++) { | |
414 | if (n[u] >= 'a' && n[u] < 'z') | |
415 | n.text[u] -= ('a' - 'A'); | |
416 | } | |
417 | ||
418 | return n; | |
419 | } | |
420 | ||
421 | // ============================================================================ | |
422 | unsigned str::count (char c) { | |
423 | unsigned n = 0; | |
424 | ITERATE_STRING (u) | |
425 | if (text[u] == c) | |
426 | n++; | |
427 | return n; | |
428 | } | |
429 | ||
430 | unsigned str::count (char* c) { | |
431 | unsigned int r = 0; | |
432 | unsigned int tmp = 0; | |
433 | ITERATE_STRING (u) { | |
434 | if (text[u] == c[r]) { | |
435 | r++; | |
436 | if (r == strlen (c)) { | |
437 | r = 0; | |
438 | tmp++; | |
439 | } | |
440 | } else { | |
441 | if (r != 0) | |
442 | u--; | |
443 | r = 0; | |
444 | } | |
445 | } | |
446 | ||
447 | return tmp; | |
448 | } | |
449 | ||
450 | // ============================================================================ | |
451 | std::vector<str> str::split (str del) { | |
452 | std::vector<str> res; | |
453 | unsigned int a = 0; | |
454 | ||
455 | // Find all separators and store the text left to them. | |
456 | while (1) { | |
457 | int b = first (del, a); | |
458 | ||
459 | if (b == -1) | |
460 | break; | |
461 | ||
462 | res.push_back (substr (a, b)); | |
463 | a = b + strlen (del); | |
464 | } | |
465 | ||
466 | // Add the string at the right of the last separator | |
467 | res.push_back (substr (a, len())); | |
468 | return res; | |
469 | } | |
470 | ||
471 | std::vector<str> str::operator/ (str splitstring) {return split(splitstring);} | |
472 | 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
|
473 | 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
|
474 | |
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
|
475 | 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
|
476 | 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
|
477 | 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
|
478 | } |