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