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