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