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