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