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 | int ParamInterface::loadMultiLine(VirtFILE* f, LoadOptions &options) |
---|
557 | { |
---|
558 | SString buf; |
---|
559 | int i; |
---|
560 | const char *p, *p0; |
---|
561 | int p_len; |
---|
562 | bool loaded; |
---|
563 | int fields_loaded = 0; |
---|
564 | int unexpected_line = 0; |
---|
565 | vector<bool> seen; |
---|
566 | seen.resize(getPropCount()); |
---|
567 | if ((i = findId("beforeLoad")) >= 0) |
---|
568 | call(i, NULL, NULL); |
---|
569 | while (((!options.abortable) || (!*options.abortable)) && loadSStringLine(f, buf)) |
---|
570 | { |
---|
571 | if (options.linenum) (*options.linenum)++; |
---|
572 | const char* t = buf.c_str(); |
---|
573 | p0 = t; while (isblank(*p0)) p0++; |
---|
574 | if (!*p0) break; |
---|
575 | if (p0[0] == '#') { unexpected_line = 0; continue; } |
---|
576 | p = strchr(p0, ':'); |
---|
577 | if (!p) |
---|
578 | { |
---|
579 | switch (unexpected_line) |
---|
580 | { |
---|
581 | case 0: |
---|
582 | logPrintf("ParamInterface", "load", LOG_WARN, "Ignored unexpected line %s while reading object '%s'", |
---|
583 | options.linenum ? |
---|
584 | SString::sprintf("%d", *options.linenum).c_str() |
---|
585 | : SString::sprintf("'%s'", p0).c_str(), |
---|
586 | getName()); |
---|
587 | break; |
---|
588 | case 1: |
---|
589 | logPrintf("ParamInterface", "load", LOG_WARN, "The following line(s) were also unexpected and were ignored"); |
---|
590 | break; |
---|
591 | } |
---|
592 | unexpected_line++; |
---|
593 | continue; |
---|
594 | } |
---|
595 | unexpected_line = 0; |
---|
596 | p_len = (int)(p - p0); |
---|
597 | loaded = false; |
---|
598 | if (p_len && ((i = findIdn(p0, p_len)) >= 0)) |
---|
599 | { |
---|
600 | if (seen[i]) |
---|
601 | { |
---|
602 | SString fileinfo; |
---|
603 | const char* fname = f->VgetPath(); |
---|
604 | if (fname != NULL) |
---|
605 | { |
---|
606 | fileinfo = SString::sprintf(" while reading from '%s'", fname); |
---|
607 | if (options.linenum) |
---|
608 | fileinfo += SString::sprintf(" (line %d)", *options.linenum); |
---|
609 | } |
---|
610 | logPrintf("ParamInterface", "load", LOG_WARN, "Multiple '%s.%s' properties found%s", getName(), id(i), fileinfo.c_str()); |
---|
611 | } |
---|
612 | else |
---|
613 | seen[i] = true; |
---|
614 | if (!(flags(i) & PARAM_DONTLOAD)) |
---|
615 | { |
---|
616 | if (p0[p_len + 1] == '~') |
---|
617 | { |
---|
618 | SString s; |
---|
619 | if (!readUntilTilde(f, s)) |
---|
620 | closingTildeError(this, f, i); |
---|
621 | int lfcount = 1; |
---|
622 | const char* tmp = s.c_str(); |
---|
623 | while (tmp) |
---|
624 | if ((tmp = strchr(tmp, '\n'))) |
---|
625 | { |
---|
626 | lfcount++; tmp++; |
---|
627 | } |
---|
628 | removeCR(s); |
---|
629 | int ch; while ((ch = f->Vgetc()) != EOF) if (ch == '\n') break; |
---|
630 | unquoteTilde(s); |
---|
631 | if (options.linenum && (flags(i) & PARAM_LINECOMMENT)) |
---|
632 | s = SString::sprintf("@file %s\n@line %d\n", f->VgetPath(), *options.linenum + 1) + s; |
---|
633 | setFromString(i, s.c_str(), false); |
---|
634 | if (options.linenum) |
---|
635 | (*options.linenum) += lfcount; |
---|
636 | } |
---|
637 | else |
---|
638 | { |
---|
639 | SString s = SString(p0 + p_len + 1); |
---|
640 | unquoteTilde(s); |
---|
641 | setFromString(i, s.c_str(), false); |
---|
642 | } |
---|
643 | fields_loaded++; |
---|
644 | loaded = true; |
---|
645 | } |
---|
646 | } |
---|
647 | else if (options.warn_unknown_fields) |
---|
648 | { |
---|
649 | SString name(p0, p_len); |
---|
650 | logPrintf("ParamInterface", "load", LOG_WARN, "Ignored unknown property '%s.%s'", getName(), name.c_str()); |
---|
651 | } |
---|
652 | |
---|
653 | if ((!loaded) && (p0[p_len + 1] == '~')) |
---|
654 | { // eat unrecognized multiline field |
---|
655 | SString s; |
---|
656 | if (!readUntilTilde(f, s)) |
---|
657 | closingTildeError(this, f, -1); |
---|
658 | if (options.linenum) |
---|
659 | { |
---|
660 | const char* tmp = s.c_str(); |
---|
661 | int lfcount = 1; |
---|
662 | while (tmp) |
---|
663 | if ((tmp = strchr(tmp, '\n'))) |
---|
664 | { |
---|
665 | lfcount++; tmp++; |
---|
666 | } |
---|
667 | (*options.linenum) += lfcount; |
---|
668 | } |
---|
669 | int ch; while ((ch = f->Vgetc()) != EOF) if (ch == '\n') break; |
---|
670 | } |
---|
671 | } |
---|
672 | if ((i = findId("afterLoad")) >= 0) |
---|
673 | call(i, NULL, NULL); |
---|
674 | return fields_loaded; |
---|
675 | } |
---|
676 | |
---|
677 | |
---|
678 | /* |
---|
679 | SString SimpleAbstractParam::getString(int i) |
---|
680 | { |
---|
681 | char *t; |
---|
682 | switch (*(t=type(i))) |
---|
683 | { |
---|
684 | case 'd': |
---|
685 | { |
---|
686 | for (i=atol(get(i));i>=0;i--) if (t) t=strchr(t+1,'~'); |
---|
687 | if (t) |
---|
688 | { |
---|
689 | t++; |
---|
690 | char *t2=strchr(t,'~'); |
---|
691 | if (!t2) t2=t+strlen(t); |
---|
692 | SString str; |
---|
693 | strncpy(str.directWrite(t2-t),t,t2-t); |
---|
694 | str.endWrite(t2-t); |
---|
695 | return str; |
---|
696 | } |
---|
697 | } |
---|
698 | } |
---|
699 | return get(i); |
---|
700 | } |
---|
701 | */ |
---|
702 | |
---|
703 | int ParamInterface::findId(const char* n) |
---|
704 | { |
---|
705 | int i; const char *p; |
---|
706 | for (i = 0; p = id(i); i++) if (!strcmp(n, p)) return i; |
---|
707 | return -1; |
---|
708 | } |
---|
709 | |
---|
710 | int ParamInterface::findIdn(const char* naz, int n) |
---|
711 | { |
---|
712 | int i; const char *p; |
---|
713 | for (i = 0; p = id(i); i++) if ((!strncmp(naz, p, n)) && (!p[n])) return i; |
---|
714 | return -1; |
---|
715 | } |
---|
716 | |
---|
717 | int ParamInterface::findGroupId(const char* name) |
---|
718 | { |
---|
719 | for (int i = 0; i < getGroupCount(); i++) |
---|
720 | if (!strcmp(grname(i), name)) |
---|
721 | return i; |
---|
722 | return -1; |
---|
723 | } |
---|
724 | |
---|
725 | void ParamInterface::get(int i, ExtValue &ret) |
---|
726 | { |
---|
727 | switch (type(i)[0]) |
---|
728 | { |
---|
729 | case 'd': ret.setInt(getInt(i)); break; |
---|
730 | case 'f': ret.setDouble(getDouble(i)); break; |
---|
731 | case 's': ret.setString(getString(i)); break; |
---|
732 | case 'o': ret.setObject(getObject(i)); break; |
---|
733 | case 'x': ret = getExtValue(i); break; |
---|
734 | default: logPrintf("ParamInterface", "get", LOG_ERROR, "%s is not a property", nameDotPropertyForMessages(i).c_str()); |
---|
735 | } |
---|
736 | } |
---|
737 | |
---|
738 | int ParamInterface::setIntFromString(int i, const char* str, bool strict) |
---|
739 | { |
---|
740 | paInt value; |
---|
741 | if (!ExtValue::parseInt(str, value, strict, true)) |
---|
742 | { |
---|
743 | paInt mn, mx, def; |
---|
744 | if (getMinMaxInt(i, mn, mx, def) >= 3) |
---|
745 | return setInt(i, def) | PSET_PARSEFAILED; |
---|
746 | else |
---|
747 | return setInt(i, (paInt)0) | PSET_PARSEFAILED; |
---|
748 | } |
---|
749 | else |
---|
750 | return setInt(i, value); |
---|
751 | } |
---|
752 | |
---|
753 | int ParamInterface::setDoubleFromString(int i, const char* str) |
---|
754 | { |
---|
755 | double value; |
---|
756 | if (!ExtValue::parseDouble(str, value, true)) |
---|
757 | { |
---|
758 | double mn, mx, def; |
---|
759 | if (getMinMaxDouble(i, mn, mx, def) >= 3) |
---|
760 | return setDouble(i, def) | PSET_PARSEFAILED; |
---|
761 | else |
---|
762 | return setDouble(i, (double)0) | PSET_PARSEFAILED; |
---|
763 | } |
---|
764 | else |
---|
765 | return setDouble(i, value); |
---|
766 | } |
---|
767 | |
---|
768 | int ParamInterface::set(int i, const ExtValue &v) |
---|
769 | { |
---|
770 | switch (type(i)[0]) |
---|
771 | { |
---|
772 | case 'd': |
---|
773 | if ((v.type == TInt) || (v.type == TDouble)) return setInt(i, v.getInt()); |
---|
774 | else |
---|
775 | { |
---|
776 | if (v.type == TObj) |
---|
777 | { |
---|
778 | logPrintf("ParamInterface", "set", LOG_ERROR, "Setting int %s from object reference (%s)", nameDotPropertyForMessages(i).c_str(), v.getString().c_str()); |
---|
779 | return 0; |
---|
780 | } |
---|
781 | else |
---|
782 | return setIntFromString(i, v.getString().c_str(), false); |
---|
783 | } |
---|
784 | case 'f': |
---|
785 | if ((v.type == TInt) || (v.type == TDouble)) return setDouble(i, v.getDouble()); |
---|
786 | else |
---|
787 | { |
---|
788 | if (v.type == TObj) |
---|
789 | { |
---|
790 | logPrintf("ParamInterface", "set", LOG_ERROR, "Setting float %s from object reference (%s)", nameDotPropertyForMessages(i).c_str(), v.getString().c_str()); |
---|
791 | return 0; |
---|
792 | } |
---|
793 | else |
---|
794 | return setDoubleFromString(i, v.getString().c_str()); |
---|
795 | } |
---|
796 | case 's': { SString t = v.getString(); return setString(i, t); } |
---|
797 | case 'o': |
---|
798 | if ((v.type != TUnknown) && (v.type != TObj)) |
---|
799 | logPrintf("ParamInterface", "set", LOG_ERROR, "Setting object %s from %s", nameDotPropertyForMessages(i).c_str(), v.typeAndValue().c_str()); |
---|
800 | else |
---|
801 | return setObject(i, v.getObject()); |
---|
802 | break; |
---|
803 | case 'x': return setExtValue(i, v); |
---|
804 | default: logPrintf("ParamInterface", "set", LOG_ERROR, "%s is not a property", nameDotPropertyForMessages(i).c_str()); |
---|
805 | } |
---|
806 | return 0; |
---|
807 | } |
---|
808 | |
---|
809 | int ParamInterface::setFromString(int i, const char *v, bool strict) |
---|
810 | { |
---|
811 | char typ = type(i)[0]; |
---|
812 | switch (typ) |
---|
813 | { |
---|
814 | case 'd': return setIntFromString(i, v, strict); |
---|
815 | case 'f': return setDoubleFromString(i, v); |
---|
816 | case 's': { SString t(v); return setString(i, t); } |
---|
817 | case 'x': case 'o': |
---|
818 | { |
---|
819 | ExtValue e; |
---|
820 | const char* after; |
---|
821 | if (!strncmp(v, SERIALIZATION_PREFIX, strlen(SERIALIZATION_PREFIX))) |
---|
822 | { |
---|
823 | after = e.deserialize(v + strlen(SERIALIZATION_PREFIX)); |
---|
824 | if ((after == NULL) || (*after)) |
---|
825 | { |
---|
826 | logPrintf("ParamInterface", "set", LOG_ERROR, "serialization format mismatch in %s.%s", (getName() ? getName() : "<Unknown>"), id(i)); |
---|
827 | e.setEmpty(); |
---|
828 | } |
---|
829 | } |
---|
830 | else if ((after = e.parseNumber(v)) && (*after == 0)) //consumed the whole string |
---|
831 | { |
---|
832 | //OK! |
---|
833 | } |
---|
834 | else |
---|
835 | { |
---|
836 | e.setString(SString(v)); |
---|
837 | } |
---|
838 | if (typ == 'x') |
---|
839 | return setExtValue(i, e); |
---|
840 | else |
---|
841 | return setObject(i, e.getObject()); |
---|
842 | } |
---|
843 | } |
---|
844 | return 0; |
---|
845 | } |
---|
846 | |
---|
847 | SString ParamInterface::getText(int i) //find the current enum text or call get(i) if not enum |
---|
848 | { |
---|
849 | const char *t; |
---|
850 | if (((*(t = type(i))) == 'd') && (strchr(t, '~') != NULL)) //type is int and contains enum labels |
---|
851 | { |
---|
852 | paInt mn, mx, def; |
---|
853 | int value = getInt(i); |
---|
854 | if (getMinMaxIntFromTypeDef(t, mn, mx, def) >= 2) |
---|
855 | { |
---|
856 | if (value > mx) |
---|
857 | return get(i);//unexpected value of out bounds (should never happen) -> fallback |
---|
858 | value -= mn; |
---|
859 | } |
---|
860 | if (value < 0) return get(i); //unexpected value of out bounds (should never happen) -> fallback |
---|
861 | // now value is 0-based index of ~text |
---|
862 | for (; value >= 0; value--) if (t) t = strchr(t + 1, '~'); else break; |
---|
863 | if (t) // found n-th ~text in type description (else: not enough ~texts in type description) |
---|
864 | { |
---|
865 | t++; |
---|
866 | const char *t2 = strchr(t, '~'); |
---|
867 | if (!t2) t2 = t + strlen(t); |
---|
868 | return SString(t, (int)(t2 - t)); |
---|
869 | } |
---|
870 | } |
---|
871 | return get(i); //fallback - return int value as string |
---|
872 | } |
---|
873 | |
---|
874 | SString ParamInterface::get(int i) |
---|
875 | { |
---|
876 | switch (type(i)[0]) |
---|
877 | { |
---|
878 | case 'd': return SString::valueOf(getInt(i)); |
---|
879 | case 'f': return SString::valueOf(getDouble(i)); |
---|
880 | case 's': return getString(i); |
---|
881 | } |
---|
882 | ExtValue v; |
---|
883 | get(i, v); |
---|
884 | return v.getString(); |
---|
885 | } |
---|
886 | |
---|
887 | bool ParamInterface::isValidTypeDescription(const char* t) |
---|
888 | { |
---|
889 | if (t == NULL) return false; |
---|
890 | if (*t == 0) return false; |
---|
891 | if (strchr("dfsoxp", *t) == NULL) return false; |
---|
892 | switch (*t) |
---|
893 | { |
---|
894 | case 'd': |
---|
895 | { |
---|
896 | paInt a, b, c; |
---|
897 | int have = getMinMaxIntFromTypeDef(t, a, b, c); |
---|
898 | if (have == 1) return false; |
---|
899 | if ((have >= 2) && (b < a) && (a != 0) && (b != -1)) return false; // max<min meaning 'undefined' is only allowed as "d 0 -1" |
---|
900 | } |
---|
901 | break; |
---|
902 | case 'f': |
---|
903 | { |
---|
904 | double a, b, c; |
---|
905 | int have = getMinMaxDoubleFromTypeDef(t, a, b, c); |
---|
906 | if (have == 1) return false; |
---|
907 | if ((have >= 2) && (b < a) && (a != 0) && (b != -1)) return false; // max<min meaning 'undefined' is only allowed as "f 0 -1" |
---|
908 | } |
---|
909 | break; |
---|
910 | case 's': |
---|
911 | { |
---|
912 | int a, b; SString c; |
---|
913 | int have = getMinMaxStringFromTypeDef(t, a, b, c); |
---|
914 | //if (have == 1) return false; //not sure? |
---|
915 | if ((have >= 1) && (!((a == 0) || (a == 1)))) return false; // 'min' for string (single/multi) can be only 0 or 1 |
---|
916 | if ((have >= 2) && (b < -1)) return false; // max=-1 means unlimited, >=0 is max len, max<-1 is not allowed |
---|
917 | } |
---|
918 | break; |
---|
919 | } |
---|
920 | return true; |
---|
921 | } |
---|
922 | |
---|
923 | SString ParamInterface::friendlyTypeDescrFromTypeDef(const char* type) |
---|
924 | { |
---|
925 | SString t; |
---|
926 | switch (type[0]) |
---|
927 | { |
---|
928 | case 'd': t += "integer"; |
---|
929 | {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); } |
---|
930 | break; |
---|
931 | case 'f': t += "float"; |
---|
932 | {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); } |
---|
933 | break; |
---|
934 | case 's': t += "string"; |
---|
935 | {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()); } |
---|
936 | break; |
---|
937 | case 'x': t += "untyped value"; break; |
---|
938 | case 'p': t += "function"; break; |
---|
939 | case 'o': t += "object"; if (type[1]) { t += " of class "; t += type + 1; } break; |
---|
940 | default: return "unknown type"; |
---|
941 | } |
---|
942 | return t; |
---|
943 | } |
---|
944 | |
---|
945 | //////////////////////////////// PARAM //////////////////////////////////// |
---|
946 | |
---|
947 | #ifdef _DEBUG |
---|
948 | void SimpleAbstractParam::sanityCheck(int i) |
---|
949 | { |
---|
950 | ParamEntry *pe = entry(i); |
---|
951 | |
---|
952 | const char* t = pe->type; |
---|
953 | const char* err = NULL; |
---|
954 | |
---|
955 | if (!isValidTypeDescription(t)) |
---|
956 | err = "invalid type description"; |
---|
957 | if (*t == 'p') |
---|
958 | { |
---|
959 | if (pe->fun1 == NULL) |
---|
960 | { |
---|
961 | MutableParamInterface *mpi = dynamic_cast<MutableParamInterface*>(this); |
---|
962 | 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. |
---|
963 | err = "no procedure defined"; |
---|
964 | } |
---|
965 | } |
---|
966 | else |
---|
967 | { |
---|
968 | if ((t[0] == 'o') && (t[1] == ' ')) |
---|
969 | { |
---|
970 | err = "space after 'o'"; |
---|
971 | } |
---|
972 | if (!(pe->flags & (PARAM_READONLY | PARAM_DONTSAVE | PARAM_USERREADONLY | PARAM_CONST | PARAM_DONTLOAD | PARAM_LINECOMMENT | PARAM_OBJECTSET))) |
---|
973 | { //write access |
---|
974 | if ((pe->fun2 == NULL) && (pe->offset == PARAM_ILLEGAL_OFFSET)) |
---|
975 | err = "no field defined (GETONLY without PARAM_READONLY?)"; |
---|
976 | } |
---|
977 | } |
---|
978 | if (err != NULL) |
---|
979 | logPrintf("SimpleAbstractParam", "sanityCheck", LOG_ERROR, |
---|
980 | "Invalid ParamEntry for %s (%s)", nameDotPropertyForMessages(i).c_str(), err); |
---|
981 | } |
---|
982 | #endif |
---|
983 | |
---|
984 | void *SimpleAbstractParam::getTarget(int i) |
---|
985 | { |
---|
986 | return (void*)(((char*)object) + entry(i)->offset); |
---|
987 | //return &(object->*(entry(i)->fldptr)); |
---|
988 | } |
---|
989 | |
---|
990 | ///////// get |
---|
991 | |
---|
992 | #ifdef _DEBUG |
---|
993 | #define SANITY_CHECK(i) sanityCheck(i) |
---|
994 | #else |
---|
995 | #define SANITY_CHECK(i) |
---|
996 | #endif |
---|
997 | |
---|
998 | paInt SimpleAbstractParam::getInt(int i) |
---|
999 | { |
---|
1000 | SANITY_CHECK(i); |
---|
1001 | ExtValue v; |
---|
1002 | ParamEntry *pe = entry(i); |
---|
1003 | if (pe->fun1) |
---|
1004 | { |
---|
1005 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
1006 | return v.getInt(); |
---|
1007 | } |
---|
1008 | else |
---|
1009 | { |
---|
1010 | void *target = getTarget(i); |
---|
1011 | return *((paInt*)target); |
---|
1012 | } |
---|
1013 | } |
---|
1014 | |
---|
1015 | double SimpleAbstractParam::getDouble(int i) |
---|
1016 | { |
---|
1017 | SANITY_CHECK(i); |
---|
1018 | ExtValue v; |
---|
1019 | ParamEntry *pe = entry(i); |
---|
1020 | if (pe->fun1) |
---|
1021 | { |
---|
1022 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
1023 | return v.getDouble(); |
---|
1024 | } |
---|
1025 | else |
---|
1026 | { |
---|
1027 | void *target = getTarget(i); |
---|
1028 | return *((double*)target); |
---|
1029 | } |
---|
1030 | } |
---|
1031 | |
---|
1032 | SString SimpleAbstractParam::getString(int i) |
---|
1033 | { |
---|
1034 | SANITY_CHECK(i); |
---|
1035 | ExtValue v; |
---|
1036 | ParamEntry *pe = entry(i); |
---|
1037 | if (pe->fun1) |
---|
1038 | { |
---|
1039 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
1040 | return v.getString(); |
---|
1041 | } |
---|
1042 | else |
---|
1043 | { |
---|
1044 | void *target = getTarget(i); |
---|
1045 | return *((SString*)target); |
---|
1046 | } |
---|
1047 | } |
---|
1048 | |
---|
1049 | ExtObject SimpleAbstractParam::getObject(int i) |
---|
1050 | { |
---|
1051 | SANITY_CHECK(i); |
---|
1052 | ExtValue v; |
---|
1053 | ParamEntry *pe = entry(i); |
---|
1054 | if (pe->fun1) |
---|
1055 | { |
---|
1056 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
1057 | return v.getObject(); |
---|
1058 | } |
---|
1059 | else |
---|
1060 | { |
---|
1061 | void *target = getTarget(i); |
---|
1062 | return *((ExtObject*)target); |
---|
1063 | } |
---|
1064 | } |
---|
1065 | |
---|
1066 | ExtValue SimpleAbstractParam::getExtValue(int i) |
---|
1067 | { |
---|
1068 | SANITY_CHECK(i); |
---|
1069 | ExtValue v; |
---|
1070 | ParamEntry *pe = entry(i); |
---|
1071 | if (pe->fun1) |
---|
1072 | { |
---|
1073 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
1074 | return v; |
---|
1075 | } |
---|
1076 | else |
---|
1077 | { |
---|
1078 | void *target = getTarget(i); |
---|
1079 | return *((ExtValue*)target); |
---|
1080 | } |
---|
1081 | } |
---|
1082 | |
---|
1083 | |
---|
1084 | //////// set |
---|
1085 | |
---|
1086 | int SimpleAbstractParam::setInt(int i, paInt x) |
---|
1087 | { |
---|
1088 | SANITY_CHECK(i); |
---|
1089 | ExtValue v; |
---|
1090 | ParamEntry *pe = entry(i); |
---|
1091 | if (pe->flags & PARAM_READONLY) return PSET_RONLY; |
---|
1092 | paInt xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
1093 | paInt mn = 0, mx = 0, de = 0; |
---|
1094 | int result = 0; |
---|
1095 | if (getMinMaxIntFromTypeDef(pe->type, mn, mx, de) >= 2) |
---|
1096 | if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking |
---|
1097 | { |
---|
1098 | if (x < mn) { x = mn; result = PSET_HITMIN; } |
---|
1099 | else if (x > mx) { x = mx; result = PSET_HITMAX; } |
---|
1100 | } |
---|
1101 | |
---|
1102 | if (pe->fun2) |
---|
1103 | { |
---|
1104 | v.setInt(x); |
---|
1105 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
1106 | } |
---|
1107 | else |
---|
1108 | { |
---|
1109 | void *target = getTarget(i); |
---|
1110 | if (dontcheckchanges || (*((paInt*)target) != x)) |
---|
1111 | { |
---|
1112 | result |= PSET_CHANGED; |
---|
1113 | *((paInt*)target) = x; |
---|
1114 | } |
---|
1115 | } |
---|
1116 | ::messageOnExceedRange(this, i, result, xcopy); |
---|
1117 | return result; |
---|
1118 | } |
---|
1119 | |
---|
1120 | int SimpleAbstractParam::setDouble(int i, double x) |
---|
1121 | { |
---|
1122 | SANITY_CHECK(i); |
---|
1123 | ExtValue v; |
---|
1124 | ParamEntry *pe = entry(i); |
---|
1125 | if (pe->flags & PARAM_READONLY) return PSET_RONLY; |
---|
1126 | double xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
1127 | double mn = 0, mx = 0, de = 0; |
---|
1128 | int result = 0; |
---|
1129 | if (getMinMaxDoubleFromTypeDef(pe->type, mn, mx, de) >= 2) |
---|
1130 | if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking |
---|
1131 | { |
---|
1132 | if (x < mn) { x = mn; result = PSET_HITMIN; } |
---|
1133 | else if (x > mx) { x = mx; result = PSET_HITMAX; } |
---|
1134 | 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. |
---|
1135 | } |
---|
1136 | |
---|
1137 | if (pe->fun2) |
---|
1138 | { |
---|
1139 | v.setDouble(x); |
---|
1140 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
1141 | } |
---|
1142 | else |
---|
1143 | { |
---|
1144 | void *target = getTarget(i); |
---|
1145 | if (dontcheckchanges || (*((double*)target) != x)) |
---|
1146 | { |
---|
1147 | result |= PSET_CHANGED; |
---|
1148 | *((double*)target) = x; |
---|
1149 | } |
---|
1150 | } |
---|
1151 | ::messageOnExceedRange(this, i, result, xcopy); |
---|
1152 | return result; |
---|
1153 | } |
---|
1154 | |
---|
1155 | int SimpleAbstractParam::setString(int i, const SString& x) |
---|
1156 | { |
---|
1157 | SANITY_CHECK(i); |
---|
1158 | ExtValue v; |
---|
1159 | SString vs; |
---|
1160 | const SString *xx = &x; |
---|
1161 | ParamEntry *pe = entry(i); |
---|
1162 | if (pe->flags & PARAM_READONLY) return PSET_RONLY; |
---|
1163 | SString xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
1164 | const char* t = pe->type + 1; |
---|
1165 | while (*t) if (*t == ' ') break; else t++; |
---|
1166 | int mn = 0, mx = 0; |
---|
1167 | int result = 0; |
---|
1168 | if (sscanf(t, "%d %d", &mn, &mx) == 2) //using getMinMax would also get default value, which is not needed here |
---|
1169 | { |
---|
1170 | if ((x.length() > mx) && (mx >= 0)) |
---|
1171 | { |
---|
1172 | vs = x.substr(0, mx); |
---|
1173 | xx = &vs; |
---|
1174 | result |= PSET_HITMAX; |
---|
1175 | } |
---|
1176 | } |
---|
1177 | |
---|
1178 | if (pe->fun2) |
---|
1179 | { |
---|
1180 | v.setString(*xx); |
---|
1181 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
1182 | } |
---|
1183 | else |
---|
1184 | { |
---|
1185 | void *target = getTarget(i); |
---|
1186 | if (dontcheckchanges || (!(*((SString*)target) == *xx))) |
---|
1187 | { |
---|
1188 | result |= PSET_CHANGED; |
---|
1189 | *((SString*)target) = *xx; |
---|
1190 | } |
---|
1191 | } |
---|
1192 | ::messageOnExceedRange(this, i, result, xcopy); |
---|
1193 | return result; |
---|
1194 | } |
---|
1195 | |
---|
1196 | int SimpleAbstractParam::setObject(int i, const ExtObject& x) |
---|
1197 | { |
---|
1198 | SANITY_CHECK(i); |
---|
1199 | ExtValue v; |
---|
1200 | ParamEntry *pe = entry(i); |
---|
1201 | if (pe->flags & PARAM_READONLY) return PSET_RONLY; |
---|
1202 | if (pe->flags & PARAM_OBJECTSET) |
---|
1203 | { |
---|
1204 | ExtObject o = getObject(i); |
---|
1205 | Param tmp; |
---|
1206 | ParamInterface* oif = o.getParamInterface(tmp); |
---|
1207 | int ass; |
---|
1208 | if (oif && ((ass = oif->findId("assign")) >= 0)) |
---|
1209 | { |
---|
1210 | ExtValue arg = x; |
---|
1211 | oif->call(ass, &arg, &v); |
---|
1212 | } |
---|
1213 | else |
---|
1214 | logPrintf("SimpleAbstractParam", "setObject", LOG_ERROR, |
---|
1215 | "%s is PARAM_OBJECTSET but no 'assign()' in %s", nameDotPropertyForMessages(i).c_str(), o.interfaceName()); |
---|
1216 | return PSET_CHANGED; |
---|
1217 | } |
---|
1218 | ExtObject xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
1219 | if (pe->fun2) |
---|
1220 | { |
---|
1221 | v.setObject(x); |
---|
1222 | int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
1223 | ::messageOnExceedRange(this, i, result, xcopy); |
---|
1224 | return result; |
---|
1225 | } |
---|
1226 | else |
---|
1227 | { |
---|
1228 | void *target = getTarget(i); |
---|
1229 | *((ExtObject*)target) = x; |
---|
1230 | return PSET_CHANGED; |
---|
1231 | } |
---|
1232 | } |
---|
1233 | |
---|
1234 | int SimpleAbstractParam::setExtValue(int i, const ExtValue& x) |
---|
1235 | { |
---|
1236 | SANITY_CHECK(i); |
---|
1237 | ParamEntry *pe = entry(i); |
---|
1238 | if (pe->flags & PARAM_READONLY) return PSET_RONLY; |
---|
1239 | ExtValue xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
1240 | if (pe->fun2) |
---|
1241 | { |
---|
1242 | int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x); |
---|
1243 | ::messageOnExceedRange(this, i, result, xcopy); |
---|
1244 | return result; |
---|
1245 | } |
---|
1246 | else |
---|
1247 | { |
---|
1248 | void *target = getTarget(i); |
---|
1249 | *((ExtValue*)target) = x; |
---|
1250 | return PSET_CHANGED; |
---|
1251 | } |
---|
1252 | } |
---|
1253 | |
---|
1254 | void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret) |
---|
1255 | { |
---|
1256 | SANITY_CHECK(i); |
---|
1257 | ParamEntry *pe = entry(i); |
---|
1258 | if (!pe) return; |
---|
1259 | if (pe->fun1 && (pe->type[0] == 'p')) |
---|
1260 | (*(void(*)(void*, ExtValue*, ExtValue*))pe->fun1)(object, args, ret); |
---|
1261 | else |
---|
1262 | { |
---|
1263 | logPrintf("SimpleAbstractParam", "call", LOG_ERROR, |
---|
1264 | (*pe->type != 'p') ? "%s is not a function" : "Internal error - undefined function pointer for %s", nameDotPropertyForMessages(i).c_str()); |
---|
1265 | ret->setInvalid(); |
---|
1266 | } |
---|
1267 | } |
---|
1268 | |
---|
1269 | void SimpleAbstractParam::setDefault() |
---|
1270 | { |
---|
1271 | bool save = dontcheckchanges; |
---|
1272 | dontcheckchanges = 1; |
---|
1273 | ParamInterface::setDefault(); |
---|
1274 | dontcheckchanges = save; |
---|
1275 | } |
---|
1276 | |
---|
1277 | void SimpleAbstractParam::setDefault(int i) |
---|
1278 | { |
---|
1279 | bool save = dontcheckchanges; |
---|
1280 | dontcheckchanges = 1; |
---|
1281 | ParamInterface::setDefault(i); |
---|
1282 | dontcheckchanges = save; |
---|
1283 | } |
---|
1284 | |
---|
1285 | // Returns the address of the beginning of the line. |
---|
1286 | // len = line length (without \n). |
---|
1287 | // 0 may mean the line with length=0 or the end of the SString. |
---|
1288 | // poz is advanced to the beginning of the next line. |
---|
1289 | // A typical loop: for(poz=0;poz<s.d;) {line=getline(s,poz,len);... |
---|
1290 | static const char *getline(const SString &s, int &poz, int &len) |
---|
1291 | { |
---|
1292 | const char *beg = s.c_str() + poz; |
---|
1293 | if (poz >= s.length()) { poz = s.length(); len = 0; return s.c_str() + s.length(); } |
---|
1294 | const char *lf = strchr(beg, '\n'); |
---|
1295 | if (!lf) { lf = s.c_str() + s.length() - 1; poz = s.length(); } |
---|
1296 | else { poz = (int)(lf - s.c_str()) + 1; if (poz > s.length()) poz = s.length(); } |
---|
1297 | while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break; |
---|
1298 | len = (int)(lf - beg) + 1; |
---|
1299 | return beg; |
---|
1300 | } |
---|
1301 | |
---|
1302 | int ParamInterface::loadSingleLine(const SString &s, LoadOptions &options) |
---|
1303 | { |
---|
1304 | int i; // the index number of the parameter |
---|
1305 | int tmpi; |
---|
1306 | int len; |
---|
1307 | int ret; |
---|
1308 | int fields_loaded = 0; |
---|
1309 | const char *t, *lin, *end; |
---|
1310 | const char *equals_sign, *field_end, *next_field; |
---|
1311 | char remember; |
---|
1312 | const char *quote, *quote2; |
---|
1313 | const char *value, *valstop; |
---|
1314 | SString tmpvalue; |
---|
1315 | bool parse_failed = false; |
---|
1316 | if (options.offset >= s.length()) return fields_loaded; |
---|
1317 | t = s.c_str() + options.offset; |
---|
1318 | |
---|
1319 | lin = getline(s, options.offset, len); // all fields must be encoded in a single line |
---|
1320 | if (!len) return fields_loaded; // empty line = end |
---|
1321 | i = 0; |
---|
1322 | end = lin + len; |
---|
1323 | while (t < end) |
---|
1324 | { |
---|
1325 | // processing a single field |
---|
1326 | // "p:name=field_value, field_name=field_value , name=value..." |
---|
1327 | // ^ ^-t (after) ^ ^_next_field |
---|
1328 | // \_t (before) \_field_end |
---|
1329 | while (isspace(*t)) if (t < end) t++; else return fields_loaded; |
---|
1330 | |
---|
1331 | field_end = strchrlimit(t, ',', end); if (!field_end) field_end = end; |
---|
1332 | next_field = field_end; |
---|
1333 | while ((field_end > t) && isblank(field_end[-1])) field_end--; |
---|
1334 | quote = strchrlimit(t, '\"', field_end); |
---|
1335 | if (quote) |
---|
1336 | { |
---|
1337 | quote2 = skipQuoteString(quote + 1, end); |
---|
1338 | if (quote2 > field_end) |
---|
1339 | { |
---|
1340 | field_end = strchrlimit(quote2 + 1, ',', end); |
---|
1341 | if (!field_end) field_end = end; |
---|
1342 | next_field = field_end; |
---|
1343 | } |
---|
1344 | equals_sign = strchrlimit(t, '=', quote); |
---|
1345 | } |
---|
1346 | else |
---|
1347 | { |
---|
1348 | equals_sign = strchrlimit(t, '=', field_end); |
---|
1349 | quote2 = 0; |
---|
1350 | } |
---|
1351 | if (equals_sign == t) { t++; equals_sign = 0; } |
---|
1352 | if (field_end == t) // skip empty value |
---|
1353 | { |
---|
1354 | t++; if (i >= 0) i++; |
---|
1355 | continue; |
---|
1356 | } |
---|
1357 | if (equals_sign) // have parameter name |
---|
1358 | { |
---|
1359 | tmpi = findIdn(t, (int)(equals_sign - t)); |
---|
1360 | i = tmpi; |
---|
1361 | if (tmpi < 0) |
---|
1362 | { |
---|
1363 | SString name(t, (int)(equals_sign - t)); |
---|
1364 | logPrintf("Param", "loadSingleLine", LOG_WARN, "Unknown property '%s.%s' (ignored)", getName(), name.c_str()); |
---|
1365 | } |
---|
1366 | t = equals_sign + 1; // t=value |
---|
1367 | } |
---|
1368 | #ifdef WARN_MISSING_NAME |
---|
1369 | else // no parameter name |
---|
1370 | { |
---|
1371 | #ifdef SAVE_SELECTED_NAMES |
---|
1372 | if ((i < 0) // field after unknown field |
---|
1373 | || (i >= getPropCount()) // field after last field |
---|
1374 | || !(flags(i) & PARAM_CANOMITNAME)) // valid field but it can't be skipped |
---|
1375 | #endif |
---|
1376 | { |
---|
1377 | if (i < getPropCount()) |
---|
1378 | logPrintf("Param", "loadSingleLine", LOG_WARN, "Missing property name in '%s'", getName()); |
---|
1379 | else // 'i' can go past PropCount because of moving to subsequent properties by i++, id(i) is then NULL |
---|
1380 | logPrintf("Param", "loadSingleLine", LOG_WARN, "Value after the last property of '%s'", getName()); |
---|
1381 | } |
---|
1382 | } |
---|
1383 | //else skipping a skippable field |
---|
1384 | #endif |
---|
1385 | if ((i >= 0) && id(i)) // shared by name and skippped name cases |
---|
1386 | { |
---|
1387 | value = t; |
---|
1388 | if (quote) |
---|
1389 | { |
---|
1390 | tmpvalue.copyFrom(quote + 1, (int)(quote2 - quote) - 1); |
---|
1391 | sstringUnquote(tmpvalue); |
---|
1392 | value = tmpvalue.c_str(); |
---|
1393 | valstop = quote2; |
---|
1394 | } |
---|
1395 | else |
---|
1396 | if (field_end < end) valstop = field_end; else valstop = end; |
---|
1397 | |
---|
1398 | remember = *valstop; |
---|
1399 | *(char*)valstop = 0; |
---|
1400 | ret = setFromString(i, value, true); |
---|
1401 | fields_loaded++; |
---|
1402 | if (ret & PSET_PARSEFAILED) |
---|
1403 | parse_failed = true; |
---|
1404 | *(char*)valstop = remember; |
---|
1405 | } |
---|
1406 | |
---|
1407 | if (i >= 0) i++; |
---|
1408 | #ifdef __CODEGUARD__ |
---|
1409 | if (next_field < end - 1) t = next_field + 1; else return fields_loaded; |
---|
1410 | #else |
---|
1411 | t = next_field + 1; |
---|
1412 | #endif |
---|
1413 | } |
---|
1414 | if (parse_failed) options.parse_failed = true; |
---|
1415 | return fields_loaded; |
---|
1416 | } |
---|
1417 | |
---|
1418 | int Param::grmember(int g, int a) |
---|
1419 | { |
---|
1420 | if ((getGroupCount() < 2) && (!g)) |
---|
1421 | return (a < getPropCount()) ? a : -9999; |
---|
1422 | |
---|
1423 | ParamEntry *e = entry(0); |
---|
1424 | int x = 0, i = 0; |
---|
1425 | for (; e->id; i++, e++) |
---|
1426 | { |
---|
1427 | if (e->group == g) |
---|
1428 | if (a == x) return i; else x++; |
---|
1429 | } |
---|
1430 | return -9999; |
---|
1431 | } |
---|