1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include <stdio.h> |
---|
6 | #include <ctype.h> |
---|
7 | |
---|
8 | #include "param.h" |
---|
9 | #include <frams/util/extvalue.h> |
---|
10 | #include "common/framsg.h" |
---|
11 | #include <frams/util/sstringutils.h> |
---|
12 | |
---|
13 | //#define SAVE_ALL_NAMES |
---|
14 | #define SAVE_SELECTED_NAMES |
---|
15 | #define WARN_MISSING_NAME |
---|
16 | |
---|
17 | char MakeCodeGuardHappy; |
---|
18 | |
---|
19 | ParamEntry empty_paramtab[] = |
---|
20 | { { "Empty", 1, 0, "Empty", }, { 0, 0, 0, }, }; |
---|
21 | |
---|
22 | static void czytdotyldy(VirtFILE *f, SString &s) |
---|
23 | { |
---|
24 | SString temp; |
---|
25 | int z; |
---|
26 | char last_char = 0; |
---|
27 | while ((z = fgetc(f)) != EOF) |
---|
28 | { |
---|
29 | if (z == '~') |
---|
30 | if (last_char != '\\') break; |
---|
31 | last_char = (char)z; |
---|
32 | temp += last_char; |
---|
33 | } |
---|
34 | s = temp; |
---|
35 | } |
---|
36 | |
---|
37 | static const char *strchrlimit(const char *t, int ch, const char *limit) |
---|
38 | { |
---|
39 | int n = (int)(limit - t); |
---|
40 | for (; (n > 0) && *t; t++, n--) |
---|
41 | if (*t == ch) return t; |
---|
42 | return 0; |
---|
43 | } |
---|
44 | |
---|
45 | void ParamInterface::copyFrom(ParamInterface *src) |
---|
46 | { |
---|
47 | int n = getPropCount(); |
---|
48 | ExtValue v; |
---|
49 | int j; |
---|
50 | for (int i = 0; i < n; i++) |
---|
51 | if ((!(flags(i)&PARAM_READONLY)) |
---|
52 | && (*type(i) != 'p')) |
---|
53 | { |
---|
54 | j = src->findId(id(i)); |
---|
55 | if (j < 0) continue; |
---|
56 | src->get(j, v); |
---|
57 | set(i, v); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | void ParamInterface::quickCopyFrom(ParamInterface *src) |
---|
62 | { |
---|
63 | int n = getPropCount(); |
---|
64 | ExtValue v; |
---|
65 | for (int i = 0; i < n; i++) |
---|
66 | if ((!(flags(i)&PARAM_READONLY)) |
---|
67 | && (*type(i) != 'p')) |
---|
68 | { |
---|
69 | src->get(i, v); |
---|
70 | set(i, v); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | int ParamInterface::getMinMax(int prop, paInt& minumum, paInt& maximum, paInt &def) |
---|
75 | { |
---|
76 | const char* t = type(prop) + 1; |
---|
77 | while (*t) if (*t == ' ') break; else t++; |
---|
78 | return sscanf(t, PA_INT_SCANF " " PA_INT_SCANF " " PA_INT_SCANF, &minumum, &maximum, &def); |
---|
79 | } |
---|
80 | |
---|
81 | int ParamInterface::getMinMax(int prop, double& minumum, double& maximum, double& def) |
---|
82 | { |
---|
83 | const char* t = type(prop) + 1; |
---|
84 | while (*t) if (*t == ' ') break; else t++; |
---|
85 | return sscanf(t, "%lg %lg %lg", &minumum, &maximum, &def); |
---|
86 | } |
---|
87 | |
---|
88 | int ParamInterface::getMinMax(int prop, int& minumum, int& maximum, SString& def) |
---|
89 | { |
---|
90 | const char* t = type(prop) + 1; |
---|
91 | while (*t) if (*t == ' ') break; else t++; |
---|
92 | int ret = sscanf(t, "%d %d", &minumum, &maximum); |
---|
93 | def = SString::empty(); |
---|
94 | if (ret == 2) |
---|
95 | { |
---|
96 | while (*t == ' ') t++; |
---|
97 | for (int skip_fields = 2; skip_fields > 0; skip_fields--) |
---|
98 | { |
---|
99 | while (*t) if (*t == ' ') break; else t++; |
---|
100 | while (*t == ' ') t++; |
---|
101 | } |
---|
102 | if (*t) |
---|
103 | { |
---|
104 | const char* end = strchr(t, '~'); |
---|
105 | if (!end) |
---|
106 | end = t + strlen(t); |
---|
107 | while ((end > t) && (end[-1] == ' ')) end--; |
---|
108 | def = SString(t, end - t); |
---|
109 | } |
---|
110 | return 3; |
---|
111 | } |
---|
112 | else |
---|
113 | return ret; |
---|
114 | } |
---|
115 | |
---|
116 | void ParamInterface::setDefault() |
---|
117 | { |
---|
118 | for (int i = 0; i < getPropCount(); i++) |
---|
119 | setDefault(i); |
---|
120 | } |
---|
121 | |
---|
122 | void ParamInterface::setMin() |
---|
123 | { |
---|
124 | for (int i = 0; i < getPropCount(); i++) |
---|
125 | setMin(i); |
---|
126 | } |
---|
127 | |
---|
128 | void ParamInterface::setMax() |
---|
129 | { |
---|
130 | for (int i = 0; i < getPropCount(); i++) |
---|
131 | setMax(i); |
---|
132 | } |
---|
133 | |
---|
134 | void ParamInterface::setDefault(int i) |
---|
135 | { |
---|
136 | const char *t = type(i); |
---|
137 | switch (*t) |
---|
138 | { |
---|
139 | case 'f': |
---|
140 | { |
---|
141 | double mn = 0, mx = 0, def = 0; |
---|
142 | if (getMinMax(i, mn, mx, def) < 3) def = mn; |
---|
143 | setDouble(i, def); |
---|
144 | } |
---|
145 | break; |
---|
146 | case 'd': |
---|
147 | { |
---|
148 | paInt mn = 0, mx = 0, def = 0; |
---|
149 | if (getMinMax(i, mn, mx, def) < 3) def = mn; |
---|
150 | setInt(i, def); |
---|
151 | } |
---|
152 | break; |
---|
153 | case 's': case 'x': |
---|
154 | { |
---|
155 | int mn, mx; SString def; |
---|
156 | getMinMax(i, mn, mx, def); |
---|
157 | if (*t == 's') |
---|
158 | setString(i, def); |
---|
159 | else |
---|
160 | { |
---|
161 | if (def.len() > 0) setExtValue(i, ExtValue(def)); else setExtValue(i, ExtValue::empty()); |
---|
162 | } |
---|
163 | } |
---|
164 | break; |
---|
165 | case 'o': |
---|
166 | setObject(i, ExtObject::empty()); |
---|
167 | break; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | void ParamInterface::setMin(int i) |
---|
172 | { |
---|
173 | const char *t = type(i); |
---|
174 | switch (*t) |
---|
175 | { |
---|
176 | case 'f': |
---|
177 | { |
---|
178 | double mn = 0, mx = 0, def = 0; |
---|
179 | getMinMax(i, mn, mx, def); |
---|
180 | setDouble(i, mn); |
---|
181 | } |
---|
182 | break; |
---|
183 | case 'd': |
---|
184 | { |
---|
185 | paInt mn = 0, mx = 0, def = 0; |
---|
186 | getMinMax(i, mn, mx, def); |
---|
187 | setInt(i, mn); |
---|
188 | } |
---|
189 | break; |
---|
190 | default: set(i, ""); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | void ParamInterface::setMax(int i) |
---|
195 | { |
---|
196 | const char *t = type(i); |
---|
197 | switch (*t) |
---|
198 | { |
---|
199 | case 'f': |
---|
200 | { |
---|
201 | double mn = 0, mx = 0, def = 0; |
---|
202 | getMinMax(i, mn, mx, def); |
---|
203 | setDouble(i, mx); |
---|
204 | } |
---|
205 | break; |
---|
206 | case 'd': |
---|
207 | { |
---|
208 | paInt mn = 0, mx = 0, def = 0; |
---|
209 | getMinMax(i, mn, mx, def); |
---|
210 | setInt(i, mx); |
---|
211 | } |
---|
212 | break; |
---|
213 | default: set(i, ""); |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | SString ParamInterface::getStringById(const char*prop) |
---|
218 | { |
---|
219 | int i = findId(prop); if (i >= 0) return getString(i); else return SString(); |
---|
220 | } |
---|
221 | paInt ParamInterface::getIntById(const char*prop) |
---|
222 | { |
---|
223 | int i = findId(prop); if (i >= 0) return getInt(i); else return 0; |
---|
224 | } |
---|
225 | double ParamInterface::getDoubleById(const char*prop) |
---|
226 | { |
---|
227 | int i = findId(prop); if (i >= 0) return getDouble(i); else return 0; |
---|
228 | } |
---|
229 | ExtObject ParamInterface::getObjectById(const char*prop) |
---|
230 | { |
---|
231 | int i = findId(prop); if (i >= 0) return getObject(i); else return ExtObject(); |
---|
232 | } |
---|
233 | ExtValue ParamInterface::getExtValueById(const char*prop) |
---|
234 | { |
---|
235 | int i = findId(prop); if (i >= 0) return getExtValue(i); else return ExtValue(); |
---|
236 | } |
---|
237 | |
---|
238 | int ParamInterface::setIntById(const char* prop, paInt v) |
---|
239 | { |
---|
240 | int i = findId(prop); if (i >= 0) return setInt(i, v); else return PSET_NOPROPERTY; |
---|
241 | } |
---|
242 | int ParamInterface::setDoubleById(const char* prop, double v) |
---|
243 | { |
---|
244 | int i = findId(prop); if (i >= 0) return setDouble(i, v); else return PSET_NOPROPERTY; |
---|
245 | } |
---|
246 | int ParamInterface::setStringById(const char* prop, const SString &v) |
---|
247 | { |
---|
248 | int i = findId(prop); if (i >= 0) return setString(i, v); else return PSET_NOPROPERTY; |
---|
249 | } |
---|
250 | int ParamInterface::setObjectById(const char* prop, const ExtObject &v) |
---|
251 | { |
---|
252 | int i = findId(prop); if (i >= 0) return setObject(i, v); else return PSET_NOPROPERTY; |
---|
253 | } |
---|
254 | int ParamInterface::setExtValueById(const char* prop, const ExtValue &v) |
---|
255 | { |
---|
256 | int i = findId(prop); if (i >= 0) return setExtValue(i, v); else return PSET_NOPROPERTY; |
---|
257 | } |
---|
258 | int ParamInterface::setById(const char* prop, const ExtValue &v) |
---|
259 | { |
---|
260 | int i = findId(prop); if (i >= 0) return set(i, v); else return PSET_NOPROPERTY; |
---|
261 | } |
---|
262 | |
---|
263 | int ParamInterface::save(VirtFILE* f, const char* altname, bool force) |
---|
264 | { |
---|
265 | const char *p; |
---|
266 | SString ws; |
---|
267 | int err = 0, i; |
---|
268 | bool withname = false; |
---|
269 | if ((altname == NULL) || (altname[0] != 0)) |
---|
270 | { |
---|
271 | err |= (fputs(altname ? altname : getName(), f) == EOF); |
---|
272 | err |= (fputs(":\n", f) == EOF); |
---|
273 | withname = true; |
---|
274 | } |
---|
275 | for (i = 0; p = id(i); i++) |
---|
276 | err |= saveprop(f, i, p, force); |
---|
277 | if (withname) |
---|
278 | err |= (fputs("\n", f) == EOF); |
---|
279 | return err; |
---|
280 | } |
---|
281 | |
---|
282 | const char* ParamInterface::SERIALIZATION_PREFIX = "@Serialized:"; |
---|
283 | |
---|
284 | int ParamInterface::saveprop(VirtFILE* f, int i, const char* p, bool force) |
---|
285 | { |
---|
286 | if ((flags(i)&PARAM_DONTSAVE) && (!force)) return 0; |
---|
287 | const char *typ = type(i); |
---|
288 | if (*typ == 'p') return 0; |
---|
289 | |
---|
290 | const char *t, *w; |
---|
291 | SString ws; |
---|
292 | int err = 0, cr; |
---|
293 | |
---|
294 | err |= (fputs(p, f) == EOF); fputc(':', f); |
---|
295 | cr = 0; |
---|
296 | if ((*typ == 'x') || (*typ == 'o')) |
---|
297 | { |
---|
298 | ExtValue ex; |
---|
299 | get(i, ex); |
---|
300 | ws = SString(SERIALIZATION_PREFIX) + ex.serialize(); |
---|
301 | } |
---|
302 | else |
---|
303 | ws = get(i); |
---|
304 | quoteTilde(ws); |
---|
305 | w = ws; |
---|
306 | if (ws.len() > 50) cr = 1; |
---|
307 | else for (t = w; *t; t++) if ((*t == 10) || (*t == 13)) { cr = 1; break; } |
---|
308 | if (cr) fputs("~\n", f); |
---|
309 | err |= (fputs(w, f) == EOF); |
---|
310 | err |= (fputs(cr ? "~\n" : "\n", f) == EOF); |
---|
311 | return err; |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | int SimpleAbstractParam::isequal(int i, void* defdata) |
---|
316 | { // defdata->member == object->member ? |
---|
317 | void *backup = object; |
---|
318 | switch (type(i)[0]) |
---|
319 | { |
---|
320 | case 'd': |
---|
321 | { |
---|
322 | select(defdata); |
---|
323 | paInt x = getInt(i); |
---|
324 | select(backup); |
---|
325 | return x == getInt(i); |
---|
326 | } |
---|
327 | case 'f': |
---|
328 | { |
---|
329 | select(defdata); |
---|
330 | double x = getDouble(i); |
---|
331 | select(backup); |
---|
332 | return x == getDouble(i); |
---|
333 | } |
---|
334 | case 's': |
---|
335 | { |
---|
336 | select(defdata); |
---|
337 | SString x = getString(i); |
---|
338 | select(backup); |
---|
339 | return x == getString(i); |
---|
340 | } |
---|
341 | } |
---|
342 | return 1; |
---|
343 | } |
---|
344 | |
---|
345 | void SimpleAbstractParam::save2(SString& f, void *defdata, bool addcr, bool all_names) |
---|
346 | { // defdata!=NULL -> does not save default values |
---|
347 | const char *p; |
---|
348 | int i; |
---|
349 | int needlabel = 0; |
---|
350 | int first = 1; |
---|
351 | SString val; |
---|
352 | SString t; |
---|
353 | int fl; |
---|
354 | // t+=SString(getName()); t+=':'; |
---|
355 | for (i = 0; p = id(i); i++) |
---|
356 | if (!((fl = flags(i))&PARAM_DONTSAVE)) |
---|
357 | { |
---|
358 | if (defdata && isequal(i, defdata)) |
---|
359 | needlabel = 1; |
---|
360 | else |
---|
361 | { |
---|
362 | if (!first) t += ", "; |
---|
363 | #ifndef SAVE_ALL_NAMES |
---|
364 | #ifdef SAVE_SELECTED_NAMES |
---|
365 | if (needlabel || all_names || !(fl & PARAM_CANOMITNAME)) |
---|
366 | #else |
---|
367 | if (needlabel) |
---|
368 | #endif |
---|
369 | #endif |
---|
370 | { |
---|
371 | t += p; t += "="; needlabel = 0; |
---|
372 | } |
---|
373 | if (type(i)[0] == 's') |
---|
374 | { // string - special case |
---|
375 | SString str = getString(i); |
---|
376 | if (strContainsOneOf(str, ", \\\n\r\t\"")) |
---|
377 | { |
---|
378 | t += "\""; |
---|
379 | sstringQuote(str); |
---|
380 | t += str; |
---|
381 | t += "\""; |
---|
382 | } |
---|
383 | else |
---|
384 | t += str; |
---|
385 | } |
---|
386 | else |
---|
387 | t += get(i); |
---|
388 | first = 0; |
---|
389 | } |
---|
390 | } |
---|
391 | if (addcr) |
---|
392 | t += "\n"; |
---|
393 | f += t; |
---|
394 | } |
---|
395 | |
---|
396 | int ParamInterface::load(VirtFILE* f, bool warn_unknown_fields, bool *abortable) |
---|
397 | { |
---|
398 | SString buf; |
---|
399 | int i; |
---|
400 | const char *p, *p0; |
---|
401 | int p_len; |
---|
402 | bool loaded; |
---|
403 | int fields_loaded = 0; |
---|
404 | while (((!abortable) || (!*abortable)) && loadSStringLine(f, buf)) |
---|
405 | { |
---|
406 | const char* t = (const char*)buf; |
---|
407 | p0 = t; while ((*p0 == ' ') || (*p0 == '\t')) p0++; |
---|
408 | if (!*p0) break; |
---|
409 | if (p0[0] == '#') continue; |
---|
410 | p = strchr(p0, ':'); if (!p) continue; |
---|
411 | p_len = (int)(p - p0); |
---|
412 | loaded = false; |
---|
413 | if (p_len && ((i = findIdn(p0, p_len)) >= 0)) |
---|
414 | { |
---|
415 | if (!(flags(i)&PARAM_DONTLOAD)) |
---|
416 | { |
---|
417 | if (p0[p_len + 1] == '~') |
---|
418 | { |
---|
419 | SString s; |
---|
420 | czytdotyldy(f, s); |
---|
421 | removeCR(s); |
---|
422 | int ch; while ((ch = fgetc(f)) != EOF) if (ch == '\n') break; |
---|
423 | unquoteTilde(s); |
---|
424 | set(i, (const char*)s); |
---|
425 | } |
---|
426 | else |
---|
427 | { |
---|
428 | set(i, p0 + p_len + 1); |
---|
429 | } |
---|
430 | fields_loaded++; |
---|
431 | loaded = true; |
---|
432 | } |
---|
433 | } |
---|
434 | else if (warn_unknown_fields) |
---|
435 | { |
---|
436 | SString name(p0, p_len); |
---|
437 | FMprintf("ParamInterface", "load", FMLV_WARN, "Unknown property '%s' while reading object '%s' (ignored)", (const char*)name, getName()); |
---|
438 | } |
---|
439 | |
---|
440 | if ((!loaded) && (p0[p_len + 1] == '~')) |
---|
441 | { // eat unrecognized multiline field |
---|
442 | SString s; |
---|
443 | czytdotyldy(f, s); |
---|
444 | int ch; while ((ch = fgetc(f)) != EOF) if (ch == '\n') break; |
---|
445 | } |
---|
446 | } |
---|
447 | return fields_loaded; |
---|
448 | } |
---|
449 | |
---|
450 | |
---|
451 | /* |
---|
452 | SString SimpleAbstractParam::getString(int i) |
---|
453 | { |
---|
454 | char *t; |
---|
455 | switch (*(t=type(i))) |
---|
456 | { |
---|
457 | case 'd': |
---|
458 | { |
---|
459 | for (i=atol(get(i));i>=0;i--) if (t) t=strchr(t+1,'~'); |
---|
460 | if (t) |
---|
461 | { |
---|
462 | t++; |
---|
463 | char *t2=strchr(t,'~'); |
---|
464 | if (!t2) t2=t+strlen(t); |
---|
465 | SString str; |
---|
466 | strncpy(str.directWrite(t2-t),t,t2-t); |
---|
467 | str.endWrite(t2-t); |
---|
468 | return str; |
---|
469 | } |
---|
470 | } |
---|
471 | } |
---|
472 | return get(i); |
---|
473 | } |
---|
474 | */ |
---|
475 | |
---|
476 | int ParamInterface::findId(const char* n) |
---|
477 | { |
---|
478 | int i; const char *p; |
---|
479 | for (i = 0; p = id(i); i++) if (!strcmp(n, p)) return i; |
---|
480 | return -1; |
---|
481 | } |
---|
482 | |
---|
483 | int ParamInterface::findIdn(const char* naz, int n) |
---|
484 | { |
---|
485 | int i; const char *p; |
---|
486 | for (i = 0; p = id(i); i++) if ((!strncmp(naz, p, n)) && (!p[n])) return i; |
---|
487 | return -1; |
---|
488 | } |
---|
489 | |
---|
490 | void ParamInterface::get(int i, ExtValue &ret) |
---|
491 | { |
---|
492 | switch (type(i)[0]) |
---|
493 | { |
---|
494 | case 'd': ret.setInt(getInt(i)); break; |
---|
495 | case 'f': ret.setDouble(getDouble(i)); break; |
---|
496 | case 's': ret.setString(getString(i)); break; |
---|
497 | case 'o': ret.setObject(getObject(i)); break; |
---|
498 | case 'x': ret = getExtValue(i); break; |
---|
499 | default: FMprintf("ParamInterface", "get", FMLV_ERROR, "'%s.%s' is not a field", getName(), id(i)); |
---|
500 | } |
---|
501 | } |
---|
502 | |
---|
503 | static bool stringIsNumeric(const char* str) |
---|
504 | {// /-?.?[0-9]+/ |
---|
505 | if (!str) return false; |
---|
506 | if (*str == '-') str++; |
---|
507 | if (*str == '.') str++; |
---|
508 | return isdigit(*str) != 0; |
---|
509 | } |
---|
510 | |
---|
511 | int ParamInterface::setInt(int i, const char* str) |
---|
512 | { |
---|
513 | if (!stringIsNumeric(str)) |
---|
514 | { |
---|
515 | paInt mn, mx, def; |
---|
516 | if (getMinMax(i, mn, mx, def) >= 3) |
---|
517 | return setInt(i, def); |
---|
518 | else |
---|
519 | return setInt(i, (paInt)0); |
---|
520 | } |
---|
521 | else |
---|
522 | return setInt(i, ExtValue::getInt(str)); |
---|
523 | } |
---|
524 | |
---|
525 | int ParamInterface::setDouble(int i, const char* str) |
---|
526 | { |
---|
527 | if (!stringIsNumeric(str)) |
---|
528 | { |
---|
529 | double mn, mx, def; |
---|
530 | if (getMinMax(i, mn, mx, def) >= 3) |
---|
531 | return setDouble(i, def); |
---|
532 | else |
---|
533 | return setDouble(i, (double)0); |
---|
534 | } |
---|
535 | else |
---|
536 | return setDouble(i, ExtValue::getDouble(str)); |
---|
537 | } |
---|
538 | |
---|
539 | int ParamInterface::set(int i, const ExtValue &v) |
---|
540 | { |
---|
541 | switch (type(i)[0]) |
---|
542 | { |
---|
543 | case 'd': |
---|
544 | if ((v.type == TInt) || (v.type == TDouble)) return setInt(i, v.getInt()); |
---|
545 | else |
---|
546 | { |
---|
547 | if (v.type == TObj) |
---|
548 | FMprintf("ParamInterface", "set", FMLV_WARN, "Getting integer value from object reference (%s)", (const char*)v.getString()); |
---|
549 | return setInt(i, (const char*)v.getString()); |
---|
550 | } |
---|
551 | case 'f': |
---|
552 | if ((v.type == TInt) || (v.type == TDouble)) return setDouble(i, v.getDouble()); |
---|
553 | else |
---|
554 | { |
---|
555 | if (v.type == TObj) |
---|
556 | FMprintf("ParamInterface", "set", FMLV_WARN, "Getting floating point value from object reference (%s)", (const char*)v.getString()); |
---|
557 | return setDouble(i, (const char*)v.getString()); |
---|
558 | } |
---|
559 | case 's': { SString t = v.getString(); return setString(i, t); } |
---|
560 | case 'o': return setObject(i, v.getObject()); |
---|
561 | case 'x': return setExtValue(i, v); |
---|
562 | default: FMprintf("ParamInterface", "set", FMLV_ERROR, "'%s.%s' is not a field", getName(), id(i)); |
---|
563 | } |
---|
564 | return 0; |
---|
565 | } |
---|
566 | |
---|
567 | int ParamInterface::set(int i, const char *v) |
---|
568 | { |
---|
569 | char typ = type(i)[0]; |
---|
570 | switch (typ) |
---|
571 | { |
---|
572 | case 'd': return setInt(i, v); |
---|
573 | case 'f': return setDouble(i, v); |
---|
574 | case 's': { SString t(v); return setString(i, t); } |
---|
575 | case 'x': case 'o': |
---|
576 | { |
---|
577 | ExtValue e; |
---|
578 | const char* after; |
---|
579 | if (!strncmp(v, SERIALIZATION_PREFIX, strlen(SERIALIZATION_PREFIX))) |
---|
580 | { |
---|
581 | after = e.deserialize(v + strlen(SERIALIZATION_PREFIX)); |
---|
582 | if ((after == NULL) || (*after)) |
---|
583 | FMprintf("ParamInterface", "set", FMLV_WARN, "serialization format mismatch in %s.%s", (getName() ? getName() : "<Unknown>"), id(i)); |
---|
584 | } |
---|
585 | else if ((after = e.parseNumber(v)) && (*after == 0)) //consumed the whole string |
---|
586 | { |
---|
587 | //OK! |
---|
588 | } |
---|
589 | else |
---|
590 | { |
---|
591 | e.setString(SString(v)); |
---|
592 | } |
---|
593 | if (typ == 'x') |
---|
594 | return setExtValue(i, e); |
---|
595 | else |
---|
596 | return setObject(i, e.getObject()); |
---|
597 | } |
---|
598 | } |
---|
599 | return 0; |
---|
600 | } |
---|
601 | |
---|
602 | SString ParamInterface::getText(int i) |
---|
603 | { |
---|
604 | const char *t; |
---|
605 | if ((*(t = type(i))) == 'd') |
---|
606 | { |
---|
607 | paInt mn, mx, def; |
---|
608 | int value = getInt(i); |
---|
609 | if (getMinMax(i, mn, mx, def) >= 2) |
---|
610 | { |
---|
611 | if (value > mx) |
---|
612 | return get(i); |
---|
613 | value -= mn; |
---|
614 | } |
---|
615 | if (value < 0) return get(i); |
---|
616 | for (; value >= 0; value--) if (t) t = strchr(t + 1, '~'); |
---|
617 | if (t) |
---|
618 | { |
---|
619 | t++; |
---|
620 | const char *t2 = strchr(t, '~'); |
---|
621 | if (!t2) t2 = t + strlen(t); |
---|
622 | return SString(t, (int)(t2 - t)); |
---|
623 | } |
---|
624 | } |
---|
625 | return get(i); |
---|
626 | } |
---|
627 | |
---|
628 | SString ParamInterface::get(int i) |
---|
629 | { |
---|
630 | switch (type(i)[0]) |
---|
631 | { |
---|
632 | case 'd': return SString::valueOf(getInt(i)); |
---|
633 | case 'f': return SString::valueOf(getDouble(i)); |
---|
634 | case 's': return getString(i); |
---|
635 | } |
---|
636 | ExtValue v; |
---|
637 | get(i, v); |
---|
638 | return v.getString(); |
---|
639 | } |
---|
640 | |
---|
641 | |
---|
642 | //////////////////////////////// PARAM //////////////////////////////////// |
---|
643 | |
---|
644 | #ifdef DEBUG |
---|
645 | void SimpleAbstractParam::sanityCheck(int i) |
---|
646 | { |
---|
647 | ParamEntry *pe=entry(i); |
---|
648 | |
---|
649 | const char* t=pe->type; |
---|
650 | const char* err=NULL; |
---|
651 | |
---|
652 | if (*t=='p') |
---|
653 | { |
---|
654 | if (pe->fun1==NULL) |
---|
655 | err="no procedure defined"; |
---|
656 | } |
---|
657 | else |
---|
658 | { |
---|
659 | if (!(pe->flags & PARAM_READONLY)) |
---|
660 | { //write access |
---|
661 | if ((pe->fun2==NULL)&&(pe->offset==PARAM_ILLEGAL_OFFSET)) |
---|
662 | err="no field defined (GETONLY without PARAM_READONLY?)"; |
---|
663 | } |
---|
664 | } |
---|
665 | if (err!=NULL) |
---|
666 | FMprintf("SimpleAbstractParam","sanityCheck", FMLV_ERROR, |
---|
667 | "Invalid ParamEntry for %s.%s (%s)", getName(), pe->id, err); |
---|
668 | } |
---|
669 | #endif |
---|
670 | |
---|
671 | void *SimpleAbstractParam::getTarget(int i) |
---|
672 | { |
---|
673 | return (void*)(((char*)object) + entry(i)->offset); |
---|
674 | //return &(object->*(entry(i)->fldptr)); |
---|
675 | } |
---|
676 | |
---|
677 | ///////// get |
---|
678 | |
---|
679 | #ifdef DEBUG |
---|
680 | #define SANITY_CHECK(i) sanityCheck(i) |
---|
681 | #else |
---|
682 | #define SANITY_CHECK(i) |
---|
683 | #endif |
---|
684 | |
---|
685 | paInt SimpleAbstractParam::getInt(int i) |
---|
686 | { |
---|
687 | SANITY_CHECK(i); |
---|
688 | ExtValue v; |
---|
689 | ParamEntry *pe = entry(i); |
---|
690 | if (pe->fun1) |
---|
691 | { |
---|
692 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
693 | return v.getInt(); |
---|
694 | } |
---|
695 | else |
---|
696 | { |
---|
697 | void *target = getTarget(i); |
---|
698 | return *((paInt*)target); |
---|
699 | } |
---|
700 | } |
---|
701 | |
---|
702 | double SimpleAbstractParam::getDouble(int i) |
---|
703 | { |
---|
704 | SANITY_CHECK(i); |
---|
705 | ExtValue v; |
---|
706 | ParamEntry *pe = entry(i); |
---|
707 | if (pe->fun1) |
---|
708 | { |
---|
709 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
710 | return v.getDouble(); |
---|
711 | } |
---|
712 | else |
---|
713 | { |
---|
714 | void *target = getTarget(i); |
---|
715 | return *((double*)target); |
---|
716 | } |
---|
717 | } |
---|
718 | |
---|
719 | SString SimpleAbstractParam::getString(int i) |
---|
720 | { |
---|
721 | SANITY_CHECK(i); |
---|
722 | ExtValue v; |
---|
723 | ParamEntry *pe = entry(i); |
---|
724 | if (pe->fun1) |
---|
725 | { |
---|
726 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
727 | return v.getString(); |
---|
728 | } |
---|
729 | else |
---|
730 | { |
---|
731 | void *target = getTarget(i); |
---|
732 | return *((SString*)target); |
---|
733 | } |
---|
734 | } |
---|
735 | |
---|
736 | ExtObject SimpleAbstractParam::getObject(int i) |
---|
737 | { |
---|
738 | SANITY_CHECK(i); |
---|
739 | ExtValue v; |
---|
740 | ParamEntry *pe = entry(i); |
---|
741 | if (pe->fun1) |
---|
742 | { |
---|
743 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
744 | return v.getObject(); |
---|
745 | } |
---|
746 | else |
---|
747 | { |
---|
748 | void *target = getTarget(i); |
---|
749 | return *((ExtObject*)target); |
---|
750 | } |
---|
751 | } |
---|
752 | |
---|
753 | ExtValue SimpleAbstractParam::getExtValue(int i) |
---|
754 | { |
---|
755 | SANITY_CHECK(i); |
---|
756 | ExtValue v; |
---|
757 | ParamEntry *pe = entry(i); |
---|
758 | if (pe->fun1) |
---|
759 | { |
---|
760 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
761 | return v; |
---|
762 | } |
---|
763 | else |
---|
764 | { |
---|
765 | void *target = getTarget(i); |
---|
766 | return *((ExtValue*)target); |
---|
767 | } |
---|
768 | } |
---|
769 | |
---|
770 | |
---|
771 | //////// set |
---|
772 | |
---|
773 | int SimpleAbstractParam::setInt(int i, paInt x) |
---|
774 | { |
---|
775 | SANITY_CHECK(i); |
---|
776 | ExtValue v; |
---|
777 | ParamEntry *pe = entry(i); |
---|
778 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
779 | paInt xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
780 | paInt mn = 0, mx = 0; |
---|
781 | int result = 0; |
---|
782 | const char* t = pe->type + 1; |
---|
783 | while (*t) if (*t == ' ') break; else t++; |
---|
784 | if (sscanf(t, PA_INT_SCANF " " PA_INT_SCANF, &mn, &mx) == 2) |
---|
785 | if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking |
---|
786 | { |
---|
787 | if (x < mn) { x = mn; result = PSET_HITMIN; } |
---|
788 | else if (x > mx) { x = mx; result = PSET_HITMAX; } |
---|
789 | } |
---|
790 | |
---|
791 | if (pe->fun2) |
---|
792 | { |
---|
793 | v.setInt(x); |
---|
794 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
795 | } |
---|
796 | else |
---|
797 | { |
---|
798 | void *target = getTarget(i); |
---|
799 | if (dontcheckchanges || (*((paInt*)target) != x)) |
---|
800 | { |
---|
801 | result |= PSET_CHANGED; |
---|
802 | *((paInt*)target) = x; |
---|
803 | } |
---|
804 | } |
---|
805 | messageOnExceedRange(i, result, xcopy); |
---|
806 | return result; |
---|
807 | } |
---|
808 | |
---|
809 | int SimpleAbstractParam::setDouble(int i, double x) |
---|
810 | { |
---|
811 | SANITY_CHECK(i); |
---|
812 | ExtValue v; |
---|
813 | ParamEntry *pe = entry(i); |
---|
814 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
815 | double xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
816 | double mn = 0, mx = 0; |
---|
817 | int result = 0; |
---|
818 | const char* t = pe->type + 1; |
---|
819 | while (*t) if (*t == ' ') break; else t++; |
---|
820 | if (sscanf(t, "%lg %lg", &mn, &mx) == 2) |
---|
821 | if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking |
---|
822 | { |
---|
823 | if (x < mn) { x = mn; result = PSET_HITMIN; } |
---|
824 | else if (x > mx) { x = mx; result = PSET_HITMAX; } |
---|
825 | } |
---|
826 | |
---|
827 | if (pe->fun2) |
---|
828 | { |
---|
829 | v.setDouble(x); |
---|
830 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
831 | } |
---|
832 | else |
---|
833 | { |
---|
834 | void *target = getTarget(i); |
---|
835 | if (dontcheckchanges || (*((double*)target) != x)) |
---|
836 | { |
---|
837 | result |= PSET_CHANGED; |
---|
838 | *((double*)target) = x; |
---|
839 | } |
---|
840 | } |
---|
841 | messageOnExceedRange(i, result, xcopy); |
---|
842 | return result; |
---|
843 | } |
---|
844 | |
---|
845 | int SimpleAbstractParam::setString(int i, const SString& x) |
---|
846 | { |
---|
847 | SANITY_CHECK(i); |
---|
848 | ExtValue v; |
---|
849 | SString vs; |
---|
850 | const SString *xx = &x; |
---|
851 | ParamEntry *pe = entry(i); |
---|
852 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
853 | SString xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
854 | const char* t = pe->type + 1; |
---|
855 | while (*t) if (*t == ' ') break; else t++; |
---|
856 | int mn = 0, mx = 0; |
---|
857 | int result = 0; |
---|
858 | if (sscanf(t, "%d %d", &mn, &mx) == 2) //using getMinMax would also get default value, which is not needed here |
---|
859 | { |
---|
860 | if ((x.len() > mx) && (mx > 0)) |
---|
861 | { |
---|
862 | vs = x.substr(0, mx); |
---|
863 | xx = &vs; |
---|
864 | result |= PSET_HITMAX; |
---|
865 | } |
---|
866 | } |
---|
867 | |
---|
868 | if (pe->fun2) |
---|
869 | { |
---|
870 | v.setString(*xx); |
---|
871 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
872 | } |
---|
873 | else |
---|
874 | { |
---|
875 | void *target = getTarget(i); |
---|
876 | if (dontcheckchanges || (!(*((SString*)target) == *xx))) |
---|
877 | { |
---|
878 | result |= PSET_CHANGED; |
---|
879 | *((SString*)target) = *xx; |
---|
880 | } |
---|
881 | } |
---|
882 | messageOnExceedRange(i, result, xcopy); |
---|
883 | return result; |
---|
884 | } |
---|
885 | |
---|
886 | int SimpleAbstractParam::setObject(int i, const ExtObject& x) |
---|
887 | { |
---|
888 | SANITY_CHECK(i); |
---|
889 | ExtValue v; |
---|
890 | ParamEntry *pe = entry(i); |
---|
891 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
892 | ExtObject xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
893 | if (pe->fun2) |
---|
894 | { |
---|
895 | v.setObject(x); |
---|
896 | int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
897 | messageOnExceedRange(i, result, xcopy); |
---|
898 | return result; |
---|
899 | } |
---|
900 | else |
---|
901 | { |
---|
902 | void *target = getTarget(i); |
---|
903 | *((ExtObject*)target) = x; |
---|
904 | return PSET_CHANGED; |
---|
905 | } |
---|
906 | } |
---|
907 | |
---|
908 | int SimpleAbstractParam::setExtValue(int i, const ExtValue& x) |
---|
909 | { |
---|
910 | SANITY_CHECK(i); |
---|
911 | ParamEntry *pe = entry(i); |
---|
912 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
913 | ExtValue xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
914 | if (pe->fun2) |
---|
915 | { |
---|
916 | int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x); |
---|
917 | messageOnExceedRange(i, result, xcopy); |
---|
918 | return result; |
---|
919 | } |
---|
920 | else |
---|
921 | { |
---|
922 | void *target = getTarget(i); |
---|
923 | *((ExtValue*)target) = x; |
---|
924 | return PSET_CHANGED; |
---|
925 | } |
---|
926 | } |
---|
927 | |
---|
928 | void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret) |
---|
929 | { |
---|
930 | SANITY_CHECK(i); |
---|
931 | ParamEntry *pe = entry(i); |
---|
932 | if (!pe) return; |
---|
933 | if (pe->fun1 && (pe->type[0] == 'p')) |
---|
934 | (*(void(*)(void*, ExtValue*, ExtValue*))pe->fun1)(object, args, ret); |
---|
935 | else |
---|
936 | { |
---|
937 | FMprintf("SimpleAbstractParam", "call", FMLV_ERROR, |
---|
938 | (*pe->type != 'p') ? "'%s.%s' is not a function" : "Internal error - undefined function pointer for '%s.%s'", getName(), pe->id); |
---|
939 | ret->setInvalid(); |
---|
940 | } |
---|
941 | } |
---|
942 | |
---|
943 | void SimpleAbstractParam::setDefault() |
---|
944 | { |
---|
945 | bool save = dontcheckchanges; |
---|
946 | dontcheckchanges = 1; |
---|
947 | ParamInterface::setDefault(); |
---|
948 | dontcheckchanges = save; |
---|
949 | } |
---|
950 | |
---|
951 | void SimpleAbstractParam::setDefault(int i) |
---|
952 | { |
---|
953 | bool save = dontcheckchanges; |
---|
954 | dontcheckchanges = 1; |
---|
955 | ParamInterface::setDefault(i); |
---|
956 | dontcheckchanges = save; |
---|
957 | } |
---|
958 | |
---|
959 | // Returns the address of the beginning of the line. |
---|
960 | // len = line length (without \n). |
---|
961 | // 0 may mean the line with length=0 or the end of the SString. |
---|
962 | // poz is advanced to the beginning of the next line. |
---|
963 | // A typical loop: for(poz=0;poz<s.d;) {line=getline(s,poz,len);... |
---|
964 | static const char *getline(const SString &s, int &poz, int &len) |
---|
965 | { |
---|
966 | const char *beg = (const char*)s + poz; |
---|
967 | if (poz >= s.len()) { poz = s.len(); len = 0; return (const char*)s + s.len(); } |
---|
968 | const char *lf = strchr(beg, '\n'); |
---|
969 | if (!lf) { lf = (const char*)s + s.len() - 1; poz = s.len(); } |
---|
970 | else { poz = (int)(lf - (const char*)s) + 1; if (poz > s.len()) poz = s.len(); } |
---|
971 | while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break; |
---|
972 | len = (int)(lf - beg) + 1; |
---|
973 | return beg; |
---|
974 | } |
---|
975 | |
---|
976 | int ParamInterface::load2(const SString &s, int &poz) |
---|
977 | { |
---|
978 | int i; // the index number of the parameter |
---|
979 | int tmpi; |
---|
980 | int len; |
---|
981 | int ret; |
---|
982 | int fields_loaded = 0; |
---|
983 | const char *t, *lin, *end; |
---|
984 | const char *equals_sign, *comma_sign; |
---|
985 | char remember; |
---|
986 | const char *quote, *quote2; |
---|
987 | const char *value, *valstop; |
---|
988 | SString tmpvalue; |
---|
989 | if (poz >= s.len()) return fields_loaded; |
---|
990 | t = (const char*)s + poz; |
---|
991 | |
---|
992 | lin = getline(s, poz, len); // all fields must be encoded in a single line |
---|
993 | if (!len) return fields_loaded; // empty line = end |
---|
994 | i = 0; |
---|
995 | end = lin + len; |
---|
996 | while (t < end) |
---|
997 | { |
---|
998 | // processing a single field |
---|
999 | while (strchr(" \n\r\t", *t)) if (t < end) t++; else return fields_loaded; |
---|
1000 | |
---|
1001 | comma_sign = strchrlimit(t, ',', end); if (!comma_sign) comma_sign = end; |
---|
1002 | quote = strchrlimit(t, '\"', comma_sign); |
---|
1003 | if (quote) |
---|
1004 | { |
---|
1005 | quote2 = skipQuoteString(quote + 1, end); |
---|
1006 | if (quote2 > comma_sign) |
---|
1007 | { |
---|
1008 | comma_sign = strchrlimit(quote2 + 1, ',', end); |
---|
1009 | if (!comma_sign) comma_sign = end; |
---|
1010 | } |
---|
1011 | equals_sign = strchrlimit(t, '=', quote); |
---|
1012 | } |
---|
1013 | else |
---|
1014 | { |
---|
1015 | equals_sign = strchrlimit(t, '=', comma_sign); |
---|
1016 | quote2 = 0; |
---|
1017 | } |
---|
1018 | if (equals_sign == t) { t++; equals_sign = 0; } |
---|
1019 | if (comma_sign == t) // skip empty value |
---|
1020 | { |
---|
1021 | t++; i++; |
---|
1022 | continue; |
---|
1023 | } |
---|
1024 | if (equals_sign) // have parameter name |
---|
1025 | { |
---|
1026 | tmpi = findIdn(t, (int)(equals_sign - t)); |
---|
1027 | i = tmpi; |
---|
1028 | if (tmpi < 0) |
---|
1029 | { |
---|
1030 | SString name(t, (int)(equals_sign - t)); |
---|
1031 | FMprintf("Param", "load2", FMLV_WARN, "Unknown property '%s' while reading object '%s' (ignored)", (const char*)name, getName()); |
---|
1032 | } |
---|
1033 | t = equals_sign + 1; // t=value |
---|
1034 | } |
---|
1035 | #ifdef WARN_MISSING_NAME |
---|
1036 | else |
---|
1037 | #ifdef SAVE_SELECTED_NAMES |
---|
1038 | if (!(flags(i)&PARAM_CANOMITNAME)) |
---|
1039 | #endif |
---|
1040 | { |
---|
1041 | FMprintf("Param", "load2", FMLV_WARN, "Missing property name in '%s' (assuming '%s')", |
---|
1042 | getName(), id(i) ? id(i) : "unknown property?"); |
---|
1043 | } |
---|
1044 | #endif |
---|
1045 | if ((i >= 0) && id(i)) |
---|
1046 | { |
---|
1047 | value = t; |
---|
1048 | if (quote) |
---|
1049 | { |
---|
1050 | tmpvalue.copyFrom(quote + 1, (int)(quote2 - quote) - 1); |
---|
1051 | sstringUnquote(tmpvalue); |
---|
1052 | value = tmpvalue; |
---|
1053 | valstop = quote2; |
---|
1054 | } |
---|
1055 | else |
---|
1056 | if (comma_sign < end) valstop = comma_sign; else valstop = end; |
---|
1057 | |
---|
1058 | remember = *valstop; |
---|
1059 | *(char*)valstop = 0; |
---|
1060 | ret = set(i, value); |
---|
1061 | fields_loaded++; |
---|
1062 | if (ret&(PSET_HITMAX | PSET_HITMIN)) |
---|
1063 | FMprintf("Param", "load2", FMLV_WARN, "Adjusted '%s' in '%s' (was too %s)", |
---|
1064 | id(i), getName(), (ret&PSET_HITMAX) ? "big" : "small"); |
---|
1065 | *(char*)valstop = remember; |
---|
1066 | } |
---|
1067 | |
---|
1068 | if (i >= 0) i++; |
---|
1069 | #ifdef __CODEGUARD__ |
---|
1070 | if (comma_sign<end-1) t=comma_sign+1; else return fields_loaded; |
---|
1071 | #else |
---|
1072 | t = comma_sign + 1; |
---|
1073 | #endif |
---|
1074 | } |
---|
1075 | return fields_loaded; |
---|
1076 | } |
---|
1077 | |
---|
1078 | int Param::grmember(int g, int a) |
---|
1079 | { |
---|
1080 | if ((getGroupCount() < 2) && (!g)) |
---|
1081 | return (a < getPropCount()) ? a : -9999; |
---|
1082 | |
---|
1083 | ParamEntry *e = entry(0); |
---|
1084 | int x = 0, i = 0; |
---|
1085 | for (; e->id; i++, e++) |
---|
1086 | { |
---|
1087 | if (e->group == g) |
---|
1088 | if (a == x) return i; else x++; |
---|
1089 | } |
---|
1090 | return -9999; |
---|
1091 | } |
---|