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