[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] | 704 | SString ParamInterface::getText(int i) //find the current enum text or call get(i) if not enum |
---|
[109] | 705 | { |
---|
[154] | 706 | const char *t; |
---|
[704] | 707 | if (((*(t = type(i))) == 'd') && (strchr(t, '~')!=NULL)) //type is int and contains enum labels |
---|
[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) |
---|
[704] | 714 | return get(i);//unexpected value of out bounds (should never happen) -> fallback |
---|
[314] | 715 | value -= mn; |
---|
[312] | 716 | } |
---|
[704] | 717 | if (value < 0) return get(i); //unexpected value of out bounds (should never happen) -> fallback |
---|
| 718 | // now value is 0-based index of ~text |
---|
| 719 | for (; value >= 0; value--) if (t) t = strchr(t + 1, '~'); else break; |
---|
| 720 | if (t) // found n-th ~text in type description (else: not enough ~texts in type description) |
---|
[109] | 721 | { |
---|
[154] | 722 | t++; |
---|
| 723 | const char *t2 = strchr(t, '~'); |
---|
| 724 | if (!t2) t2 = t + strlen(t); |
---|
[247] | 725 | return SString(t, (int)(t2 - t)); |
---|
[109] | 726 | } |
---|
| 727 | } |
---|
[704] | 728 | return get(i); //fallback - return int value as string |
---|
[109] | 729 | } |
---|
| 730 | |
---|
| 731 | SString ParamInterface::get(int i) |
---|
| 732 | { |
---|
[154] | 733 | switch (type(i)[0]) |
---|
[109] | 734 | { |
---|
| 735 | case 'd': return SString::valueOf(getInt(i)); |
---|
| 736 | case 'f': return SString::valueOf(getDouble(i)); |
---|
| 737 | case 's': return getString(i); |
---|
| 738 | } |
---|
[154] | 739 | ExtValue v; |
---|
| 740 | get(i, v); |
---|
| 741 | return v.getString(); |
---|
[109] | 742 | } |
---|
| 743 | |
---|
[535] | 744 | bool ParamInterface::isValidTypeDescription(const char* t) |
---|
| 745 | { |
---|
[650] | 746 | if (t == NULL) return false; |
---|
| 747 | if (*t == 0) return false; |
---|
| 748 | if (strchr("dfsoxp", *t) == NULL) return false; |
---|
| 749 | switch (*t) |
---|
[574] | 750 | { |
---|
| 751 | case 'd': |
---|
[650] | 752 | {paInt a, b, c; if (getMinMax(t, a, b, c) == 1) return false; } |
---|
[574] | 753 | break; |
---|
| 754 | case 'f': |
---|
[650] | 755 | {double a, b, c; if (getMinMax(t, a, b, c) == 1) return false; } |
---|
[574] | 756 | break; |
---|
| 757 | } |
---|
[650] | 758 | return true; |
---|
[535] | 759 | } |
---|
[109] | 760 | |
---|
[640] | 761 | SString ParamInterface::describeType(const char* type) |
---|
| 762 | { |
---|
[650] | 763 | SString t; |
---|
| 764 | switch (type[0]) |
---|
[640] | 765 | { |
---|
[650] | 766 | case 'd': t += "integer"; |
---|
| 767 | {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); } |
---|
| 768 | break; |
---|
| 769 | case 'f': t += "float"; |
---|
| 770 | {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); } |
---|
| 771 | break; |
---|
| 772 | case 's': t += "string"; |
---|
| 773 | {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()); } |
---|
| 774 | break; |
---|
| 775 | case 'x': t += "untyped value"; break; |
---|
| 776 | case 'p': t += "function"; break; |
---|
| 777 | case 'o': t += "object"; if (type[1]) { t += " of class "; t += type + 1; } break; |
---|
[640] | 778 | default: return "unknown type"; |
---|
| 779 | } |
---|
[650] | 780 | return t; |
---|
[640] | 781 | } |
---|
| 782 | |
---|
[109] | 783 | //////////////////////////////// PARAM //////////////////////////////////// |
---|
| 784 | |
---|
[483] | 785 | #ifdef _DEBUG |
---|
[230] | 786 | void SimpleAbstractParam::sanityCheck(int i) |
---|
| 787 | { |
---|
[650] | 788 | ParamEntry *pe = entry(i); |
---|
[230] | 789 | |
---|
[650] | 790 | const char* t = pe->type; |
---|
| 791 | const char* err = NULL; |
---|
[230] | 792 | |
---|
[535] | 793 | if (!isValidTypeDescription(t)) |
---|
[650] | 794 | err = "invalid type description"; |
---|
| 795 | if (*t == 'p') |
---|
[230] | 796 | { |
---|
[650] | 797 | if (pe->fun1 == NULL) |
---|
| 798 | err = "no procedure defined"; |
---|
[654] | 799 | if (pe->flags & PARAM_READONLY) |
---|
| 800 | err = "function can't be PARAM_READONLY"; |
---|
[230] | 801 | } |
---|
[312] | 802 | else |
---|
[230] | 803 | { |
---|
[659] | 804 | if (!(pe->flags & (PARAM_READONLY | PARAM_DONTSAVE | PARAM_USERREADONLY | PARAM_CONST | PARAM_DONTLOAD | PARAM_LINECOMMENT | PARAM_OBJECTSET))) |
---|
[230] | 805 | { //write access |
---|
[650] | 806 | if ((pe->fun2 == NULL) && (pe->offset == PARAM_ILLEGAL_OFFSET)) |
---|
| 807 | err = "no field defined (GETONLY without PARAM_READONLY?)"; |
---|
[230] | 808 | } |
---|
| 809 | } |
---|
[650] | 810 | if (err != NULL) |
---|
| 811 | logPrintf("SimpleAbstractParam", "sanityCheck", LOG_ERROR, |
---|
[312] | 812 | "Invalid ParamEntry for %s.%s (%s)", getName(), pe->id, err); |
---|
[650] | 813 | } |
---|
[230] | 814 | #endif |
---|
| 815 | |
---|
[109] | 816 | void *SimpleAbstractParam::getTarget(int i) |
---|
| 817 | { |
---|
[154] | 818 | return (void*)(((char*)object) + entry(i)->offset); |
---|
| 819 | //return &(object->*(entry(i)->fldptr)); |
---|
[109] | 820 | } |
---|
| 821 | |
---|
| 822 | ///////// get |
---|
| 823 | |
---|
[483] | 824 | #ifdef _DEBUG |
---|
[230] | 825 | #define SANITY_CHECK(i) sanityCheck(i) |
---|
| 826 | #else |
---|
| 827 | #define SANITY_CHECK(i) |
---|
| 828 | #endif |
---|
| 829 | |
---|
[247] | 830 | paInt SimpleAbstractParam::getInt(int i) |
---|
[109] | 831 | { |
---|
[230] | 832 | SANITY_CHECK(i); |
---|
[154] | 833 | ExtValue v; |
---|
| 834 | ParamEntry *pe = entry(i); |
---|
| 835 | if (pe->fun1) |
---|
[109] | 836 | { |
---|
[154] | 837 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 838 | return v.getInt(); |
---|
[109] | 839 | } |
---|
[154] | 840 | else |
---|
[109] | 841 | { |
---|
[154] | 842 | void *target = getTarget(i); |
---|
[247] | 843 | return *((paInt*)target); |
---|
[109] | 844 | } |
---|
| 845 | } |
---|
| 846 | |
---|
| 847 | double SimpleAbstractParam::getDouble(int i) |
---|
| 848 | { |
---|
[230] | 849 | SANITY_CHECK(i); |
---|
[154] | 850 | ExtValue v; |
---|
| 851 | ParamEntry *pe = entry(i); |
---|
| 852 | if (pe->fun1) |
---|
[109] | 853 | { |
---|
[154] | 854 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 855 | return v.getDouble(); |
---|
[109] | 856 | } |
---|
[154] | 857 | else |
---|
[109] | 858 | { |
---|
[154] | 859 | void *target = getTarget(i); |
---|
| 860 | return *((double*)target); |
---|
[109] | 861 | } |
---|
| 862 | } |
---|
| 863 | |
---|
| 864 | SString SimpleAbstractParam::getString(int i) |
---|
| 865 | { |
---|
[230] | 866 | SANITY_CHECK(i); |
---|
[154] | 867 | ExtValue v; |
---|
| 868 | ParamEntry *pe = entry(i); |
---|
| 869 | if (pe->fun1) |
---|
[109] | 870 | { |
---|
[154] | 871 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 872 | return v.getString(); |
---|
[109] | 873 | } |
---|
[154] | 874 | else |
---|
[109] | 875 | { |
---|
[154] | 876 | void *target = getTarget(i); |
---|
| 877 | return *((SString*)target); |
---|
[109] | 878 | } |
---|
| 879 | } |
---|
| 880 | |
---|
| 881 | ExtObject SimpleAbstractParam::getObject(int i) |
---|
| 882 | { |
---|
[230] | 883 | SANITY_CHECK(i); |
---|
[154] | 884 | ExtValue v; |
---|
| 885 | ParamEntry *pe = entry(i); |
---|
| 886 | if (pe->fun1) |
---|
[109] | 887 | { |
---|
[154] | 888 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 889 | return v.getObject(); |
---|
[109] | 890 | } |
---|
[154] | 891 | else |
---|
[109] | 892 | { |
---|
[154] | 893 | void *target = getTarget(i); |
---|
| 894 | return *((ExtObject*)target); |
---|
[109] | 895 | } |
---|
| 896 | } |
---|
| 897 | |
---|
| 898 | ExtValue SimpleAbstractParam::getExtValue(int i) |
---|
| 899 | { |
---|
[230] | 900 | SANITY_CHECK(i); |
---|
[154] | 901 | ExtValue v; |
---|
| 902 | ParamEntry *pe = entry(i); |
---|
| 903 | if (pe->fun1) |
---|
[109] | 904 | { |
---|
[154] | 905 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 906 | return v; |
---|
[109] | 907 | } |
---|
[154] | 908 | else |
---|
[109] | 909 | { |
---|
[154] | 910 | void *target = getTarget(i); |
---|
| 911 | return *((ExtValue*)target); |
---|
[109] | 912 | } |
---|
| 913 | } |
---|
| 914 | |
---|
| 915 | |
---|
| 916 | //////// set |
---|
| 917 | |
---|
[247] | 918 | int SimpleAbstractParam::setInt(int i, paInt x) |
---|
[109] | 919 | { |
---|
[230] | 920 | SANITY_CHECK(i); |
---|
[154] | 921 | ExtValue v; |
---|
| 922 | ParamEntry *pe = entry(i); |
---|
| 923 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
[247] | 924 | paInt xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
[574] | 925 | paInt mn = 0, mx = 0, de = 0; |
---|
[154] | 926 | int result = 0; |
---|
[650] | 927 | if (getMinMax(pe->type, mn, mx, de) >= 2) |
---|
[316] | 928 | if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking |
---|
[109] | 929 | { |
---|
[314] | 930 | if (x < mn) { x = mn; result = PSET_HITMIN; } |
---|
| 931 | else if (x > mx) { x = mx; result = PSET_HITMAX; } |
---|
[109] | 932 | } |
---|
| 933 | |
---|
[154] | 934 | if (pe->fun2) |
---|
[109] | 935 | { |
---|
[154] | 936 | v.setInt(x); |
---|
| 937 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
[109] | 938 | } |
---|
[154] | 939 | else |
---|
[109] | 940 | { |
---|
[154] | 941 | void *target = getTarget(i); |
---|
[247] | 942 | if (dontcheckchanges || (*((paInt*)target) != x)) |
---|
[154] | 943 | { |
---|
[109] | 944 | result |= PSET_CHANGED; |
---|
[247] | 945 | *((paInt*)target) = x; |
---|
[154] | 946 | } |
---|
[109] | 947 | } |
---|
[154] | 948 | messageOnExceedRange(i, result, xcopy); |
---|
[109] | 949 | return result; |
---|
| 950 | } |
---|
| 951 | |
---|
[154] | 952 | int SimpleAbstractParam::setDouble(int i, double x) |
---|
[109] | 953 | { |
---|
[230] | 954 | SANITY_CHECK(i); |
---|
[154] | 955 | ExtValue v; |
---|
| 956 | ParamEntry *pe = entry(i); |
---|
| 957 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
| 958 | double xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
[574] | 959 | double mn = 0, mx = 0, de = 0; |
---|
[154] | 960 | int result = 0; |
---|
[650] | 961 | if (getMinMax(pe->type, mn, mx, de) >= 2) |
---|
[316] | 962 | if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking |
---|
[109] | 963 | { |
---|
[314] | 964 | if (x < mn) { x = mn; result = PSET_HITMIN; } |
---|
| 965 | else if (x > mx) { x = mx; result = PSET_HITMAX; } |
---|
[109] | 966 | } |
---|
| 967 | |
---|
[154] | 968 | if (pe->fun2) |
---|
[109] | 969 | { |
---|
[154] | 970 | v.setDouble(x); |
---|
| 971 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
[109] | 972 | } |
---|
[154] | 973 | else |
---|
[109] | 974 | { |
---|
[154] | 975 | void *target = getTarget(i); |
---|
| 976 | if (dontcheckchanges || (*((double*)target) != x)) |
---|
[109] | 977 | { |
---|
[154] | 978 | result |= PSET_CHANGED; |
---|
| 979 | *((double*)target) = x; |
---|
[109] | 980 | } |
---|
| 981 | } |
---|
[154] | 982 | messageOnExceedRange(i, result, xcopy); |
---|
[109] | 983 | return result; |
---|
| 984 | } |
---|
| 985 | |
---|
[154] | 986 | int SimpleAbstractParam::setString(int i, const SString& x) |
---|
[109] | 987 | { |
---|
[230] | 988 | SANITY_CHECK(i); |
---|
[154] | 989 | ExtValue v; |
---|
| 990 | SString vs; |
---|
| 991 | const SString *xx = &x; |
---|
| 992 | ParamEntry *pe = entry(i); |
---|
| 993 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
| 994 | SString xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
| 995 | const char* t = pe->type + 1; |
---|
| 996 | while (*t) if (*t == ' ') break; else t++; |
---|
[314] | 997 | int mn = 0, mx = 0; |
---|
[154] | 998 | int result = 0; |
---|
[314] | 999 | if (sscanf(t, "%d %d", &mn, &mx) == 2) //using getMinMax would also get default value, which is not needed here |
---|
[109] | 1000 | { |
---|
[314] | 1001 | if ((x.len() > mx) && (mx > 0)) |
---|
[109] | 1002 | { |
---|
[314] | 1003 | vs = x.substr(0, mx); |
---|
[154] | 1004 | xx = &vs; |
---|
| 1005 | result |= PSET_HITMAX; |
---|
[109] | 1006 | } |
---|
| 1007 | } |
---|
| 1008 | |
---|
[154] | 1009 | if (pe->fun2) |
---|
[109] | 1010 | { |
---|
[154] | 1011 | v.setString(*xx); |
---|
| 1012 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
[109] | 1013 | } |
---|
[154] | 1014 | else |
---|
[109] | 1015 | { |
---|
[154] | 1016 | void *target = getTarget(i); |
---|
| 1017 | if (dontcheckchanges || (!(*((SString*)target) == *xx))) |
---|
[109] | 1018 | { |
---|
[154] | 1019 | result |= PSET_CHANGED; |
---|
[306] | 1020 | *((SString*)target) = *xx; |
---|
[109] | 1021 | } |
---|
| 1022 | } |
---|
[154] | 1023 | messageOnExceedRange(i, result, xcopy); |
---|
[109] | 1024 | return result; |
---|
| 1025 | } |
---|
| 1026 | |
---|
[154] | 1027 | int SimpleAbstractParam::setObject(int i, const ExtObject& x) |
---|
[109] | 1028 | { |
---|
[230] | 1029 | SANITY_CHECK(i); |
---|
[154] | 1030 | ExtValue v; |
---|
| 1031 | ParamEntry *pe = entry(i); |
---|
| 1032 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
[478] | 1033 | if (pe->flags&PARAM_OBJECTSET) |
---|
[650] | 1034 | { |
---|
| 1035 | ExtObject o = getObject(i); |
---|
[478] | 1036 | Param tmp; |
---|
[650] | 1037 | ParamInterface* oif = o.getParamInterface(tmp); |
---|
[478] | 1038 | int ass; |
---|
[650] | 1039 | if (oif && ((ass = oif->findId("assign")) >= 0)) |
---|
| 1040 | { |
---|
| 1041 | ExtValue arg = x; |
---|
| 1042 | oif->call(ass, &arg, &v); |
---|
| 1043 | } |
---|
[478] | 1044 | else |
---|
| 1045 | logPrintf("SimpleAbstractParam", "setObject", LOG_ERROR, |
---|
[650] | 1046 | "'%s.%s' is PARAM_OBJECTSET but no 'assign()' in %s", getName(), pe->id, o.interfaceName()); |
---|
[478] | 1047 | return PSET_CHANGED; |
---|
[650] | 1048 | } |
---|
[154] | 1049 | ExtObject xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
| 1050 | if (pe->fun2) |
---|
[109] | 1051 | { |
---|
[154] | 1052 | v.setObject(x); |
---|
| 1053 | int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
| 1054 | messageOnExceedRange(i, result, xcopy); |
---|
| 1055 | return result; |
---|
[109] | 1056 | } |
---|
[154] | 1057 | else |
---|
[109] | 1058 | { |
---|
[154] | 1059 | void *target = getTarget(i); |
---|
| 1060 | *((ExtObject*)target) = x; |
---|
| 1061 | return PSET_CHANGED; |
---|
[109] | 1062 | } |
---|
| 1063 | } |
---|
| 1064 | |
---|
[154] | 1065 | int SimpleAbstractParam::setExtValue(int i, const ExtValue& x) |
---|
[109] | 1066 | { |
---|
[230] | 1067 | SANITY_CHECK(i); |
---|
[154] | 1068 | ParamEntry *pe = entry(i); |
---|
| 1069 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
| 1070 | ExtValue xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
| 1071 | if (pe->fun2) |
---|
[109] | 1072 | { |
---|
[154] | 1073 | int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x); |
---|
| 1074 | messageOnExceedRange(i, result, xcopy); |
---|
| 1075 | return result; |
---|
[109] | 1076 | } |
---|
[154] | 1077 | else |
---|
[109] | 1078 | { |
---|
[154] | 1079 | void *target = getTarget(i); |
---|
| 1080 | *((ExtValue*)target) = x; |
---|
| 1081 | return PSET_CHANGED; |
---|
[109] | 1082 | } |
---|
| 1083 | } |
---|
| 1084 | |
---|
[154] | 1085 | void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret) |
---|
[109] | 1086 | { |
---|
[230] | 1087 | SANITY_CHECK(i); |
---|
[154] | 1088 | ParamEntry *pe = entry(i); |
---|
| 1089 | if (!pe) return; |
---|
| 1090 | if (pe->fun1 && (pe->type[0] == 'p')) |
---|
| 1091 | (*(void(*)(void*, ExtValue*, ExtValue*))pe->fun1)(object, args, ret); |
---|
| 1092 | else |
---|
[109] | 1093 | { |
---|
[375] | 1094 | logPrintf("SimpleAbstractParam", "call", LOG_ERROR, |
---|
[154] | 1095 | (*pe->type != 'p') ? "'%s.%s' is not a function" : "Internal error - undefined function pointer for '%s.%s'", getName(), pe->id); |
---|
[290] | 1096 | ret->setInvalid(); |
---|
[109] | 1097 | } |
---|
| 1098 | } |
---|
| 1099 | |
---|
[278] | 1100 | void SimpleAbstractParam::setDefault() |
---|
[109] | 1101 | { |
---|
[154] | 1102 | bool save = dontcheckchanges; |
---|
| 1103 | dontcheckchanges = 1; |
---|
[278] | 1104 | ParamInterface::setDefault(); |
---|
[154] | 1105 | dontcheckchanges = save; |
---|
[109] | 1106 | } |
---|
| 1107 | |
---|
[278] | 1108 | void SimpleAbstractParam::setDefault(int i) |
---|
[109] | 1109 | { |
---|
[154] | 1110 | bool save = dontcheckchanges; |
---|
| 1111 | dontcheckchanges = 1; |
---|
[278] | 1112 | ParamInterface::setDefault(i); |
---|
[154] | 1113 | dontcheckchanges = save; |
---|
[109] | 1114 | } |
---|
| 1115 | |
---|
[154] | 1116 | // Returns the address of the beginning of the line. |
---|
| 1117 | // len = line length (without \n). |
---|
| 1118 | // 0 may mean the line with length=0 or the end of the SString. |
---|
| 1119 | // poz is advanced to the beginning of the next line. |
---|
| 1120 | // A typical loop: for(poz=0;poz<s.d;) {line=getline(s,poz,len);... |
---|
| 1121 | static const char *getline(const SString &s, int &poz, int &len) |
---|
[109] | 1122 | { |
---|
[348] | 1123 | const char *beg = s.c_str() + poz; |
---|
| 1124 | if (poz >= s.len()) { poz = s.len(); len = 0; return s.c_str() + s.len(); } |
---|
[154] | 1125 | const char *lf = strchr(beg, '\n'); |
---|
[348] | 1126 | if (!lf) { lf = s.c_str() + s.len() - 1; poz = s.len(); } |
---|
| 1127 | else { poz = (int)(lf - s.c_str()) + 1; if (poz > s.len()) poz = s.len(); } |
---|
[154] | 1128 | while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break; |
---|
[247] | 1129 | len = (int)(lf - beg) + 1; |
---|
[154] | 1130 | return beg; |
---|
[109] | 1131 | } |
---|
| 1132 | |
---|
[154] | 1133 | int ParamInterface::load2(const SString &s, int &poz) |
---|
[109] | 1134 | { |
---|
[154] | 1135 | int i; // the index number of the parameter |
---|
| 1136 | int tmpi; |
---|
| 1137 | int len; |
---|
| 1138 | int ret; |
---|
| 1139 | int fields_loaded = 0; |
---|
| 1140 | const char *t, *lin, *end; |
---|
[320] | 1141 | const char *equals_sign, *field_end, *next_field; |
---|
[154] | 1142 | char remember; |
---|
| 1143 | const char *quote, *quote2; |
---|
| 1144 | const char *value, *valstop; |
---|
| 1145 | SString tmpvalue; |
---|
[650] | 1146 | bool parse_failed = false; |
---|
[154] | 1147 | if (poz >= s.len()) return fields_loaded; |
---|
[348] | 1148 | t = s.c_str() + poz; |
---|
[109] | 1149 | |
---|
[154] | 1150 | lin = getline(s, poz, len); // all fields must be encoded in a single line |
---|
| 1151 | if (!len) return fields_loaded; // empty line = end |
---|
| 1152 | i = 0; |
---|
| 1153 | end = lin + len; |
---|
| 1154 | while (t < end) |
---|
| 1155 | { |
---|
| 1156 | // processing a single field |
---|
[320] | 1157 | // "p:name=field_value, field_name=field_value , name=value..." |
---|
| 1158 | // ^ ^-t (after) ^ ^_next_field |
---|
| 1159 | // \_t (before) \_field_end |
---|
[325] | 1160 | while (isspace(*t)) if (t < end) t++; else return fields_loaded; |
---|
[109] | 1161 | |
---|
[320] | 1162 | field_end = strchrlimit(t, ',', end); if (!field_end) field_end = end; |
---|
[333] | 1163 | next_field = field_end; |
---|
[325] | 1164 | while ((field_end>t) && isblank(field_end[-1])) field_end--; |
---|
[320] | 1165 | quote = strchrlimit(t, '\"', field_end); |
---|
[154] | 1166 | if (quote) |
---|
[109] | 1167 | { |
---|
[154] | 1168 | quote2 = skipQuoteString(quote + 1, end); |
---|
[320] | 1169 | if (quote2 > field_end) |
---|
[154] | 1170 | { |
---|
[320] | 1171 | field_end = strchrlimit(quote2 + 1, ',', end); |
---|
[393] | 1172 | if (!field_end) field_end = end; |
---|
| 1173 | next_field = field_end; |
---|
[154] | 1174 | } |
---|
| 1175 | equals_sign = strchrlimit(t, '=', quote); |
---|
[109] | 1176 | } |
---|
[154] | 1177 | else |
---|
| 1178 | { |
---|
[320] | 1179 | equals_sign = strchrlimit(t, '=', field_end); |
---|
[154] | 1180 | quote2 = 0; |
---|
| 1181 | } |
---|
| 1182 | if (equals_sign == t) { t++; equals_sign = 0; } |
---|
[320] | 1183 | if (field_end == t) // skip empty value |
---|
[154] | 1184 | { |
---|
| 1185 | t++; i++; |
---|
| 1186 | continue; |
---|
| 1187 | } |
---|
| 1188 | if (equals_sign) // have parameter name |
---|
| 1189 | { |
---|
[247] | 1190 | tmpi = findIdn(t, (int)(equals_sign - t)); |
---|
[154] | 1191 | i = tmpi; |
---|
| 1192 | if (tmpi < 0) |
---|
[312] | 1193 | { |
---|
| 1194 | SString name(t, (int)(equals_sign - t)); |
---|
[375] | 1195 | logPrintf("Param", "load2", LOG_WARN, "Unknown property '%s' while reading object '%s' (ignored)", name.c_str(), getName()); |
---|
[312] | 1196 | } |
---|
[154] | 1197 | t = equals_sign + 1; // t=value |
---|
| 1198 | } |
---|
[109] | 1199 | #ifdef WARN_MISSING_NAME |
---|
[154] | 1200 | else |
---|
[109] | 1201 | #ifdef SAVE_SELECTED_NAMES |
---|
[650] | 1202 | if ((i >= getPropCount()) || !(flags(i)&PARAM_CANOMITNAME)) |
---|
[109] | 1203 | #endif |
---|
[154] | 1204 | { |
---|
[533] | 1205 | if (id(i)) |
---|
| 1206 | logPrintf("Param", "load2", LOG_WARN, "Missing property name in '%s' (assuming '%s')", getName(), id(i)); |
---|
| 1207 | else |
---|
| 1208 | logPrintf("Param", "load2", LOG_WARN, "Value after the last property of '%s'", getName()); |
---|
[154] | 1209 | } |
---|
[109] | 1210 | #endif |
---|
[154] | 1211 | if ((i >= 0) && id(i)) |
---|
[109] | 1212 | { |
---|
[154] | 1213 | value = t; |
---|
| 1214 | if (quote) |
---|
| 1215 | { |
---|
[247] | 1216 | tmpvalue.copyFrom(quote + 1, (int)(quote2 - quote) - 1); |
---|
[154] | 1217 | sstringUnquote(tmpvalue); |
---|
[348] | 1218 | value = tmpvalue.c_str(); |
---|
[154] | 1219 | valstop = quote2; |
---|
| 1220 | } |
---|
| 1221 | else |
---|
[320] | 1222 | if (field_end < end) valstop = field_end; else valstop = end; |
---|
[154] | 1223 | |
---|
| 1224 | remember = *valstop; |
---|
| 1225 | *(char*)valstop = 0; |
---|
[645] | 1226 | ret = set(i, value, true); |
---|
[154] | 1227 | fields_loaded++; |
---|
| 1228 | if (ret&(PSET_HITMAX | PSET_HITMIN)) |
---|
[375] | 1229 | logPrintf("Param", "load2", LOG_WARN, "Adjusted '%s' in '%s' (was too %s)", |
---|
[154] | 1230 | id(i), getName(), (ret&PSET_HITMAX) ? "big" : "small"); |
---|
[393] | 1231 | if (ret&PSET_PARSEFAILED) |
---|
[650] | 1232 | parse_failed = true; |
---|
[154] | 1233 | *(char*)valstop = remember; |
---|
[109] | 1234 | } |
---|
| 1235 | |
---|
[154] | 1236 | if (i >= 0) i++; |
---|
[109] | 1237 | #ifdef __CODEGUARD__ |
---|
[320] | 1238 | if (next_field<end-1) t=next_field+1; else return fields_loaded; |
---|
[109] | 1239 | #else |
---|
[320] | 1240 | t = next_field + 1; |
---|
[109] | 1241 | #endif |
---|
[154] | 1242 | } |
---|
[393] | 1243 | return fields_loaded | (parse_failed ? LOAD2_PARSE_FAILED : 0); |
---|
[109] | 1244 | } |
---|
| 1245 | |
---|
[154] | 1246 | int Param::grmember(int g, int a) |
---|
[109] | 1247 | { |
---|
[154] | 1248 | if ((getGroupCount() < 2) && (!g)) |
---|
| 1249 | return (a < getPropCount()) ? a : -9999; |
---|
[109] | 1250 | |
---|
[154] | 1251 | ParamEntry *e = entry(0); |
---|
| 1252 | int x = 0, i = 0; |
---|
| 1253 | for (; e->id; i++, e++) |
---|
[109] | 1254 | { |
---|
[154] | 1255 | if (e->group == g) |
---|
| 1256 | if (a == x) return i; else x++; |
---|
[109] | 1257 | } |
---|
[154] | 1258 | return -9999; |
---|
[109] | 1259 | } |
---|