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