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