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