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