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