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