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