[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 | |
---|
[743] | 106 | int ParamInterface::getMinMaxInt(int prop, paInt& minumum, paInt& maximum, paInt &def) |
---|
[109] | 107 | { |
---|
[743] | 108 | return getMinMaxIntFromTypeDef(type(prop), minumum, maximum, def); |
---|
[574] | 109 | } |
---|
| 110 | |
---|
[743] | 111 | int ParamInterface::getMinMaxDouble(int prop, double& minumum, double& maximum, double& def) |
---|
[574] | 112 | { |
---|
[743] | 113 | return getMinMaxDoubleFromTypeDef(type(prop), minumum, maximum, def); |
---|
[574] | 114 | } |
---|
| 115 | |
---|
[743] | 116 | int ParamInterface::getMinMaxString(int prop, int& minumum, int& maximum, SString& def) |
---|
[574] | 117 | { |
---|
[743] | 118 | return getMinMaxStringFromTypeDef(type(prop), minumum, maximum, def); |
---|
[574] | 119 | } |
---|
| 120 | |
---|
[743] | 121 | int ParamInterface::getMinMaxIntFromTypeDef(const char* t, paInt& minumum, paInt& maximum, paInt &def) |
---|
[574] | 122 | { |
---|
[154] | 123 | while (*t) if (*t == ' ') break; else t++; |
---|
[247] | 124 | return sscanf(t, PA_INT_SCANF " " PA_INT_SCANF " " PA_INT_SCANF, &minumum, &maximum, &def); |
---|
[109] | 125 | } |
---|
| 126 | |
---|
[743] | 127 | int ParamInterface::getMinMaxDoubleFromTypeDef(const char* t, double& minumum, double& maximum, double& def) |
---|
[109] | 128 | { |
---|
[154] | 129 | while (*t) if (*t == ' ') break; else t++; |
---|
| 130 | return sscanf(t, "%lg %lg %lg", &minumum, &maximum, &def); |
---|
[109] | 131 | } |
---|
| 132 | |
---|
[743] | 133 | int ParamInterface::getMinMaxStringFromTypeDef(const char* t, int& minumum, int& maximum, SString& def) |
---|
[253] | 134 | { |
---|
| 135 | while (*t) if (*t == ' ') break; else t++; |
---|
[312] | 136 | int ret = sscanf(t, "%d %d", &minumum, &maximum); |
---|
| 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 | |
---|
| 556 | int ParamInterface::loadMultiLine(VirtFILE* f, LoadOptions &options) |
---|
| 557 | { |
---|
[154] | 558 | SString buf; |
---|
| 559 | int i; |
---|
| 560 | const char *p, *p0; |
---|
| 561 | int p_len; |
---|
| 562 | bool loaded; |
---|
| 563 | int fields_loaded = 0; |
---|
[413] | 564 | int unexpected_line = 0; |
---|
[426] | 565 | vector<bool> seen; |
---|
| 566 | seen.resize(getPropCount()); |
---|
[650] | 567 | if ((i = findId("beforeLoad")) >= 0) |
---|
| 568 | call(i, NULL, NULL); |
---|
[720] | 569 | while (((!options.abortable) || (!*options.abortable)) && loadSStringLine(f, buf)) |
---|
[109] | 570 | { |
---|
[720] | 571 | if (options.linenum) (*options.linenum)++; |
---|
[348] | 572 | const char* t = buf.c_str(); |
---|
[413] | 573 | p0 = t; while (isblank(*p0)) p0++; |
---|
[154] | 574 | if (!*p0) break; |
---|
[413] | 575 | if (p0[0] == '#') { unexpected_line = 0; continue; } |
---|
| 576 | p = strchr(p0, ':'); |
---|
| 577 | if (!p) |
---|
[650] | 578 | { |
---|
| 579 | switch (unexpected_line) |
---|
[413] | 580 | { |
---|
[650] | 581 | case 0: |
---|
| 582 | logPrintf("ParamInterface", "load", LOG_WARN, "Ignored unexpected line %s while reading object '%s'", |
---|
[720] | 583 | options.linenum ? |
---|
| 584 | SString::sprintf("%d", *options.linenum).c_str() |
---|
[413] | 585 | : SString::sprintf("'%s'", p0).c_str(), |
---|
[650] | 586 | getName()); |
---|
| 587 | break; |
---|
| 588 | case 1: |
---|
| 589 | logPrintf("ParamInterface", "load", LOG_WARN, "The following line(s) were also unexpected and were ignored"); |
---|
| 590 | break; |
---|
| 591 | } |
---|
[413] | 592 | unexpected_line++; |
---|
| 593 | continue; |
---|
[650] | 594 | } |
---|
[413] | 595 | unexpected_line = 0; |
---|
[247] | 596 | p_len = (int)(p - p0); |
---|
[154] | 597 | loaded = false; |
---|
[268] | 598 | if (p_len && ((i = findIdn(p0, p_len)) >= 0)) |
---|
[109] | 599 | { |
---|
[426] | 600 | if (seen[i]) |
---|
[650] | 601 | { |
---|
| 602 | SString fileinfo; |
---|
| 603 | const char* fname = f->VgetPath(); |
---|
| 604 | if (fname != NULL) |
---|
[426] | 605 | { |
---|
[650] | 606 | fileinfo = SString::sprintf(" while reading from '%s'", fname); |
---|
[720] | 607 | if (options.linenum) |
---|
| 608 | fileinfo += SString::sprintf(" (line %d)", *options.linenum); |
---|
[426] | 609 | } |
---|
[796] | 610 | logPrintf("ParamInterface", "load", LOG_WARN, "Multiple '%s.%s' properties found%s", getName(), id(i), fileinfo.c_str()); |
---|
[650] | 611 | } |
---|
[426] | 612 | else |
---|
[650] | 613 | seen[i] = true; |
---|
[973] | 614 | if (!(flags(i) & PARAM_DONTLOAD)) |
---|
[109] | 615 | { |
---|
[312] | 616 | if (p0[p_len + 1] == '~') |
---|
| 617 | { |
---|
| 618 | SString s; |
---|
[650] | 619 | if (!readUntilTilde(f, s)) |
---|
| 620 | closingTildeError(this, f, i); |
---|
[333] | 621 | int lfcount = 1; |
---|
[348] | 622 | const char* tmp = s.c_str(); |
---|
[333] | 623 | while (tmp) |
---|
| 624 | if ((tmp = strchr(tmp, '\n'))) |
---|
| 625 | { |
---|
[822] | 626 | lfcount++; tmp++; |
---|
[333] | 627 | } |
---|
[312] | 628 | removeCR(s); |
---|
[523] | 629 | int ch; while ((ch = f->Vgetc()) != EOF) if (ch == '\n') break; |
---|
[312] | 630 | unquoteTilde(s); |
---|
[973] | 631 | if (options.linenum && (flags(i) & PARAM_LINECOMMENT)) |
---|
[720] | 632 | s = SString::sprintf("@file %s\n@line %d\n", f->VgetPath(), *options.linenum + 1) + s; |
---|
[743] | 633 | setFromString(i, s.c_str(), false); |
---|
[720] | 634 | if (options.linenum) |
---|
| 635 | (*options.linenum) += lfcount; |
---|
[312] | 636 | } |
---|
| 637 | else |
---|
| 638 | { |
---|
[883] | 639 | SString s = SString(p0 + p_len + 1); |
---|
| 640 | unquoteTilde(s); |
---|
| 641 | setFromString(i, s.c_str(), false); |
---|
[312] | 642 | } |
---|
| 643 | fields_loaded++; |
---|
| 644 | loaded = true; |
---|
[109] | 645 | } |
---|
| 646 | } |
---|
[720] | 647 | else if (options.warn_unknown_fields) |
---|
[312] | 648 | { |
---|
| 649 | SString name(p0, p_len); |
---|
[796] | 650 | logPrintf("ParamInterface", "load", LOG_WARN, "Ignored unknown property '%s.%s'", getName(), name.c_str()); |
---|
[312] | 651 | } |
---|
[268] | 652 | |
---|
[154] | 653 | if ((!loaded) && (p0[p_len + 1] == '~')) |
---|
[109] | 654 | { // eat unrecognized multiline field |
---|
[154] | 655 | SString s; |
---|
[650] | 656 | if (!readUntilTilde(f, s)) |
---|
| 657 | closingTildeError(this, f, -1); |
---|
[720] | 658 | if (options.linenum) |
---|
[333] | 659 | { |
---|
[348] | 660 | const char* tmp = s.c_str(); |
---|
[333] | 661 | int lfcount = 1; |
---|
| 662 | while (tmp) |
---|
| 663 | if ((tmp = strchr(tmp, '\n'))) |
---|
| 664 | { |
---|
[822] | 665 | lfcount++; tmp++; |
---|
[333] | 666 | } |
---|
[720] | 667 | (*options.linenum) += lfcount; |
---|
[333] | 668 | } |
---|
[523] | 669 | int ch; while ((ch = f->Vgetc()) != EOF) if (ch == '\n') break; |
---|
[109] | 670 | } |
---|
| 671 | } |
---|
[650] | 672 | if ((i = findId("afterLoad")) >= 0) |
---|
| 673 | call(i, NULL, NULL); |
---|
[154] | 674 | return fields_loaded; |
---|
[109] | 675 | } |
---|
| 676 | |
---|
| 677 | |
---|
| 678 | /* |
---|
| 679 | SString SimpleAbstractParam::getString(int i) |
---|
| 680 | { |
---|
| 681 | char *t; |
---|
| 682 | switch (*(t=type(i))) |
---|
[312] | 683 | { |
---|
[333] | 684 | case 'd': |
---|
| 685 | { |
---|
| 686 | for (i=atol(get(i));i>=0;i--) if (t) t=strchr(t+1,'~'); |
---|
| 687 | if (t) |
---|
| 688 | { |
---|
| 689 | t++; |
---|
| 690 | char *t2=strchr(t,'~'); |
---|
| 691 | if (!t2) t2=t+strlen(t); |
---|
| 692 | SString str; |
---|
| 693 | strncpy(str.directWrite(t2-t),t,t2-t); |
---|
| 694 | str.endWrite(t2-t); |
---|
| 695 | return str; |
---|
[312] | 696 | } |
---|
[333] | 697 | } |
---|
| 698 | } |
---|
[109] | 699 | return get(i); |
---|
| 700 | } |
---|
| 701 | */ |
---|
| 702 | |
---|
| 703 | int ParamInterface::findId(const char* n) |
---|
| 704 | { |
---|
[154] | 705 | int i; const char *p; |
---|
| 706 | for (i = 0; p = id(i); i++) if (!strcmp(n, p)) return i; |
---|
| 707 | return -1; |
---|
[109] | 708 | } |
---|
| 709 | |
---|
[154] | 710 | int ParamInterface::findIdn(const char* naz, int n) |
---|
[109] | 711 | { |
---|
[154] | 712 | int i; const char *p; |
---|
| 713 | for (i = 0; p = id(i); i++) if ((!strncmp(naz, p, n)) && (!p[n])) return i; |
---|
| 714 | return -1; |
---|
[109] | 715 | } |
---|
| 716 | |
---|
[1155] | 717 | int ParamInterface::findGroupId(const char* name) |
---|
| 718 | { |
---|
[1253] | 719 | for (int i = 0; i < getGroupCount(); i++) |
---|
| 720 | if (!strcmp(grname(i), name)) |
---|
| 721 | return i; |
---|
| 722 | return -1; |
---|
[1155] | 723 | } |
---|
| 724 | |
---|
[154] | 725 | void ParamInterface::get(int i, ExtValue &ret) |
---|
[109] | 726 | { |
---|
[154] | 727 | switch (type(i)[0]) |
---|
[109] | 728 | { |
---|
| 729 | case 'd': ret.setInt(getInt(i)); break; |
---|
| 730 | case 'f': ret.setDouble(getDouble(i)); break; |
---|
| 731 | case 's': ret.setString(getString(i)); break; |
---|
| 732 | case 'o': ret.setObject(getObject(i)); break; |
---|
[154] | 733 | case 'x': ret = getExtValue(i); break; |
---|
[950] | 734 | default: logPrintf("ParamInterface", "get", LOG_ERROR, "%s is not a property", nameDotPropertyForMessages(i).c_str()); |
---|
[109] | 735 | } |
---|
| 736 | } |
---|
| 737 | |
---|
[743] | 738 | int ParamInterface::setIntFromString(int i, const char* str, bool strict) |
---|
[109] | 739 | { |
---|
[326] | 740 | paInt value; |
---|
[645] | 741 | if (!ExtValue::parseInt(str, value, strict, true)) |
---|
[109] | 742 | { |
---|
[314] | 743 | paInt mn, mx, def; |
---|
[743] | 744 | if (getMinMaxInt(i, mn, mx, def) >= 3) |
---|
[393] | 745 | return setInt(i, def) | PSET_PARSEFAILED; |
---|
[154] | 746 | else |
---|
[393] | 747 | return setInt(i, (paInt)0) | PSET_PARSEFAILED; |
---|
[154] | 748 | } |
---|
[109] | 749 | else |
---|
[326] | 750 | return setInt(i, value); |
---|
[109] | 751 | } |
---|
| 752 | |
---|
[743] | 753 | int ParamInterface::setDoubleFromString(int i, const char* str) |
---|
[109] | 754 | { |
---|
[326] | 755 | double value; |
---|
[333] | 756 | if (!ExtValue::parseDouble(str, value, true)) |
---|
[109] | 757 | { |
---|
[314] | 758 | double mn, mx, def; |
---|
[743] | 759 | if (getMinMaxDouble(i, mn, mx, def) >= 3) |
---|
[393] | 760 | return setDouble(i, def) | PSET_PARSEFAILED; |
---|
[154] | 761 | else |
---|
[393] | 762 | return setDouble(i, (double)0) | PSET_PARSEFAILED; |
---|
[154] | 763 | } |
---|
[109] | 764 | else |
---|
[326] | 765 | return setDouble(i, value); |
---|
[109] | 766 | } |
---|
| 767 | |
---|
[154] | 768 | int ParamInterface::set(int i, const ExtValue &v) |
---|
[109] | 769 | { |
---|
[154] | 770 | switch (type(i)[0]) |
---|
[109] | 771 | { |
---|
[144] | 772 | case 'd': |
---|
[154] | 773 | if ((v.type == TInt) || (v.type == TDouble)) return setInt(i, v.getInt()); |
---|
[144] | 774 | else |
---|
[154] | 775 | { |
---|
| 776 | if (v.type == TObj) |
---|
[333] | 777 | { |
---|
[950] | 778 | logPrintf("ParamInterface", "set", LOG_ERROR, "Setting int %s from object reference (%s)", nameDotPropertyForMessages(i).c_str(), v.getString().c_str()); |
---|
[333] | 779 | return 0; |
---|
| 780 | } |
---|
| 781 | else |
---|
[743] | 782 | return setIntFromString(i, v.getString().c_str(), false); |
---|
[154] | 783 | } |
---|
[144] | 784 | case 'f': |
---|
[154] | 785 | if ((v.type == TInt) || (v.type == TDouble)) return setDouble(i, v.getDouble()); |
---|
[144] | 786 | else |
---|
[154] | 787 | { |
---|
| 788 | if (v.type == TObj) |
---|
[333] | 789 | { |
---|
[950] | 790 | logPrintf("ParamInterface", "set", LOG_ERROR, "Setting float %s from object reference (%s)", nameDotPropertyForMessages(i).c_str(), v.getString().c_str()); |
---|
[333] | 791 | return 0; |
---|
| 792 | } |
---|
| 793 | else |
---|
[743] | 794 | return setDoubleFromString(i, v.getString().c_str()); |
---|
[154] | 795 | } |
---|
| 796 | case 's': { SString t = v.getString(); return setString(i, t); } |
---|
[478] | 797 | case 'o': |
---|
[650] | 798 | if ((v.type != TUnknown) && (v.type != TObj)) |
---|
[950] | 799 | logPrintf("ParamInterface", "set", LOG_ERROR, "Setting object %s from %s", nameDotPropertyForMessages(i).c_str(), v.typeAndValue().c_str()); |
---|
[478] | 800 | else |
---|
| 801 | return setObject(i, v.getObject()); |
---|
| 802 | break; |
---|
[154] | 803 | case 'x': return setExtValue(i, v); |
---|
[950] | 804 | default: logPrintf("ParamInterface", "set", LOG_ERROR, "%s is not a property", nameDotPropertyForMessages(i).c_str()); |
---|
[109] | 805 | } |
---|
[154] | 806 | return 0; |
---|
[109] | 807 | } |
---|
| 808 | |
---|
[743] | 809 | int ParamInterface::setFromString(int i, const char *v, bool strict) |
---|
[109] | 810 | { |
---|
[312] | 811 | char typ = type(i)[0]; |
---|
[273] | 812 | switch (typ) |
---|
[109] | 813 | { |
---|
[743] | 814 | case 'd': return setIntFromString(i, v, strict); |
---|
| 815 | case 'f': return setDoubleFromString(i, v); |
---|
[154] | 816 | case 's': { SString t(v); return setString(i, t); } |
---|
[273] | 817 | case 'x': case 'o': |
---|
[109] | 818 | { |
---|
[154] | 819 | ExtValue e; |
---|
| 820 | const char* after; |
---|
| 821 | if (!strncmp(v, SERIALIZATION_PREFIX, strlen(SERIALIZATION_PREFIX))) |
---|
[109] | 822 | { |
---|
[154] | 823 | after = e.deserialize(v + strlen(SERIALIZATION_PREFIX)); |
---|
| 824 | if ((after == NULL) || (*after)) |
---|
[343] | 825 | { |
---|
[478] | 826 | logPrintf("ParamInterface", "set", LOG_ERROR, "serialization format mismatch in %s.%s", (getName() ? getName() : "<Unknown>"), id(i)); |
---|
[334] | 827 | e.setEmpty(); |
---|
[343] | 828 | } |
---|
[109] | 829 | } |
---|
[154] | 830 | else if ((after = e.parseNumber(v)) && (*after == 0)) //consumed the whole string |
---|
[109] | 831 | { |
---|
[154] | 832 | //OK! |
---|
[109] | 833 | } |
---|
[154] | 834 | else |
---|
[109] | 835 | { |
---|
[154] | 836 | e.setString(SString(v)); |
---|
[109] | 837 | } |
---|
[312] | 838 | if (typ == 'x') |
---|
[273] | 839 | return setExtValue(i, e); |
---|
| 840 | else |
---|
| 841 | return setObject(i, e.getObject()); |
---|
[109] | 842 | } |
---|
| 843 | } |
---|
[154] | 844 | return 0; |
---|
[109] | 845 | } |
---|
| 846 | |
---|
[704] | 847 | SString ParamInterface::getText(int i) //find the current enum text or call get(i) if not enum |
---|
[109] | 848 | { |
---|
[154] | 849 | const char *t; |
---|
[720] | 850 | if (((*(t = type(i))) == 'd') && (strchr(t, '~') != NULL)) //type is int and contains enum labels |
---|
[109] | 851 | { |
---|
[314] | 852 | paInt mn, mx, def; |
---|
[312] | 853 | int value = getInt(i); |
---|
[743] | 854 | if (getMinMaxIntFromTypeDef(t, mn, mx, def) >= 2) |
---|
[312] | 855 | { |
---|
[314] | 856 | if (value > mx) |
---|
[704] | 857 | return get(i);//unexpected value of out bounds (should never happen) -> fallback |
---|
[314] | 858 | value -= mn; |
---|
[312] | 859 | } |
---|
[704] | 860 | if (value < 0) return get(i); //unexpected value of out bounds (should never happen) -> fallback |
---|
| 861 | // now value is 0-based index of ~text |
---|
| 862 | for (; value >= 0; value--) if (t) t = strchr(t + 1, '~'); else break; |
---|
| 863 | if (t) // found n-th ~text in type description (else: not enough ~texts in type description) |
---|
[109] | 864 | { |
---|
[154] | 865 | t++; |
---|
| 866 | const char *t2 = strchr(t, '~'); |
---|
| 867 | if (!t2) t2 = t + strlen(t); |
---|
[247] | 868 | return SString(t, (int)(t2 - t)); |
---|
[109] | 869 | } |
---|
| 870 | } |
---|
[704] | 871 | return get(i); //fallback - return int value as string |
---|
[109] | 872 | } |
---|
| 873 | |
---|
| 874 | SString ParamInterface::get(int i) |
---|
| 875 | { |
---|
[154] | 876 | switch (type(i)[0]) |
---|
[109] | 877 | { |
---|
| 878 | case 'd': return SString::valueOf(getInt(i)); |
---|
| 879 | case 'f': return SString::valueOf(getDouble(i)); |
---|
| 880 | case 's': return getString(i); |
---|
| 881 | } |
---|
[154] | 882 | ExtValue v; |
---|
| 883 | get(i, v); |
---|
| 884 | return v.getString(); |
---|
[109] | 885 | } |
---|
| 886 | |
---|
[535] | 887 | bool ParamInterface::isValidTypeDescription(const char* t) |
---|
| 888 | { |
---|
[650] | 889 | if (t == NULL) return false; |
---|
| 890 | if (*t == 0) return false; |
---|
| 891 | if (strchr("dfsoxp", *t) == NULL) return false; |
---|
| 892 | switch (*t) |
---|
[574] | 893 | { |
---|
| 894 | case 'd': |
---|
[754] | 895 | { |
---|
| 896 | paInt a, b, c; |
---|
| 897 | int have = getMinMaxIntFromTypeDef(t, a, b, c); |
---|
| 898 | if (have == 1) return false; |
---|
| 899 | if ((have >= 2) && (b < a) && (a != 0) && (b != -1)) return false; // max<min meaning 'undefined' is only allowed as "d 0 -1" |
---|
| 900 | } |
---|
[822] | 901 | break; |
---|
[574] | 902 | case 'f': |
---|
[754] | 903 | { |
---|
| 904 | double a, b, c; |
---|
| 905 | int have = getMinMaxDoubleFromTypeDef(t, a, b, c); |
---|
| 906 | if (have == 1) return false; |
---|
| 907 | if ((have >= 2) && (b < a) && (a != 0) && (b != -1)) return false; // max<min meaning 'undefined' is only allowed as "f 0 -1" |
---|
| 908 | } |
---|
[822] | 909 | break; |
---|
[754] | 910 | case 's': |
---|
| 911 | { |
---|
| 912 | int a, b; SString c; |
---|
| 913 | int have = getMinMaxStringFromTypeDef(t, a, b, c); |
---|
| 914 | //if (have == 1) return false; //not sure? |
---|
| 915 | if ((have >= 1) && (!((a == 0) || (a == 1)))) return false; // 'min' for string (single/multi) can be only 0 or 1 |
---|
[1186] | 916 | if ((have >= 2) && (b < -1)) return false; // max=-1 means unlimited, >=0 is max len, max<-1 is not allowed |
---|
[574] | 917 | } |
---|
[822] | 918 | break; |
---|
[754] | 919 | } |
---|
[650] | 920 | return true; |
---|
[535] | 921 | } |
---|
[109] | 922 | |
---|
[743] | 923 | SString ParamInterface::friendlyTypeDescrFromTypeDef(const char* type) |
---|
[640] | 924 | { |
---|
[650] | 925 | SString t; |
---|
| 926 | switch (type[0]) |
---|
[640] | 927 | { |
---|
[650] | 928 | case 'd': t += "integer"; |
---|
[743] | 929 | {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] | 930 | break; |
---|
[650] | 931 | case 'f': t += "float"; |
---|
[743] | 932 | {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] | 933 | break; |
---|
[650] | 934 | case 's': t += "string"; |
---|
[743] | 935 | {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] | 936 | break; |
---|
[650] | 937 | case 'x': t += "untyped value"; break; |
---|
| 938 | case 'p': t += "function"; break; |
---|
| 939 | case 'o': t += "object"; if (type[1]) { t += " of class "; t += type + 1; } break; |
---|
[640] | 940 | default: return "unknown type"; |
---|
| 941 | } |
---|
[650] | 942 | return t; |
---|
[640] | 943 | } |
---|
| 944 | |
---|
[109] | 945 | //////////////////////////////// PARAM //////////////////////////////////// |
---|
| 946 | |
---|
[483] | 947 | #ifdef _DEBUG |
---|
[230] | 948 | void SimpleAbstractParam::sanityCheck(int i) |
---|
| 949 | { |
---|
[650] | 950 | ParamEntry *pe = entry(i); |
---|
[230] | 951 | |
---|
[650] | 952 | const char* t = pe->type; |
---|
| 953 | const char* err = NULL; |
---|
[230] | 954 | |
---|
[535] | 955 | if (!isValidTypeDescription(t)) |
---|
[650] | 956 | err = "invalid type description"; |
---|
| 957 | if (*t == 'p') |
---|
[230] | 958 | { |
---|
[650] | 959 | if (pe->fun1 == NULL) |
---|
[822] | 960 | { |
---|
| 961 | MutableParamInterface *mpi = dynamic_cast<MutableParamInterface*>(this); |
---|
| 962 | 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. |
---|
| 963 | err = "no procedure defined"; |
---|
| 964 | } |
---|
[230] | 965 | } |
---|
[312] | 966 | else |
---|
[230] | 967 | { |
---|
[732] | 968 | if ((t[0] == 'o') && (t[1] == ' ')) |
---|
| 969 | { |
---|
| 970 | err = "space after 'o'"; |
---|
| 971 | } |
---|
[659] | 972 | if (!(pe->flags & (PARAM_READONLY | PARAM_DONTSAVE | PARAM_USERREADONLY | PARAM_CONST | PARAM_DONTLOAD | PARAM_LINECOMMENT | PARAM_OBJECTSET))) |
---|
[230] | 973 | { //write access |
---|
[650] | 974 | if ((pe->fun2 == NULL) && (pe->offset == PARAM_ILLEGAL_OFFSET)) |
---|
| 975 | err = "no field defined (GETONLY without PARAM_READONLY?)"; |
---|
[230] | 976 | } |
---|
| 977 | } |
---|
[650] | 978 | if (err != NULL) |
---|
| 979 | logPrintf("SimpleAbstractParam", "sanityCheck", LOG_ERROR, |
---|
[973] | 980 | "Invalid ParamEntry for %s (%s)", nameDotPropertyForMessages(i).c_str(), err); |
---|
[650] | 981 | } |
---|
[230] | 982 | #endif |
---|
| 983 | |
---|
[109] | 984 | void *SimpleAbstractParam::getTarget(int i) |
---|
| 985 | { |
---|
[154] | 986 | return (void*)(((char*)object) + entry(i)->offset); |
---|
| 987 | //return &(object->*(entry(i)->fldptr)); |
---|
[109] | 988 | } |
---|
| 989 | |
---|
| 990 | ///////// get |
---|
| 991 | |
---|
[483] | 992 | #ifdef _DEBUG |
---|
[230] | 993 | #define SANITY_CHECK(i) sanityCheck(i) |
---|
| 994 | #else |
---|
| 995 | #define SANITY_CHECK(i) |
---|
| 996 | #endif |
---|
| 997 | |
---|
[247] | 998 | paInt SimpleAbstractParam::getInt(int i) |
---|
[109] | 999 | { |
---|
[230] | 1000 | SANITY_CHECK(i); |
---|
[154] | 1001 | ExtValue v; |
---|
| 1002 | ParamEntry *pe = entry(i); |
---|
| 1003 | if (pe->fun1) |
---|
[109] | 1004 | { |
---|
[154] | 1005 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 1006 | return v.getInt(); |
---|
[109] | 1007 | } |
---|
[154] | 1008 | else |
---|
[109] | 1009 | { |
---|
[154] | 1010 | void *target = getTarget(i); |
---|
[247] | 1011 | return *((paInt*)target); |
---|
[109] | 1012 | } |
---|
| 1013 | } |
---|
| 1014 | |
---|
| 1015 | double SimpleAbstractParam::getDouble(int i) |
---|
| 1016 | { |
---|
[230] | 1017 | SANITY_CHECK(i); |
---|
[154] | 1018 | ExtValue v; |
---|
| 1019 | ParamEntry *pe = entry(i); |
---|
| 1020 | if (pe->fun1) |
---|
[109] | 1021 | { |
---|
[154] | 1022 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 1023 | return v.getDouble(); |
---|
[109] | 1024 | } |
---|
[154] | 1025 | else |
---|
[109] | 1026 | { |
---|
[154] | 1027 | void *target = getTarget(i); |
---|
| 1028 | return *((double*)target); |
---|
[109] | 1029 | } |
---|
| 1030 | } |
---|
| 1031 | |
---|
| 1032 | SString SimpleAbstractParam::getString(int i) |
---|
| 1033 | { |
---|
[230] | 1034 | SANITY_CHECK(i); |
---|
[154] | 1035 | ExtValue v; |
---|
| 1036 | ParamEntry *pe = entry(i); |
---|
| 1037 | if (pe->fun1) |
---|
[109] | 1038 | { |
---|
[154] | 1039 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 1040 | return v.getString(); |
---|
[109] | 1041 | } |
---|
[154] | 1042 | else |
---|
[109] | 1043 | { |
---|
[154] | 1044 | void *target = getTarget(i); |
---|
| 1045 | return *((SString*)target); |
---|
[109] | 1046 | } |
---|
| 1047 | } |
---|
| 1048 | |
---|
| 1049 | ExtObject SimpleAbstractParam::getObject(int i) |
---|
| 1050 | { |
---|
[230] | 1051 | SANITY_CHECK(i); |
---|
[154] | 1052 | ExtValue v; |
---|
| 1053 | ParamEntry *pe = entry(i); |
---|
| 1054 | if (pe->fun1) |
---|
[109] | 1055 | { |
---|
[154] | 1056 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 1057 | return v.getObject(); |
---|
[109] | 1058 | } |
---|
[154] | 1059 | else |
---|
[109] | 1060 | { |
---|
[154] | 1061 | void *target = getTarget(i); |
---|
| 1062 | return *((ExtObject*)target); |
---|
[109] | 1063 | } |
---|
| 1064 | } |
---|
| 1065 | |
---|
| 1066 | ExtValue SimpleAbstractParam::getExtValue(int i) |
---|
| 1067 | { |
---|
[230] | 1068 | SANITY_CHECK(i); |
---|
[154] | 1069 | ExtValue v; |
---|
| 1070 | ParamEntry *pe = entry(i); |
---|
| 1071 | if (pe->fun1) |
---|
[109] | 1072 | { |
---|
[154] | 1073 | (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v); |
---|
| 1074 | return v; |
---|
[109] | 1075 | } |
---|
[154] | 1076 | else |
---|
[109] | 1077 | { |
---|
[154] | 1078 | void *target = getTarget(i); |
---|
| 1079 | return *((ExtValue*)target); |
---|
[109] | 1080 | } |
---|
| 1081 | } |
---|
| 1082 | |
---|
| 1083 | |
---|
| 1084 | //////// set |
---|
| 1085 | |
---|
[247] | 1086 | int SimpleAbstractParam::setInt(int i, paInt x) |
---|
[109] | 1087 | { |
---|
[230] | 1088 | SANITY_CHECK(i); |
---|
[154] | 1089 | ExtValue v; |
---|
| 1090 | ParamEntry *pe = entry(i); |
---|
[973] | 1091 | if (pe->flags & PARAM_READONLY) return PSET_RONLY; |
---|
[247] | 1092 | paInt xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
[574] | 1093 | paInt mn = 0, mx = 0, de = 0; |
---|
[154] | 1094 | int result = 0; |
---|
[743] | 1095 | if (getMinMaxIntFromTypeDef(pe->type, mn, mx, de) >= 2) |
---|
[316] | 1096 | if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking |
---|
[109] | 1097 | { |
---|
[822] | 1098 | if (x < mn) { x = mn; result = PSET_HITMIN; } |
---|
| 1099 | else if (x > mx) { x = mx; result = PSET_HITMAX; } |
---|
[109] | 1100 | } |
---|
| 1101 | |
---|
[154] | 1102 | if (pe->fun2) |
---|
[109] | 1103 | { |
---|
[154] | 1104 | v.setInt(x); |
---|
| 1105 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
[109] | 1106 | } |
---|
[154] | 1107 | else |
---|
[109] | 1108 | { |
---|
[154] | 1109 | void *target = getTarget(i); |
---|
[247] | 1110 | if (dontcheckchanges || (*((paInt*)target) != x)) |
---|
[154] | 1111 | { |
---|
[109] | 1112 | result |= PSET_CHANGED; |
---|
[247] | 1113 | *((paInt*)target) = x; |
---|
[154] | 1114 | } |
---|
[109] | 1115 | } |
---|
[805] | 1116 | ::messageOnExceedRange(this, i, result, xcopy); |
---|
[109] | 1117 | return result; |
---|
| 1118 | } |
---|
| 1119 | |
---|
[154] | 1120 | int SimpleAbstractParam::setDouble(int i, double x) |
---|
[109] | 1121 | { |
---|
[230] | 1122 | SANITY_CHECK(i); |
---|
[154] | 1123 | ExtValue v; |
---|
| 1124 | ParamEntry *pe = entry(i); |
---|
[973] | 1125 | if (pe->flags & PARAM_READONLY) return PSET_RONLY; |
---|
[154] | 1126 | double xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
[574] | 1127 | double mn = 0, mx = 0, de = 0; |
---|
[154] | 1128 | int result = 0; |
---|
[743] | 1129 | if (getMinMaxDoubleFromTypeDef(pe->type, mn, mx, de) >= 2) |
---|
[316] | 1130 | if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking |
---|
[109] | 1131 | { |
---|
[822] | 1132 | if (x < mn) { x = mn; result = PSET_HITMIN; } |
---|
| 1133 | else if (x > mx) { x = mx; result = PSET_HITMAX; } |
---|
[1253] | 1134 | 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] | 1135 | } |
---|
| 1136 | |
---|
[154] | 1137 | if (pe->fun2) |
---|
[109] | 1138 | { |
---|
[154] | 1139 | v.setDouble(x); |
---|
| 1140 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
[109] | 1141 | } |
---|
[154] | 1142 | else |
---|
[109] | 1143 | { |
---|
[154] | 1144 | void *target = getTarget(i); |
---|
| 1145 | if (dontcheckchanges || (*((double*)target) != x)) |
---|
[109] | 1146 | { |
---|
[154] | 1147 | result |= PSET_CHANGED; |
---|
| 1148 | *((double*)target) = x; |
---|
[109] | 1149 | } |
---|
| 1150 | } |
---|
[805] | 1151 | ::messageOnExceedRange(this, i, result, xcopy); |
---|
[109] | 1152 | return result; |
---|
| 1153 | } |
---|
| 1154 | |
---|
[154] | 1155 | int SimpleAbstractParam::setString(int i, const SString& x) |
---|
[109] | 1156 | { |
---|
[230] | 1157 | SANITY_CHECK(i); |
---|
[154] | 1158 | ExtValue v; |
---|
| 1159 | SString vs; |
---|
| 1160 | const SString *xx = &x; |
---|
| 1161 | ParamEntry *pe = entry(i); |
---|
[973] | 1162 | if (pe->flags & PARAM_READONLY) return PSET_RONLY; |
---|
[154] | 1163 | SString xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
| 1164 | const char* t = pe->type + 1; |
---|
| 1165 | while (*t) if (*t == ' ') break; else t++; |
---|
[314] | 1166 | int mn = 0, mx = 0; |
---|
[154] | 1167 | int result = 0; |
---|
[314] | 1168 | if (sscanf(t, "%d %d", &mn, &mx) == 2) //using getMinMax would also get default value, which is not needed here |
---|
[109] | 1169 | { |
---|
[1186] | 1170 | if ((x.length() > mx) && (mx >= 0)) |
---|
[109] | 1171 | { |
---|
[314] | 1172 | vs = x.substr(0, mx); |
---|
[154] | 1173 | xx = &vs; |
---|
| 1174 | result |= PSET_HITMAX; |
---|
[109] | 1175 | } |
---|
| 1176 | } |
---|
| 1177 | |
---|
[154] | 1178 | if (pe->fun2) |
---|
[109] | 1179 | { |
---|
[154] | 1180 | v.setString(*xx); |
---|
| 1181 | result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
[109] | 1182 | } |
---|
[154] | 1183 | else |
---|
[109] | 1184 | { |
---|
[154] | 1185 | void *target = getTarget(i); |
---|
| 1186 | if (dontcheckchanges || (!(*((SString*)target) == *xx))) |
---|
[109] | 1187 | { |
---|
[154] | 1188 | result |= PSET_CHANGED; |
---|
[306] | 1189 | *((SString*)target) = *xx; |
---|
[109] | 1190 | } |
---|
| 1191 | } |
---|
[805] | 1192 | ::messageOnExceedRange(this, i, result, xcopy); |
---|
[109] | 1193 | return result; |
---|
| 1194 | } |
---|
| 1195 | |
---|
[154] | 1196 | int SimpleAbstractParam::setObject(int i, const ExtObject& x) |
---|
[109] | 1197 | { |
---|
[230] | 1198 | SANITY_CHECK(i); |
---|
[154] | 1199 | ExtValue v; |
---|
| 1200 | ParamEntry *pe = entry(i); |
---|
[973] | 1201 | if (pe->flags & PARAM_READONLY) return PSET_RONLY; |
---|
| 1202 | if (pe->flags & PARAM_OBJECTSET) |
---|
[650] | 1203 | { |
---|
| 1204 | ExtObject o = getObject(i); |
---|
[478] | 1205 | Param tmp; |
---|
[650] | 1206 | ParamInterface* oif = o.getParamInterface(tmp); |
---|
[478] | 1207 | int ass; |
---|
[650] | 1208 | if (oif && ((ass = oif->findId("assign")) >= 0)) |
---|
| 1209 | { |
---|
| 1210 | ExtValue arg = x; |
---|
| 1211 | oif->call(ass, &arg, &v); |
---|
| 1212 | } |
---|
[478] | 1213 | else |
---|
| 1214 | logPrintf("SimpleAbstractParam", "setObject", LOG_ERROR, |
---|
[950] | 1215 | "%s is PARAM_OBJECTSET but no 'assign()' in %s", nameDotPropertyForMessages(i).c_str(), o.interfaceName()); |
---|
[478] | 1216 | return PSET_CHANGED; |
---|
[650] | 1217 | } |
---|
[154] | 1218 | ExtObject xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
| 1219 | if (pe->fun2) |
---|
[109] | 1220 | { |
---|
[154] | 1221 | v.setObject(x); |
---|
| 1222 | int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v); |
---|
[805] | 1223 | ::messageOnExceedRange(this, i, result, xcopy); |
---|
[154] | 1224 | return result; |
---|
[109] | 1225 | } |
---|
[154] | 1226 | else |
---|
[109] | 1227 | { |
---|
[154] | 1228 | void *target = getTarget(i); |
---|
| 1229 | *((ExtObject*)target) = x; |
---|
| 1230 | return PSET_CHANGED; |
---|
[109] | 1231 | } |
---|
| 1232 | } |
---|
| 1233 | |
---|
[154] | 1234 | int SimpleAbstractParam::setExtValue(int i, const ExtValue& x) |
---|
[109] | 1235 | { |
---|
[230] | 1236 | SANITY_CHECK(i); |
---|
[154] | 1237 | ParamEntry *pe = entry(i); |
---|
[973] | 1238 | if (pe->flags & PARAM_READONLY) return PSET_RONLY; |
---|
[154] | 1239 | ExtValue xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below |
---|
| 1240 | if (pe->fun2) |
---|
[109] | 1241 | { |
---|
[154] | 1242 | int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x); |
---|
[805] | 1243 | ::messageOnExceedRange(this, i, result, xcopy); |
---|
[154] | 1244 | return result; |
---|
[109] | 1245 | } |
---|
[154] | 1246 | else |
---|
[109] | 1247 | { |
---|
[154] | 1248 | void *target = getTarget(i); |
---|
| 1249 | *((ExtValue*)target) = x; |
---|
| 1250 | return PSET_CHANGED; |
---|
[109] | 1251 | } |
---|
| 1252 | } |
---|
| 1253 | |
---|
[154] | 1254 | void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret) |
---|
[109] | 1255 | { |
---|
[230] | 1256 | SANITY_CHECK(i); |
---|
[154] | 1257 | ParamEntry *pe = entry(i); |
---|
| 1258 | if (!pe) return; |
---|
| 1259 | if (pe->fun1 && (pe->type[0] == 'p')) |
---|
| 1260 | (*(void(*)(void*, ExtValue*, ExtValue*))pe->fun1)(object, args, ret); |
---|
| 1261 | else |
---|
[109] | 1262 | { |
---|
[375] | 1263 | logPrintf("SimpleAbstractParam", "call", LOG_ERROR, |
---|
[950] | 1264 | (*pe->type != 'p') ? "%s is not a function" : "Internal error - undefined function pointer for %s", nameDotPropertyForMessages(i).c_str()); |
---|
[290] | 1265 | ret->setInvalid(); |
---|
[109] | 1266 | } |
---|
| 1267 | } |
---|
| 1268 | |
---|
[278] | 1269 | void SimpleAbstractParam::setDefault() |
---|
[109] | 1270 | { |
---|
[154] | 1271 | bool save = dontcheckchanges; |
---|
| 1272 | dontcheckchanges = 1; |
---|
[278] | 1273 | ParamInterface::setDefault(); |
---|
[154] | 1274 | dontcheckchanges = save; |
---|
[109] | 1275 | } |
---|
| 1276 | |
---|
[278] | 1277 | void SimpleAbstractParam::setDefault(int i) |
---|
[109] | 1278 | { |
---|
[154] | 1279 | bool save = dontcheckchanges; |
---|
| 1280 | dontcheckchanges = 1; |
---|
[278] | 1281 | ParamInterface::setDefault(i); |
---|
[154] | 1282 | dontcheckchanges = save; |
---|
[109] | 1283 | } |
---|
| 1284 | |
---|
[154] | 1285 | // Returns the address of the beginning of the line. |
---|
| 1286 | // len = line length (without \n). |
---|
| 1287 | // 0 may mean the line with length=0 or the end of the SString. |
---|
| 1288 | // poz is advanced to the beginning of the next line. |
---|
| 1289 | // A typical loop: for(poz=0;poz<s.d;) {line=getline(s,poz,len);... |
---|
| 1290 | static const char *getline(const SString &s, int &poz, int &len) |
---|
[109] | 1291 | { |
---|
[348] | 1292 | const char *beg = s.c_str() + poz; |
---|
[973] | 1293 | if (poz >= s.length()) { poz = s.length(); len = 0; return s.c_str() + s.length(); } |
---|
[154] | 1294 | const char *lf = strchr(beg, '\n'); |
---|
[973] | 1295 | if (!lf) { lf = s.c_str() + s.length() - 1; poz = s.length(); } |
---|
| 1296 | else { poz = (int)(lf - s.c_str()) + 1; if (poz > s.length()) poz = s.length(); } |
---|
[154] | 1297 | while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break; |
---|
[247] | 1298 | len = (int)(lf - beg) + 1; |
---|
[154] | 1299 | return beg; |
---|
[109] | 1300 | } |
---|
| 1301 | |
---|
[720] | 1302 | int ParamInterface::loadSingleLine(const SString &s, LoadOptions &options) |
---|
[109] | 1303 | { |
---|
[154] | 1304 | int i; // the index number of the parameter |
---|
| 1305 | int tmpi; |
---|
| 1306 | int len; |
---|
| 1307 | int ret; |
---|
| 1308 | int fields_loaded = 0; |
---|
| 1309 | const char *t, *lin, *end; |
---|
[320] | 1310 | const char *equals_sign, *field_end, *next_field; |
---|
[154] | 1311 | char remember; |
---|
| 1312 | const char *quote, *quote2; |
---|
| 1313 | const char *value, *valstop; |
---|
| 1314 | SString tmpvalue; |
---|
[650] | 1315 | bool parse_failed = false; |
---|
[973] | 1316 | if (options.offset >= s.length()) return fields_loaded; |
---|
[720] | 1317 | t = s.c_str() + options.offset; |
---|
[109] | 1318 | |
---|
[720] | 1319 | lin = getline(s, options.offset, len); // all fields must be encoded in a single line |
---|
[154] | 1320 | if (!len) return fields_loaded; // empty line = end |
---|
| 1321 | i = 0; |
---|
| 1322 | end = lin + len; |
---|
| 1323 | while (t < end) |
---|
| 1324 | { |
---|
| 1325 | // processing a single field |
---|
[320] | 1326 | // "p:name=field_value, field_name=field_value , name=value..." |
---|
| 1327 | // ^ ^-t (after) ^ ^_next_field |
---|
| 1328 | // \_t (before) \_field_end |
---|
[325] | 1329 | while (isspace(*t)) if (t < end) t++; else return fields_loaded; |
---|
[109] | 1330 | |
---|
[320] | 1331 | field_end = strchrlimit(t, ',', end); if (!field_end) field_end = end; |
---|
[333] | 1332 | next_field = field_end; |
---|
[822] | 1333 | while ((field_end > t) && isblank(field_end[-1])) field_end--; |
---|
[320] | 1334 | quote = strchrlimit(t, '\"', field_end); |
---|
[154] | 1335 | if (quote) |
---|
[109] | 1336 | { |
---|
[154] | 1337 | quote2 = skipQuoteString(quote + 1, end); |
---|
[320] | 1338 | if (quote2 > field_end) |
---|
[154] | 1339 | { |
---|
[320] | 1340 | field_end = strchrlimit(quote2 + 1, ',', end); |
---|
[393] | 1341 | if (!field_end) field_end = end; |
---|
| 1342 | next_field = field_end; |
---|
[154] | 1343 | } |
---|
| 1344 | equals_sign = strchrlimit(t, '=', quote); |
---|
[109] | 1345 | } |
---|
[154] | 1346 | else |
---|
| 1347 | { |
---|
[320] | 1348 | equals_sign = strchrlimit(t, '=', field_end); |
---|
[154] | 1349 | quote2 = 0; |
---|
| 1350 | } |
---|
| 1351 | if (equals_sign == t) { t++; equals_sign = 0; } |
---|
[320] | 1352 | if (field_end == t) // skip empty value |
---|
[154] | 1353 | { |
---|
[973] | 1354 | t++; if (i >= 0) i++; |
---|
[154] | 1355 | continue; |
---|
| 1356 | } |
---|
| 1357 | if (equals_sign) // have parameter name |
---|
| 1358 | { |
---|
[247] | 1359 | tmpi = findIdn(t, (int)(equals_sign - t)); |
---|
[154] | 1360 | i = tmpi; |
---|
| 1361 | if (tmpi < 0) |
---|
[312] | 1362 | { |
---|
| 1363 | SString name(t, (int)(equals_sign - t)); |
---|
[796] | 1364 | logPrintf("Param", "loadSingleLine", LOG_WARN, "Unknown property '%s.%s' (ignored)", getName(), name.c_str()); |
---|
[312] | 1365 | } |
---|
[154] | 1366 | t = equals_sign + 1; // t=value |
---|
| 1367 | } |
---|
[109] | 1368 | #ifdef WARN_MISSING_NAME |
---|
[914] | 1369 | else // no parameter name |
---|
[973] | 1370 | { |
---|
[109] | 1371 | #ifdef SAVE_SELECTED_NAMES |
---|
[914] | 1372 | if ((i < 0) // field after unknown field |
---|
[973] | 1373 | || (i >= getPropCount()) // field after last field |
---|
| 1374 | || !(flags(i) & PARAM_CANOMITNAME)) // valid field but it can't be skipped |
---|
[109] | 1375 | #endif |
---|
[154] | 1376 | { |
---|
[914] | 1377 | if (i < getPropCount()) |
---|
| 1378 | logPrintf("Param", "loadSingleLine", LOG_WARN, "Missing property name in '%s'", getName()); |
---|
| 1379 | else // 'i' can go past PropCount because of moving to subsequent properties by i++, id(i) is then NULL |
---|
[822] | 1380 | logPrintf("Param", "loadSingleLine", LOG_WARN, "Value after the last property of '%s'", getName()); |
---|
[154] | 1381 | } |
---|
[973] | 1382 | } |
---|
[914] | 1383 | //else skipping a skippable field |
---|
[109] | 1384 | #endif |
---|
[914] | 1385 | if ((i >= 0) && id(i)) // shared by name and skippped name cases |
---|
[109] | 1386 | { |
---|
[154] | 1387 | value = t; |
---|
| 1388 | if (quote) |
---|
| 1389 | { |
---|
[247] | 1390 | tmpvalue.copyFrom(quote + 1, (int)(quote2 - quote) - 1); |
---|
[154] | 1391 | sstringUnquote(tmpvalue); |
---|
[348] | 1392 | value = tmpvalue.c_str(); |
---|
[154] | 1393 | valstop = quote2; |
---|
| 1394 | } |
---|
| 1395 | else |
---|
[320] | 1396 | if (field_end < end) valstop = field_end; else valstop = end; |
---|
[154] | 1397 | |
---|
| 1398 | remember = *valstop; |
---|
| 1399 | *(char*)valstop = 0; |
---|
[743] | 1400 | ret = setFromString(i, value, true); |
---|
[154] | 1401 | fields_loaded++; |
---|
[973] | 1402 | if (ret & PSET_PARSEFAILED) |
---|
[650] | 1403 | parse_failed = true; |
---|
[154] | 1404 | *(char*)valstop = remember; |
---|
[109] | 1405 | } |
---|
| 1406 | |
---|
[154] | 1407 | if (i >= 0) i++; |
---|
[109] | 1408 | #ifdef __CODEGUARD__ |
---|
[732] | 1409 | if (next_field < end - 1) t = next_field + 1; else return fields_loaded; |
---|
[109] | 1410 | #else |
---|
[320] | 1411 | t = next_field + 1; |
---|
[109] | 1412 | #endif |
---|
[154] | 1413 | } |
---|
[720] | 1414 | if (parse_failed) options.parse_failed = true; |
---|
| 1415 | return fields_loaded; |
---|
[109] | 1416 | } |
---|
| 1417 | |
---|
[154] | 1418 | int Param::grmember(int g, int a) |
---|
[109] | 1419 | { |
---|
[154] | 1420 | if ((getGroupCount() < 2) && (!g)) |
---|
| 1421 | return (a < getPropCount()) ? a : -9999; |
---|
[109] | 1422 | |
---|
[154] | 1423 | ParamEntry *e = entry(0); |
---|
| 1424 | int x = 0, i = 0; |
---|
| 1425 | for (; e->id; i++, e++) |
---|
[109] | 1426 | { |
---|
[154] | 1427 | if (e->group == g) |
---|
| 1428 | if (a == x) return i; else x++; |
---|
[109] | 1429 | } |
---|
[154] | 1430 | return -9999; |
---|
[109] | 1431 | } |
---|