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