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