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