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