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