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