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