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