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 | } |
---|
799 | else |
---|
800 | { |
---|
801 | if (!(pe->flags & PARAM_READONLY)) |
---|
802 | { //write access |
---|
803 | if ((pe->fun2 == NULL) && (pe->offset == PARAM_ILLEGAL_OFFSET)) |
---|
804 | err = "no field defined (GETONLY without PARAM_READONLY?)"; |
---|
805 | } |
---|
806 | } |
---|
807 | if (err != NULL) |
---|
808 | logPrintf("SimpleAbstractParam", "sanityCheck", LOG_ERROR, |
---|
809 | "Invalid ParamEntry for %s.%s (%s)", getName(), pe->id, err); |
---|
810 | } |
---|
811 | #endif |
---|
812 | |
---|
813 | void *SimpleAbstractParam::getTarget(int i) |
---|
814 | { |
---|
815 | return (void*)(((char*)object) + entry(i)->offset); |
---|
816 | //return &(object->*(entry(i)->fldptr)); |
---|
817 | } |
---|
818 | |
---|
819 | ///////// get |
---|
820 | |
---|
821 | #ifdef _DEBUG |
---|
822 | #define SANITY_CHECK(i) sanityCheck(i) |
---|
823 | #else |
---|
824 | #define SANITY_CHECK(i) |
---|
825 | #endif |
---|
826 | |
---|
827 | paInt SimpleAbstractParam::getInt(int i) |
---|
828 | { |
---|
829 | SANITY_CHECK(i); |
---|
830 | ExtValue v; |
---|
831 | ParamEntry *pe = entry(i); |
---|
832 | if (pe->fun1) |
---|
833 | { |
---|
834 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
835 | return v.getInt(); |
---|
836 | } |
---|
837 | else |
---|
838 | { |
---|
839 | void *target = getTarget(i); |
---|
840 | return *((paInt*)target); |
---|
841 | } |
---|
842 | } |
---|
843 | |
---|
844 | double SimpleAbstractParam::getDouble(int i) |
---|
845 | { |
---|
846 | SANITY_CHECK(i); |
---|
847 | ExtValue v; |
---|
848 | ParamEntry *pe = entry(i); |
---|
849 | if (pe->fun1) |
---|
850 | { |
---|
851 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
852 | return v.getDouble(); |
---|
853 | } |
---|
854 | else |
---|
855 | { |
---|
856 | void *target = getTarget(i); |
---|
857 | return *((double*)target); |
---|
858 | } |
---|
859 | } |
---|
860 | |
---|
861 | SString SimpleAbstractParam::getString(int i) |
---|
862 | { |
---|
863 | SANITY_CHECK(i); |
---|
864 | ExtValue v; |
---|
865 | ParamEntry *pe = entry(i); |
---|
866 | if (pe->fun1) |
---|
867 | { |
---|
868 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
869 | return v.getString(); |
---|
870 | } |
---|
871 | else |
---|
872 | { |
---|
873 | void *target = getTarget(i); |
---|
874 | return *((SString*)target); |
---|
875 | } |
---|
876 | } |
---|
877 | |
---|
878 | ExtObject SimpleAbstractParam::getObject(int i) |
---|
879 | { |
---|
880 | SANITY_CHECK(i); |
---|
881 | ExtValue v; |
---|
882 | ParamEntry *pe = entry(i); |
---|
883 | if (pe->fun1) |
---|
884 | { |
---|
885 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
886 | return v.getObject(); |
---|
887 | } |
---|
888 | else |
---|
889 | { |
---|
890 | void *target = getTarget(i); |
---|
891 | return *((ExtObject*)target); |
---|
892 | } |
---|
893 | } |
---|
894 | |
---|
895 | ExtValue SimpleAbstractParam::getExtValue(int i) |
---|
896 | { |
---|
897 | SANITY_CHECK(i); |
---|
898 | ExtValue v; |
---|
899 | ParamEntry *pe = entry(i); |
---|
900 | if (pe->fun1) |
---|
901 | { |
---|
902 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
903 | return v; |
---|
904 | } |
---|
905 | else |
---|
906 | { |
---|
907 | void *target = getTarget(i); |
---|
908 | return *((ExtValue*)target); |
---|
909 | } |
---|
910 | } |
---|
911 | |
---|
912 | |
---|
913 | //////// set |
---|
914 | |
---|
915 | int SimpleAbstractParam::setInt(int i, paInt x) |
---|
916 | { |
---|
917 | SANITY_CHECK(i); |
---|
918 | ExtValue v; |
---|
919 | ParamEntry *pe = entry(i); |
---|
920 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
921 | paInt xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
922 | paInt mn = 0, mx = 0, de = 0; |
---|
923 | int result = 0; |
---|
924 | if (getMinMax(pe->type, mn, mx, de) >= 2) |
---|
925 | if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking |
---|
926 | { |
---|
927 | if (x < mn) { x = mn; result = PSET_HITMIN; } |
---|
928 | else if (x > mx) { x = mx; result = PSET_HITMAX; } |
---|
929 | } |
---|
930 | |
---|
931 | if (pe->fun2) |
---|
932 | { |
---|
933 | v.setInt(x); |
---|
934 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
935 | } |
---|
936 | else |
---|
937 | { |
---|
938 | void *target = getTarget(i); |
---|
939 | if (dontcheckchanges || (*((paInt*)target) != x)) |
---|
940 | { |
---|
941 | result |= PSET_CHANGED; |
---|
942 | *((paInt*)target) = x; |
---|
943 | } |
---|
944 | } |
---|
945 | messageOnExceedRange(i, result, xcopy); |
---|
946 | return result; |
---|
947 | } |
---|
948 | |
---|
949 | int SimpleAbstractParam::setDouble(int i, double x) |
---|
950 | { |
---|
951 | SANITY_CHECK(i); |
---|
952 | ExtValue v; |
---|
953 | ParamEntry *pe = entry(i); |
---|
954 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
955 | double xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
956 | double mn = 0, mx = 0, de = 0; |
---|
957 | int result = 0; |
---|
958 | if (getMinMax(pe->type, mn, mx, de) >= 2) |
---|
959 | if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking |
---|
960 | { |
---|
961 | if (x < mn) { x = mn; result = PSET_HITMIN; } |
---|
962 | else if (x > mx) { x = mx; result = PSET_HITMAX; } |
---|
963 | } |
---|
964 | |
---|
965 | if (pe->fun2) |
---|
966 | { |
---|
967 | v.setDouble(x); |
---|
968 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
969 | } |
---|
970 | else |
---|
971 | { |
---|
972 | void *target = getTarget(i); |
---|
973 | if (dontcheckchanges || (*((double*)target) != x)) |
---|
974 | { |
---|
975 | result |= PSET_CHANGED; |
---|
976 | *((double*)target) = x; |
---|
977 | } |
---|
978 | } |
---|
979 | messageOnExceedRange(i, result, xcopy); |
---|
980 | return result; |
---|
981 | } |
---|
982 | |
---|
983 | int SimpleAbstractParam::setString(int i, const SString& x) |
---|
984 | { |
---|
985 | SANITY_CHECK(i); |
---|
986 | ExtValue v; |
---|
987 | SString vs; |
---|
988 | const SString *xx = &x; |
---|
989 | ParamEntry *pe = entry(i); |
---|
990 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
991 | SString xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
992 | const char* t = pe->type + 1; |
---|
993 | while (*t) if (*t == ' ') break; else t++; |
---|
994 | int mn = 0, mx = 0; |
---|
995 | int result = 0; |
---|
996 | if (sscanf(t, "%d %d", &mn, &mx) == 2) //using getMinMax would also get default value, which is not needed here |
---|
997 | { |
---|
998 | if ((x.len() > mx) && (mx > 0)) |
---|
999 | { |
---|
1000 | vs = x.substr(0, mx); |
---|
1001 | xx = &vs; |
---|
1002 | result |= PSET_HITMAX; |
---|
1003 | } |
---|
1004 | } |
---|
1005 | |
---|
1006 | if (pe->fun2) |
---|
1007 | { |
---|
1008 | v.setString(*xx); |
---|
1009 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
1010 | } |
---|
1011 | else |
---|
1012 | { |
---|
1013 | void *target = getTarget(i); |
---|
1014 | if (dontcheckchanges || (!(*((SString*)target) == *xx))) |
---|
1015 | { |
---|
1016 | result |= PSET_CHANGED; |
---|
1017 | *((SString*)target) = *xx; |
---|
1018 | } |
---|
1019 | } |
---|
1020 | messageOnExceedRange(i, result, xcopy); |
---|
1021 | return result; |
---|
1022 | } |
---|
1023 | |
---|
1024 | int SimpleAbstractParam::setObject(int i, const ExtObject& x) |
---|
1025 | { |
---|
1026 | SANITY_CHECK(i); |
---|
1027 | ExtValue v; |
---|
1028 | ParamEntry *pe = entry(i); |
---|
1029 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
1030 | if (pe->flags&PARAM_OBJECTSET) |
---|
1031 | { |
---|
1032 | ExtObject o = getObject(i); |
---|
1033 | Param tmp; |
---|
1034 | ParamInterface* oif = o.getParamInterface(tmp); |
---|
1035 | int ass; |
---|
1036 | if (oif && ((ass = oif->findId("assign")) >= 0)) |
---|
1037 | { |
---|
1038 | ExtValue arg = x; |
---|
1039 | oif->call(ass, &arg, &v); |
---|
1040 | } |
---|
1041 | else |
---|
1042 | logPrintf("SimpleAbstractParam", "setObject", LOG_ERROR, |
---|
1043 | "'%s.%s' is PARAM_OBJECTSET but no 'assign()' in %s", getName(), pe->id, o.interfaceName()); |
---|
1044 | return PSET_CHANGED; |
---|
1045 | } |
---|
1046 | ExtObject xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
1047 | if (pe->fun2) |
---|
1048 | { |
---|
1049 | v.setObject(x); |
---|
1050 | int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
1051 | messageOnExceedRange(i, result, xcopy); |
---|
1052 | return result; |
---|
1053 | } |
---|
1054 | else |
---|
1055 | { |
---|
1056 | void *target = getTarget(i); |
---|
1057 | *((ExtObject*)target) = x; |
---|
1058 | return PSET_CHANGED; |
---|
1059 | } |
---|
1060 | } |
---|
1061 | |
---|
1062 | int SimpleAbstractParam::setExtValue(int i, const ExtValue& x) |
---|
1063 | { |
---|
1064 | SANITY_CHECK(i); |
---|
1065 | ParamEntry *pe = entry(i); |
---|
1066 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
1067 | ExtValue xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
1068 | if (pe->fun2) |
---|
1069 | { |
---|
1070 | int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x); |
---|
1071 | messageOnExceedRange(i, result, xcopy); |
---|
1072 | return result; |
---|
1073 | } |
---|
1074 | else |
---|
1075 | { |
---|
1076 | void *target = getTarget(i); |
---|
1077 | *((ExtValue*)target) = x; |
---|
1078 | return PSET_CHANGED; |
---|
1079 | } |
---|
1080 | } |
---|
1081 | |
---|
1082 | void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret) |
---|
1083 | { |
---|
1084 | SANITY_CHECK(i); |
---|
1085 | ParamEntry *pe = entry(i); |
---|
1086 | if (!pe) return; |
---|
1087 | if (pe->fun1 && (pe->type[0] == 'p')) |
---|
1088 | (*(void(*)(void*, ExtValue*, ExtValue*))pe->fun1)(object, args, ret); |
---|
1089 | else |
---|
1090 | { |
---|
1091 | logPrintf("SimpleAbstractParam", "call", LOG_ERROR, |
---|
1092 | (*pe->type != 'p') ? "'%s.%s' is not a function" : "Internal error - undefined function pointer for '%s.%s'", getName(), pe->id); |
---|
1093 | ret->setInvalid(); |
---|
1094 | } |
---|
1095 | } |
---|
1096 | |
---|
1097 | void SimpleAbstractParam::setDefault() |
---|
1098 | { |
---|
1099 | bool save = dontcheckchanges; |
---|
1100 | dontcheckchanges = 1; |
---|
1101 | ParamInterface::setDefault(); |
---|
1102 | dontcheckchanges = save; |
---|
1103 | } |
---|
1104 | |
---|
1105 | void SimpleAbstractParam::setDefault(int i) |
---|
1106 | { |
---|
1107 | bool save = dontcheckchanges; |
---|
1108 | dontcheckchanges = 1; |
---|
1109 | ParamInterface::setDefault(i); |
---|
1110 | dontcheckchanges = save; |
---|
1111 | } |
---|
1112 | |
---|
1113 | // Returns the address of the beginning of the line. |
---|
1114 | // len = line length (without \n). |
---|
1115 | // 0 may mean the line with length=0 or the end of the SString. |
---|
1116 | // poz is advanced to the beginning of the next line. |
---|
1117 | // A typical loop: for(poz=0;poz<s.d;) {line=getline(s,poz,len);... |
---|
1118 | static const char *getline(const SString &s, int &poz, int &len) |
---|
1119 | { |
---|
1120 | const char *beg = s.c_str() + poz; |
---|
1121 | if (poz >= s.len()) { poz = s.len(); len = 0; return s.c_str() + s.len(); } |
---|
1122 | const char *lf = strchr(beg, '\n'); |
---|
1123 | if (!lf) { lf = s.c_str() + s.len() - 1; poz = s.len(); } |
---|
1124 | else { poz = (int)(lf - s.c_str()) + 1; if (poz > s.len()) poz = s.len(); } |
---|
1125 | while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break; |
---|
1126 | len = (int)(lf - beg) + 1; |
---|
1127 | return beg; |
---|
1128 | } |
---|
1129 | |
---|
1130 | int ParamInterface::load2(const SString &s, int &poz) |
---|
1131 | { |
---|
1132 | int i; // the index number of the parameter |
---|
1133 | int tmpi; |
---|
1134 | int len; |
---|
1135 | int ret; |
---|
1136 | int fields_loaded = 0; |
---|
1137 | const char *t, *lin, *end; |
---|
1138 | const char *equals_sign, *field_end, *next_field; |
---|
1139 | char remember; |
---|
1140 | const char *quote, *quote2; |
---|
1141 | const char *value, *valstop; |
---|
1142 | SString tmpvalue; |
---|
1143 | bool parse_failed = false; |
---|
1144 | if (poz >= s.len()) return fields_loaded; |
---|
1145 | t = s.c_str() + poz; |
---|
1146 | |
---|
1147 | lin = getline(s, poz, len); // all fields must be encoded in a single line |
---|
1148 | if (!len) return fields_loaded; // empty line = end |
---|
1149 | i = 0; |
---|
1150 | end = lin + len; |
---|
1151 | while (t < end) |
---|
1152 | { |
---|
1153 | // processing a single field |
---|
1154 | // "p:name=field_value, field_name=field_value , name=value..." |
---|
1155 | // ^ ^-t (after) ^ ^_next_field |
---|
1156 | // \_t (before) \_field_end |
---|
1157 | while (isspace(*t)) if (t < end) t++; else return fields_loaded; |
---|
1158 | |
---|
1159 | field_end = strchrlimit(t, ',', end); if (!field_end) field_end = end; |
---|
1160 | next_field = field_end; |
---|
1161 | while ((field_end>t) && isblank(field_end[-1])) field_end--; |
---|
1162 | quote = strchrlimit(t, '\"', field_end); |
---|
1163 | if (quote) |
---|
1164 | { |
---|
1165 | quote2 = skipQuoteString(quote + 1, end); |
---|
1166 | if (quote2 > field_end) |
---|
1167 | { |
---|
1168 | field_end = strchrlimit(quote2 + 1, ',', end); |
---|
1169 | if (!field_end) field_end = end; |
---|
1170 | next_field = field_end; |
---|
1171 | } |
---|
1172 | equals_sign = strchrlimit(t, '=', quote); |
---|
1173 | } |
---|
1174 | else |
---|
1175 | { |
---|
1176 | equals_sign = strchrlimit(t, '=', field_end); |
---|
1177 | quote2 = 0; |
---|
1178 | } |
---|
1179 | if (equals_sign == t) { t++; equals_sign = 0; } |
---|
1180 | if (field_end == t) // skip empty value |
---|
1181 | { |
---|
1182 | t++; i++; |
---|
1183 | continue; |
---|
1184 | } |
---|
1185 | if (equals_sign) // have parameter name |
---|
1186 | { |
---|
1187 | tmpi = findIdn(t, (int)(equals_sign - t)); |
---|
1188 | i = tmpi; |
---|
1189 | if (tmpi < 0) |
---|
1190 | { |
---|
1191 | SString name(t, (int)(equals_sign - t)); |
---|
1192 | logPrintf("Param", "load2", LOG_WARN, "Unknown property '%s' while reading object '%s' (ignored)", name.c_str(), getName()); |
---|
1193 | } |
---|
1194 | t = equals_sign + 1; // t=value |
---|
1195 | } |
---|
1196 | #ifdef WARN_MISSING_NAME |
---|
1197 | else |
---|
1198 | #ifdef SAVE_SELECTED_NAMES |
---|
1199 | if ((i >= getPropCount()) || !(flags(i)&PARAM_CANOMITNAME)) |
---|
1200 | #endif |
---|
1201 | { |
---|
1202 | if (id(i)) |
---|
1203 | logPrintf("Param", "load2", LOG_WARN, "Missing property name in '%s' (assuming '%s')", getName(), id(i)); |
---|
1204 | else |
---|
1205 | logPrintf("Param", "load2", LOG_WARN, "Value after the last property of '%s'", getName()); |
---|
1206 | } |
---|
1207 | #endif |
---|
1208 | if ((i >= 0) && id(i)) |
---|
1209 | { |
---|
1210 | value = t; |
---|
1211 | if (quote) |
---|
1212 | { |
---|
1213 | tmpvalue.copyFrom(quote + 1, (int)(quote2 - quote) - 1); |
---|
1214 | sstringUnquote(tmpvalue); |
---|
1215 | value = tmpvalue.c_str(); |
---|
1216 | valstop = quote2; |
---|
1217 | } |
---|
1218 | else |
---|
1219 | if (field_end < end) valstop = field_end; else valstop = end; |
---|
1220 | |
---|
1221 | remember = *valstop; |
---|
1222 | *(char*)valstop = 0; |
---|
1223 | ret = set(i, value, true); |
---|
1224 | fields_loaded++; |
---|
1225 | if (ret&(PSET_HITMAX | PSET_HITMIN)) |
---|
1226 | logPrintf("Param", "load2", LOG_WARN, "Adjusted '%s' in '%s' (was too %s)", |
---|
1227 | id(i), getName(), (ret&PSET_HITMAX) ? "big" : "small"); |
---|
1228 | if (ret&PSET_PARSEFAILED) |
---|
1229 | parse_failed = true; |
---|
1230 | *(char*)valstop = remember; |
---|
1231 | } |
---|
1232 | |
---|
1233 | if (i >= 0) i++; |
---|
1234 | #ifdef __CODEGUARD__ |
---|
1235 | if (next_field<end-1) t=next_field+1; else return fields_loaded; |
---|
1236 | #else |
---|
1237 | t = next_field + 1; |
---|
1238 | #endif |
---|
1239 | } |
---|
1240 | return fields_loaded | (parse_failed ? LOAD2_PARSE_FAILED : 0); |
---|
1241 | } |
---|
1242 | |
---|
1243 | int Param::grmember(int g, int a) |
---|
1244 | { |
---|
1245 | if ((getGroupCount() < 2) && (!g)) |
---|
1246 | return (a < getPropCount()) ? a : -9999; |
---|
1247 | |
---|
1248 | ParamEntry *e = entry(0); |
---|
1249 | int x = 0, i = 0; |
---|
1250 | for (; e->id; i++, e++) |
---|
1251 | { |
---|
1252 | if (e->group == g) |
---|
1253 | if (a == x) return i; else x++; |
---|
1254 | } |
---|
1255 | return -9999; |
---|
1256 | } |
---|