[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 | { |
---|
[325] | 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 | { |
---|
[154] | 269 | err |= (fputs(altname ? altname : getName(), f) == EOF); |
---|
| 270 | err |= (fputs(":\n", f) == EOF); |
---|
| 271 | withname = true; |
---|
[109] | 272 | } |
---|
[154] | 273 | for (i = 0; p = id(i); i++) |
---|
| 274 | err |= saveprop(f, i, p, force); |
---|
| 275 | if (withname) |
---|
| 276 | err |= (fputs("\n", f) == EOF); |
---|
| 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 | |
---|
[154] | 292 | err |= (fputs(p, f) == EOF); fputc(':', f); |
---|
| 293 | cr = 0; |
---|
[312] | 294 | if ((*typ == 'x') || (*typ == 'o')) |
---|
[109] | 295 | { |
---|
[154] | 296 | ExtValue ex; |
---|
| 297 | get(i, ex); |
---|
| 298 | ws = SString(SERIALIZATION_PREFIX) + ex.serialize(); |
---|
[109] | 299 | } |
---|
[154] | 300 | else |
---|
| 301 | ws = get(i); |
---|
| 302 | quoteTilde(ws); |
---|
| 303 | w = ws; |
---|
| 304 | if (ws.len() > 50) cr = 1; |
---|
| 305 | else for (t = w; *t; t++) if ((*t == 10) || (*t == 13)) { cr = 1; break; } |
---|
| 306 | if (cr) fputs("~\n", f); |
---|
| 307 | err |= (fputs(w, f) == EOF); |
---|
| 308 | err |= (fputs(cr ? "~\n" : "\n", f) == EOF); |
---|
| 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); |
---|
| 374 | if (strContainsOneOf(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 | |
---|
[312] | 394 | int ParamInterface::load(VirtFILE* f, bool warn_unknown_fields, bool *abortable) |
---|
[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; |
---|
[312] | 402 | while (((!abortable) || (!*abortable)) && loadSStringLine(f, buf)) |
---|
[109] | 403 | { |
---|
[154] | 404 | const char* t = (const char*)buf; |
---|
| 405 | p0 = t; while ((*p0 == ' ') || (*p0 == '\t')) p0++; |
---|
| 406 | if (!*p0) break; |
---|
[312] | 407 | if (p0[0] == '#') continue; |
---|
[154] | 408 | p = strchr(p0, ':'); if (!p) continue; |
---|
[247] | 409 | p_len = (int)(p - p0); |
---|
[154] | 410 | loaded = false; |
---|
[268] | 411 | if (p_len && ((i = findIdn(p0, p_len)) >= 0)) |
---|
[109] | 412 | { |
---|
[312] | 413 | if (!(flags(i)&PARAM_DONTLOAD)) |
---|
[109] | 414 | { |
---|
[312] | 415 | if (p0[p_len + 1] == '~') |
---|
| 416 | { |
---|
| 417 | SString s; |
---|
| 418 | czytdotyldy(f, s); |
---|
| 419 | removeCR(s); |
---|
| 420 | int ch; while ((ch = fgetc(f)) != EOF) if (ch == '\n') break; |
---|
| 421 | unquoteTilde(s); |
---|
| 422 | set(i, (const char*)s); |
---|
| 423 | } |
---|
| 424 | else |
---|
| 425 | { |
---|
| 426 | set(i, p0 + p_len + 1); |
---|
| 427 | } |
---|
| 428 | fields_loaded++; |
---|
| 429 | loaded = true; |
---|
[109] | 430 | } |
---|
| 431 | } |
---|
[268] | 432 | else if (warn_unknown_fields) |
---|
[312] | 433 | { |
---|
| 434 | SString name(p0, p_len); |
---|
| 435 | FMprintf("ParamInterface", "load", FMLV_WARN, "Unknown property '%s' while reading object '%s' (ignored)", (const char*)name, getName()); |
---|
| 436 | } |
---|
[268] | 437 | |
---|
[154] | 438 | if ((!loaded) && (p0[p_len + 1] == '~')) |
---|
[109] | 439 | { // eat unrecognized multiline field |
---|
[154] | 440 | SString s; |
---|
| 441 | czytdotyldy(f, s); |
---|
| 442 | int ch; while ((ch = fgetc(f)) != EOF) if (ch == '\n') break; |
---|
[109] | 443 | } |
---|
| 444 | } |
---|
[154] | 445 | return fields_loaded; |
---|
[109] | 446 | } |
---|
| 447 | |
---|
| 448 | |
---|
| 449 | /* |
---|
| 450 | SString SimpleAbstractParam::getString(int i) |
---|
| 451 | { |
---|
| 452 | char *t; |
---|
| 453 | switch (*(t=type(i))) |
---|
[312] | 454 | { |
---|
[109] | 455 | case 'd': |
---|
| 456 | { |
---|
[312] | 457 | for (i=atol(get(i));i>=0;i--) if (t) t=strchr(t+1,'~'); |
---|
| 458 | if (t) |
---|
[109] | 459 | { |
---|
[312] | 460 | t++; |
---|
| 461 | char *t2=strchr(t,'~'); |
---|
| 462 | if (!t2) t2=t+strlen(t); |
---|
| 463 | SString str; |
---|
| 464 | strncpy(str.directWrite(t2-t),t,t2-t); |
---|
| 465 | str.endWrite(t2-t); |
---|
| 466 | return str; |
---|
[109] | 467 | } |
---|
| 468 | } |
---|
[312] | 469 | } |
---|
[109] | 470 | return get(i); |
---|
| 471 | } |
---|
| 472 | */ |
---|
| 473 | |
---|
| 474 | int ParamInterface::findId(const char* n) |
---|
| 475 | { |
---|
[154] | 476 | int i; const char *p; |
---|
| 477 | for (i = 0; p = id(i); i++) if (!strcmp(n, p)) return i; |
---|
| 478 | return -1; |
---|
[109] | 479 | } |
---|
| 480 | |
---|
[154] | 481 | int ParamInterface::findIdn(const char* naz, int n) |
---|
[109] | 482 | { |
---|
[154] | 483 | int i; const char *p; |
---|
| 484 | for (i = 0; p = id(i); i++) if ((!strncmp(naz, p, n)) && (!p[n])) return i; |
---|
| 485 | return -1; |
---|
[109] | 486 | } |
---|
| 487 | |
---|
[154] | 488 | void ParamInterface::get(int i, ExtValue &ret) |
---|
[109] | 489 | { |
---|
[154] | 490 | switch (type(i)[0]) |
---|
[109] | 491 | { |
---|
| 492 | case 'd': ret.setInt(getInt(i)); break; |
---|
| 493 | case 'f': ret.setDouble(getDouble(i)); break; |
---|
| 494 | case 's': ret.setString(getString(i)); break; |
---|
| 495 | case 'o': ret.setObject(getObject(i)); break; |
---|
[154] | 496 | case 'x': ret = getExtValue(i); break; |
---|
[316] | 497 | default: FMprintf("ParamInterface", "get", FMLV_ERROR, "'%s.%s' is not a field", getName(), id(i)); |
---|
[109] | 498 | } |
---|
| 499 | } |
---|
| 500 | |
---|
[154] | 501 | int ParamInterface::setInt(int i, const char* str) |
---|
[109] | 502 | { |
---|
[326] | 503 | paInt value; |
---|
| 504 | if (!ExtValue::parseInt(str,value,false,true)) |
---|
[109] | 505 | { |
---|
[314] | 506 | paInt mn, mx, def; |
---|
| 507 | if (getMinMax(i, mn, mx, def) >= 3) |
---|
| 508 | return setInt(i, def); |
---|
[154] | 509 | else |
---|
[247] | 510 | return setInt(i, (paInt)0); |
---|
[154] | 511 | } |
---|
[109] | 512 | else |
---|
[326] | 513 | return setInt(i, value); |
---|
[109] | 514 | } |
---|
| 515 | |
---|
[154] | 516 | int ParamInterface::setDouble(int i, const char* str) |
---|
[109] | 517 | { |
---|
[326] | 518 | double value; |
---|
| 519 | if (!ExtValue::parseDouble(str,value,true)) |
---|
[109] | 520 | { |
---|
[314] | 521 | double mn, mx, def; |
---|
| 522 | if (getMinMax(i, mn, mx, def) >= 3) |
---|
| 523 | return setDouble(i, def); |
---|
[154] | 524 | else |
---|
| 525 | return setDouble(i, (double)0); |
---|
| 526 | } |
---|
[109] | 527 | else |
---|
[326] | 528 | return setDouble(i, value); |
---|
[109] | 529 | } |
---|
| 530 | |
---|
[154] | 531 | int ParamInterface::set(int i, const ExtValue &v) |
---|
[109] | 532 | { |
---|
[154] | 533 | switch (type(i)[0]) |
---|
[109] | 534 | { |
---|
[144] | 535 | case 'd': |
---|
[154] | 536 | if ((v.type == TInt) || (v.type == TDouble)) return setInt(i, v.getInt()); |
---|
[144] | 537 | else |
---|
[154] | 538 | { |
---|
| 539 | if (v.type == TObj) |
---|
| 540 | FMprintf("ParamInterface", "set", FMLV_WARN, "Getting integer value from object reference (%s)", (const char*)v.getString()); |
---|
| 541 | return setInt(i, (const char*)v.getString()); |
---|
| 542 | } |
---|
[144] | 543 | case 'f': |
---|
[154] | 544 | if ((v.type == TInt) || (v.type == TDouble)) return setDouble(i, v.getDouble()); |
---|
[144] | 545 | else |
---|
[154] | 546 | { |
---|
| 547 | if (v.type == TObj) |
---|
| 548 | FMprintf("ParamInterface", "set", FMLV_WARN, "Getting floating point value from object reference (%s)", (const char*)v.getString()); |
---|
| 549 | return setDouble(i, (const char*)v.getString()); |
---|
| 550 | } |
---|
| 551 | case 's': { SString t = v.getString(); return setString(i, t); } |
---|
| 552 | case 'o': return setObject(i, v.getObject()); |
---|
| 553 | case 'x': return setExtValue(i, v); |
---|
| 554 | default: FMprintf("ParamInterface", "set", FMLV_ERROR, "'%s.%s' is not a field", getName(), id(i)); |
---|
[109] | 555 | } |
---|
[154] | 556 | return 0; |
---|
[109] | 557 | } |
---|
| 558 | |
---|
[154] | 559 | int ParamInterface::set(int i, const char *v) |
---|
[109] | 560 | { |
---|
[312] | 561 | char typ = type(i)[0]; |
---|
[273] | 562 | switch (typ) |
---|
[109] | 563 | { |
---|
[154] | 564 | case 'd': return setInt(i, v); |
---|
| 565 | case 'f': return setDouble(i, v); |
---|
| 566 | case 's': { SString t(v); return setString(i, t); } |
---|
[273] | 567 | case 'x': case 'o': |
---|
[109] | 568 | { |
---|
[154] | 569 | ExtValue e; |
---|
| 570 | const char* after; |
---|
| 571 | if (!strncmp(v, SERIALIZATION_PREFIX, strlen(SERIALIZATION_PREFIX))) |
---|
[109] | 572 | { |
---|
[154] | 573 | after = e.deserialize(v + strlen(SERIALIZATION_PREFIX)); |
---|
| 574 | if ((after == NULL) || (*after)) |
---|
| 575 | FMprintf("ParamInterface", "set", FMLV_WARN, "serialization format mismatch in %s.%s", (getName() ? getName() : "<Unknown>"), id(i)); |
---|
[109] | 576 | } |
---|
[154] | 577 | else if ((after = e.parseNumber(v)) && (*after == 0)) //consumed the whole string |
---|
[109] | 578 | { |
---|
[154] | 579 | //OK! |
---|
[109] | 580 | } |
---|
[154] | 581 | else |
---|
[109] | 582 | { |
---|
[154] | 583 | e.setString(SString(v)); |
---|
[109] | 584 | } |
---|
[312] | 585 | if (typ == 'x') |
---|
[273] | 586 | return setExtValue(i, e); |
---|
| 587 | else |
---|
| 588 | return setObject(i, e.getObject()); |
---|
[109] | 589 | } |
---|
| 590 | } |
---|
[154] | 591 | return 0; |
---|
[109] | 592 | } |
---|
| 593 | |
---|
| 594 | SString ParamInterface::getText(int i) |
---|
| 595 | { |
---|
[154] | 596 | const char *t; |
---|
| 597 | if ((*(t = type(i))) == 'd') |
---|
[109] | 598 | { |
---|
[314] | 599 | paInt mn, mx, def; |
---|
[312] | 600 | int value = getInt(i); |
---|
[314] | 601 | if (getMinMax(i, mn, mx, def) >= 2) |
---|
[312] | 602 | { |
---|
[314] | 603 | if (value > mx) |
---|
[310] | 604 | return get(i); |
---|
[314] | 605 | value -= mn; |
---|
[312] | 606 | } |
---|
| 607 | if (value < 0) return get(i); |
---|
[310] | 608 | for (; value >= 0; value--) if (t) t = strchr(t + 1, '~'); |
---|
[154] | 609 | if (t) |
---|
[109] | 610 | { |
---|
[154] | 611 | t++; |
---|
| 612 | const char *t2 = strchr(t, '~'); |
---|
| 613 | if (!t2) t2 = t + strlen(t); |
---|
[247] | 614 | return SString(t, (int)(t2 - t)); |
---|
[109] | 615 | } |
---|
| 616 | } |
---|
[154] | 617 | return get(i); |
---|
[109] | 618 | } |
---|
| 619 | |
---|
| 620 | SString ParamInterface::get(int i) |
---|
| 621 | { |
---|
[154] | 622 | switch (type(i)[0]) |
---|
[109] | 623 | { |
---|
| 624 | case 'd': return SString::valueOf(getInt(i)); |
---|
| 625 | case 'f': return SString::valueOf(getDouble(i)); |
---|
| 626 | case 's': return getString(i); |
---|
| 627 | } |
---|
[154] | 628 | ExtValue v; |
---|
| 629 | get(i, v); |
---|
| 630 | return v.getString(); |
---|
[109] | 631 | } |
---|
| 632 | |
---|
| 633 | |
---|
| 634 | //////////////////////////////// PARAM //////////////////////////////////// |
---|
| 635 | |
---|
[230] | 636 | #ifdef DEBUG |
---|
| 637 | void SimpleAbstractParam::sanityCheck(int i) |
---|
| 638 | { |
---|
[312] | 639 | ParamEntry *pe=entry(i); |
---|
[230] | 640 | |
---|
[312] | 641 | const char* t=pe->type; |
---|
| 642 | const char* err=NULL; |
---|
[230] | 643 | |
---|
[312] | 644 | if (*t=='p') |
---|
[230] | 645 | { |
---|
[312] | 646 | if (pe->fun1==NULL) |
---|
| 647 | err="no procedure defined"; |
---|
[230] | 648 | } |
---|
[312] | 649 | else |
---|
[230] | 650 | { |
---|
[312] | 651 | if (!(pe->flags & PARAM_READONLY)) |
---|
[230] | 652 | { //write access |
---|
[312] | 653 | if ((pe->fun2==NULL)&&(pe->offset==PARAM_ILLEGAL_OFFSET)) |
---|
| 654 | err="no field defined (GETONLY without PARAM_READONLY?)"; |
---|
[230] | 655 | } |
---|
| 656 | } |
---|
[312] | 657 | if (err!=NULL) |
---|
| 658 | FMprintf("SimpleAbstractParam","sanityCheck", FMLV_ERROR, |
---|
| 659 | "Invalid ParamEntry for %s.%s (%s)", getName(), pe->id, err); |
---|
[230] | 660 | } |
---|
| 661 | #endif |
---|
| 662 | |
---|
[109] | 663 | void *SimpleAbstractParam::getTarget(int i) |
---|
| 664 | { |
---|
[154] | 665 | return (void*)(((char*)object) + entry(i)->offset); |
---|
| 666 | //return &(object->*(entry(i)->fldptr)); |
---|
[109] | 667 | } |
---|
| 668 | |
---|
| 669 | ///////// get |
---|
| 670 | |
---|
[230] | 671 | #ifdef DEBUG |
---|
| 672 | #define SANITY_CHECK(i) sanityCheck(i) |
---|
| 673 | #else |
---|
| 674 | #define SANITY_CHECK(i) |
---|
| 675 | #endif |
---|
| 676 | |
---|
[247] | 677 | paInt SimpleAbstractParam::getInt(int i) |
---|
[109] | 678 | { |
---|
[230] | 679 | SANITY_CHECK(i); |
---|
[154] | 680 | ExtValue v; |
---|
| 681 | ParamEntry *pe = entry(i); |
---|
| 682 | if (pe->fun1) |
---|
[109] | 683 | { |
---|
[154] | 684 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 685 | return v.getInt(); |
---|
[109] | 686 | } |
---|
[154] | 687 | else |
---|
[109] | 688 | { |
---|
[154] | 689 | void *target = getTarget(i); |
---|
[247] | 690 | return *((paInt*)target); |
---|
[109] | 691 | } |
---|
| 692 | } |
---|
| 693 | |
---|
| 694 | double SimpleAbstractParam::getDouble(int i) |
---|
| 695 | { |
---|
[230] | 696 | SANITY_CHECK(i); |
---|
[154] | 697 | ExtValue v; |
---|
| 698 | ParamEntry *pe = entry(i); |
---|
| 699 | if (pe->fun1) |
---|
[109] | 700 | { |
---|
[154] | 701 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 702 | return v.getDouble(); |
---|
[109] | 703 | } |
---|
[154] | 704 | else |
---|
[109] | 705 | { |
---|
[154] | 706 | void *target = getTarget(i); |
---|
| 707 | return *((double*)target); |
---|
[109] | 708 | } |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | SString SimpleAbstractParam::getString(int i) |
---|
| 712 | { |
---|
[230] | 713 | SANITY_CHECK(i); |
---|
[154] | 714 | ExtValue v; |
---|
| 715 | ParamEntry *pe = entry(i); |
---|
| 716 | if (pe->fun1) |
---|
[109] | 717 | { |
---|
[154] | 718 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 719 | return v.getString(); |
---|
[109] | 720 | } |
---|
[154] | 721 | else |
---|
[109] | 722 | { |
---|
[154] | 723 | void *target = getTarget(i); |
---|
| 724 | return *((SString*)target); |
---|
[109] | 725 | } |
---|
| 726 | } |
---|
| 727 | |
---|
| 728 | ExtObject SimpleAbstractParam::getObject(int i) |
---|
| 729 | { |
---|
[230] | 730 | SANITY_CHECK(i); |
---|
[154] | 731 | ExtValue v; |
---|
| 732 | ParamEntry *pe = entry(i); |
---|
| 733 | if (pe->fun1) |
---|
[109] | 734 | { |
---|
[154] | 735 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 736 | return v.getObject(); |
---|
[109] | 737 | } |
---|
[154] | 738 | else |
---|
[109] | 739 | { |
---|
[154] | 740 | void *target = getTarget(i); |
---|
| 741 | return *((ExtObject*)target); |
---|
[109] | 742 | } |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | ExtValue SimpleAbstractParam::getExtValue(int i) |
---|
| 746 | { |
---|
[230] | 747 | SANITY_CHECK(i); |
---|
[154] | 748 | ExtValue v; |
---|
| 749 | ParamEntry *pe = entry(i); |
---|
| 750 | if (pe->fun1) |
---|
[109] | 751 | { |
---|
[154] | 752 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 753 | return v; |
---|
[109] | 754 | } |
---|
[154] | 755 | else |
---|
[109] | 756 | { |
---|
[154] | 757 | void *target = getTarget(i); |
---|
| 758 | return *((ExtValue*)target); |
---|
[109] | 759 | } |
---|
| 760 | } |
---|
| 761 | |
---|
| 762 | |
---|
| 763 | //////// set |
---|
| 764 | |
---|
[247] | 765 | int SimpleAbstractParam::setInt(int i, paInt x) |
---|
[109] | 766 | { |
---|
[230] | 767 | SANITY_CHECK(i); |
---|
[154] | 768 | ExtValue v; |
---|
| 769 | ParamEntry *pe = entry(i); |
---|
| 770 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
[247] | 771 | paInt xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
[314] | 772 | paInt mn = 0, mx = 0; |
---|
[154] | 773 | int result = 0; |
---|
| 774 | const char* t = pe->type + 1; |
---|
| 775 | while (*t) if (*t == ' ') break; else t++; |
---|
[314] | 776 | if (sscanf(t, PA_INT_SCANF " " PA_INT_SCANF, &mn, &mx) == 2) |
---|
[316] | 777 | if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking |
---|
[109] | 778 | { |
---|
[314] | 779 | if (x < mn) { x = mn; result = PSET_HITMIN; } |
---|
| 780 | else if (x > mx) { x = mx; result = PSET_HITMAX; } |
---|
[109] | 781 | } |
---|
| 782 | |
---|
[154] | 783 | if (pe->fun2) |
---|
[109] | 784 | { |
---|
[154] | 785 | v.setInt(x); |
---|
| 786 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
[109] | 787 | } |
---|
[154] | 788 | else |
---|
[109] | 789 | { |
---|
[154] | 790 | void *target = getTarget(i); |
---|
[247] | 791 | if (dontcheckchanges || (*((paInt*)target) != x)) |
---|
[154] | 792 | { |
---|
[109] | 793 | result |= PSET_CHANGED; |
---|
[247] | 794 | *((paInt*)target) = x; |
---|
[154] | 795 | } |
---|
[109] | 796 | } |
---|
[154] | 797 | messageOnExceedRange(i, result, xcopy); |
---|
[109] | 798 | return result; |
---|
| 799 | } |
---|
| 800 | |
---|
[154] | 801 | int SimpleAbstractParam::setDouble(int i, double x) |
---|
[109] | 802 | { |
---|
[230] | 803 | SANITY_CHECK(i); |
---|
[154] | 804 | ExtValue v; |
---|
| 805 | ParamEntry *pe = entry(i); |
---|
| 806 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
| 807 | double xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
[314] | 808 | double mn = 0, mx = 0; |
---|
[154] | 809 | int result = 0; |
---|
| 810 | const char* t = pe->type + 1; |
---|
| 811 | while (*t) if (*t == ' ') break; else t++; |
---|
[314] | 812 | if (sscanf(t, "%lg %lg", &mn, &mx) == 2) |
---|
[316] | 813 | if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking |
---|
[109] | 814 | { |
---|
[314] | 815 | if (x < mn) { x = mn; result = PSET_HITMIN; } |
---|
| 816 | else if (x > mx) { x = mx; result = PSET_HITMAX; } |
---|
[109] | 817 | } |
---|
| 818 | |
---|
[154] | 819 | if (pe->fun2) |
---|
[109] | 820 | { |
---|
[154] | 821 | v.setDouble(x); |
---|
| 822 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
[109] | 823 | } |
---|
[154] | 824 | else |
---|
[109] | 825 | { |
---|
[154] | 826 | void *target = getTarget(i); |
---|
| 827 | if (dontcheckchanges || (*((double*)target) != x)) |
---|
[109] | 828 | { |
---|
[154] | 829 | result |= PSET_CHANGED; |
---|
| 830 | *((double*)target) = x; |
---|
[109] | 831 | } |
---|
| 832 | } |
---|
[154] | 833 | messageOnExceedRange(i, result, xcopy); |
---|
[109] | 834 | return result; |
---|
| 835 | } |
---|
| 836 | |
---|
[154] | 837 | int SimpleAbstractParam::setString(int i, const SString& x) |
---|
[109] | 838 | { |
---|
[230] | 839 | SANITY_CHECK(i); |
---|
[154] | 840 | ExtValue v; |
---|
| 841 | SString vs; |
---|
| 842 | const SString *xx = &x; |
---|
| 843 | ParamEntry *pe = entry(i); |
---|
| 844 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
| 845 | SString xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
| 846 | const char* t = pe->type + 1; |
---|
| 847 | while (*t) if (*t == ' ') break; else t++; |
---|
[314] | 848 | int mn = 0, mx = 0; |
---|
[154] | 849 | int result = 0; |
---|
[314] | 850 | if (sscanf(t, "%d %d", &mn, &mx) == 2) //using getMinMax would also get default value, which is not needed here |
---|
[109] | 851 | { |
---|
[314] | 852 | if ((x.len() > mx) && (mx > 0)) |
---|
[109] | 853 | { |
---|
[314] | 854 | vs = x.substr(0, mx); |
---|
[154] | 855 | xx = &vs; |
---|
| 856 | result |= PSET_HITMAX; |
---|
[109] | 857 | } |
---|
| 858 | } |
---|
| 859 | |
---|
[154] | 860 | if (pe->fun2) |
---|
[109] | 861 | { |
---|
[154] | 862 | v.setString(*xx); |
---|
| 863 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
[109] | 864 | } |
---|
[154] | 865 | else |
---|
[109] | 866 | { |
---|
[154] | 867 | void *target = getTarget(i); |
---|
| 868 | if (dontcheckchanges || (!(*((SString*)target) == *xx))) |
---|
[109] | 869 | { |
---|
[154] | 870 | result |= PSET_CHANGED; |
---|
[306] | 871 | *((SString*)target) = *xx; |
---|
[109] | 872 | } |
---|
| 873 | } |
---|
[154] | 874 | messageOnExceedRange(i, result, xcopy); |
---|
[109] | 875 | return result; |
---|
| 876 | } |
---|
| 877 | |
---|
[154] | 878 | int SimpleAbstractParam::setObject(int i, const ExtObject& x) |
---|
[109] | 879 | { |
---|
[230] | 880 | SANITY_CHECK(i); |
---|
[154] | 881 | ExtValue v; |
---|
| 882 | ParamEntry *pe = entry(i); |
---|
| 883 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
| 884 | ExtObject xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
| 885 | if (pe->fun2) |
---|
[109] | 886 | { |
---|
[154] | 887 | v.setObject(x); |
---|
| 888 | int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
| 889 | messageOnExceedRange(i, result, xcopy); |
---|
| 890 | return result; |
---|
[109] | 891 | } |
---|
[154] | 892 | else |
---|
[109] | 893 | { |
---|
[154] | 894 | void *target = getTarget(i); |
---|
| 895 | *((ExtObject*)target) = x; |
---|
| 896 | return PSET_CHANGED; |
---|
[109] | 897 | } |
---|
| 898 | } |
---|
| 899 | |
---|
[154] | 900 | int SimpleAbstractParam::setExtValue(int i, const ExtValue& x) |
---|
[109] | 901 | { |
---|
[230] | 902 | SANITY_CHECK(i); |
---|
[154] | 903 | ParamEntry *pe = entry(i); |
---|
| 904 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
| 905 | ExtValue xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
| 906 | if (pe->fun2) |
---|
[109] | 907 | { |
---|
[154] | 908 | int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x); |
---|
| 909 | messageOnExceedRange(i, result, xcopy); |
---|
| 910 | return result; |
---|
[109] | 911 | } |
---|
[154] | 912 | else |
---|
[109] | 913 | { |
---|
[154] | 914 | void *target = getTarget(i); |
---|
| 915 | *((ExtValue*)target) = x; |
---|
| 916 | return PSET_CHANGED; |
---|
[109] | 917 | } |
---|
| 918 | } |
---|
| 919 | |
---|
[154] | 920 | void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret) |
---|
[109] | 921 | { |
---|
[230] | 922 | SANITY_CHECK(i); |
---|
[154] | 923 | ParamEntry *pe = entry(i); |
---|
| 924 | if (!pe) return; |
---|
| 925 | if (pe->fun1 && (pe->type[0] == 'p')) |
---|
| 926 | (*(void(*)(void*, ExtValue*, ExtValue*))pe->fun1)(object, args, ret); |
---|
| 927 | else |
---|
[109] | 928 | { |
---|
[154] | 929 | FMprintf("SimpleAbstractParam", "call", FMLV_ERROR, |
---|
| 930 | (*pe->type != 'p') ? "'%s.%s' is not a function" : "Internal error - undefined function pointer for '%s.%s'", getName(), pe->id); |
---|
[290] | 931 | ret->setInvalid(); |
---|
[109] | 932 | } |
---|
| 933 | } |
---|
| 934 | |
---|
[278] | 935 | void SimpleAbstractParam::setDefault() |
---|
[109] | 936 | { |
---|
[154] | 937 | bool save = dontcheckchanges; |
---|
| 938 | dontcheckchanges = 1; |
---|
[278] | 939 | ParamInterface::setDefault(); |
---|
[154] | 940 | dontcheckchanges = save; |
---|
[109] | 941 | } |
---|
| 942 | |
---|
[278] | 943 | void SimpleAbstractParam::setDefault(int i) |
---|
[109] | 944 | { |
---|
[154] | 945 | bool save = dontcheckchanges; |
---|
| 946 | dontcheckchanges = 1; |
---|
[278] | 947 | ParamInterface::setDefault(i); |
---|
[154] | 948 | dontcheckchanges = save; |
---|
[109] | 949 | } |
---|
| 950 | |
---|
[154] | 951 | // Returns the address of the beginning of the line. |
---|
| 952 | // len = line length (without \n). |
---|
| 953 | // 0 may mean the line with length=0 or the end of the SString. |
---|
| 954 | // poz is advanced to the beginning of the next line. |
---|
| 955 | // A typical loop: for(poz=0;poz<s.d;) {line=getline(s,poz,len);... |
---|
| 956 | static const char *getline(const SString &s, int &poz, int &len) |
---|
[109] | 957 | { |
---|
[154] | 958 | const char *beg = (const char*)s + poz; |
---|
| 959 | if (poz >= s.len()) { poz = s.len(); len = 0; return (const char*)s + s.len(); } |
---|
| 960 | const char *lf = strchr(beg, '\n'); |
---|
| 961 | if (!lf) { lf = (const char*)s + s.len() - 1; poz = s.len(); } |
---|
[247] | 962 | else { poz = (int)(lf - (const char*)s) + 1; if (poz > s.len()) poz = s.len(); } |
---|
[154] | 963 | while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break; |
---|
[247] | 964 | len = (int)(lf - beg) + 1; |
---|
[154] | 965 | return beg; |
---|
[109] | 966 | } |
---|
| 967 | |
---|
[154] | 968 | int ParamInterface::load2(const SString &s, int &poz) |
---|
[109] | 969 | { |
---|
[154] | 970 | int i; // the index number of the parameter |
---|
| 971 | int tmpi; |
---|
| 972 | int len; |
---|
| 973 | int ret; |
---|
| 974 | int fields_loaded = 0; |
---|
| 975 | const char *t, *lin, *end; |
---|
[320] | 976 | const char *equals_sign, *field_end, *next_field; |
---|
[154] | 977 | char remember; |
---|
| 978 | const char *quote, *quote2; |
---|
| 979 | const char *value, *valstop; |
---|
| 980 | SString tmpvalue; |
---|
| 981 | if (poz >= s.len()) return fields_loaded; |
---|
| 982 | t = (const char*)s + poz; |
---|
[109] | 983 | |
---|
[154] | 984 | lin = getline(s, poz, len); // all fields must be encoded in a single line |
---|
| 985 | if (!len) return fields_loaded; // empty line = end |
---|
| 986 | i = 0; |
---|
| 987 | end = lin + len; |
---|
| 988 | while (t < end) |
---|
| 989 | { |
---|
| 990 | // processing a single field |
---|
[320] | 991 | // "p:name=field_value, field_name=field_value , name=value..." |
---|
| 992 | // ^ ^-t (after) ^ ^_next_field |
---|
| 993 | // \_t (before) \_field_end |
---|
[325] | 994 | while (isspace(*t)) if (t < end) t++; else return fields_loaded; |
---|
[109] | 995 | |
---|
[320] | 996 | field_end = strchrlimit(t, ',', end); if (!field_end) field_end = end; |
---|
| 997 | next_field=field_end; |
---|
[325] | 998 | while ((field_end>t) && isblank(field_end[-1])) field_end--; |
---|
[320] | 999 | quote = strchrlimit(t, '\"', field_end); |
---|
[154] | 1000 | if (quote) |
---|
[109] | 1001 | { |
---|
[154] | 1002 | quote2 = skipQuoteString(quote + 1, end); |
---|
[320] | 1003 | if (quote2 > field_end) |
---|
[154] | 1004 | { |
---|
[320] | 1005 | field_end = strchrlimit(quote2 + 1, ',', end); |
---|
| 1006 | if (!field_end) next_field = field_end = end; |
---|
[154] | 1007 | } |
---|
| 1008 | equals_sign = strchrlimit(t, '=', quote); |
---|
[109] | 1009 | } |
---|
[154] | 1010 | else |
---|
| 1011 | { |
---|
[320] | 1012 | equals_sign = strchrlimit(t, '=', field_end); |
---|
[154] | 1013 | quote2 = 0; |
---|
| 1014 | } |
---|
| 1015 | if (equals_sign == t) { t++; equals_sign = 0; } |
---|
[320] | 1016 | if (field_end == t) // skip empty value |
---|
[154] | 1017 | { |
---|
| 1018 | t++; i++; |
---|
| 1019 | continue; |
---|
| 1020 | } |
---|
| 1021 | if (equals_sign) // have parameter name |
---|
| 1022 | { |
---|
[247] | 1023 | tmpi = findIdn(t, (int)(equals_sign - t)); |
---|
[154] | 1024 | i = tmpi; |
---|
| 1025 | if (tmpi < 0) |
---|
[312] | 1026 | { |
---|
| 1027 | SString name(t, (int)(equals_sign - t)); |
---|
| 1028 | FMprintf("Param", "load2", FMLV_WARN, "Unknown property '%s' while reading object '%s' (ignored)", (const char*)name, getName()); |
---|
| 1029 | } |
---|
[154] | 1030 | t = equals_sign + 1; // t=value |
---|
| 1031 | } |
---|
[109] | 1032 | #ifdef WARN_MISSING_NAME |
---|
[154] | 1033 | else |
---|
[109] | 1034 | #ifdef SAVE_SELECTED_NAMES |
---|
[154] | 1035 | if (!(flags(i)&PARAM_CANOMITNAME)) |
---|
[109] | 1036 | #endif |
---|
[154] | 1037 | { |
---|
[312] | 1038 | FMprintf("Param", "load2", FMLV_WARN, "Missing property name in '%s' (assuming '%s')", |
---|
| 1039 | getName(), id(i) ? id(i) : "unknown property?"); |
---|
[154] | 1040 | } |
---|
[109] | 1041 | #endif |
---|
[154] | 1042 | if ((i >= 0) && id(i)) |
---|
[109] | 1043 | { |
---|
[154] | 1044 | value = t; |
---|
| 1045 | if (quote) |
---|
| 1046 | { |
---|
[247] | 1047 | tmpvalue.copyFrom(quote + 1, (int)(quote2 - quote) - 1); |
---|
[154] | 1048 | sstringUnquote(tmpvalue); |
---|
| 1049 | value = tmpvalue; |
---|
| 1050 | valstop = quote2; |
---|
| 1051 | } |
---|
| 1052 | else |
---|
[320] | 1053 | if (field_end < end) valstop = field_end; else valstop = end; |
---|
[154] | 1054 | |
---|
| 1055 | remember = *valstop; |
---|
| 1056 | *(char*)valstop = 0; |
---|
| 1057 | ret = set(i, value); |
---|
| 1058 | fields_loaded++; |
---|
| 1059 | if (ret&(PSET_HITMAX | PSET_HITMIN)) |
---|
| 1060 | FMprintf("Param", "load2", FMLV_WARN, "Adjusted '%s' in '%s' (was too %s)", |
---|
| 1061 | id(i), getName(), (ret&PSET_HITMAX) ? "big" : "small"); |
---|
| 1062 | *(char*)valstop = remember; |
---|
[109] | 1063 | } |
---|
| 1064 | |
---|
[154] | 1065 | if (i >= 0) i++; |
---|
[109] | 1066 | #ifdef __CODEGUARD__ |
---|
[320] | 1067 | if (next_field<end-1) t=next_field+1; else return fields_loaded; |
---|
[109] | 1068 | #else |
---|
[320] | 1069 | t = next_field + 1; |
---|
[109] | 1070 | #endif |
---|
[154] | 1071 | } |
---|
| 1072 | return fields_loaded; |
---|
[109] | 1073 | } |
---|
| 1074 | |
---|
[154] | 1075 | int Param::grmember(int g, int a) |
---|
[109] | 1076 | { |
---|
[154] | 1077 | if ((getGroupCount() < 2) && (!g)) |
---|
| 1078 | return (a < getPropCount()) ? a : -9999; |
---|
[109] | 1079 | |
---|
[154] | 1080 | ParamEntry *e = entry(0); |
---|
| 1081 | int x = 0, i = 0; |
---|
| 1082 | for (; e->id; i++, e++) |
---|
[109] | 1083 | { |
---|
[154] | 1084 | if (e->group == g) |
---|
| 1085 | if (a == x) return i; else x++; |
---|
[109] | 1086 | } |
---|
[154] | 1087 | return -9999; |
---|
[109] | 1088 | } |
---|