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