1 | // This file is a part of the Framsticks GDK library. |
---|
2 | // Copyright (C) 2002-2011 Szymon Ulatowski. See LICENSE.txt for details. |
---|
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 "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;} |
---|
211 | |
---|
212 | int ParamInterface::save(VirtFILE* f,const SString* altname,bool force) |
---|
213 | { |
---|
214 | const char *p; |
---|
215 | SString ws; |
---|
216 | int err=0,i; |
---|
217 | bool withname=false; |
---|
218 | if ((!altname)||(altname->len())) |
---|
219 | { |
---|
220 | err|=(fputs(altname?((const char*)(*altname)):getName(),f)==EOF); |
---|
221 | err|=(fputs(":\n",f)==EOF); |
---|
222 | withname=true; |
---|
223 | } |
---|
224 | for (i=0;p=id(i);i++) |
---|
225 | err|=saveprop(f,i,p,force); |
---|
226 | if (withname) |
---|
227 | err|=(fputs("\n",f)==EOF); |
---|
228 | return err; |
---|
229 | } |
---|
230 | |
---|
231 | int ParamInterface::saveprop(VirtFILE* f,int i,const char* p,bool force) |
---|
232 | { |
---|
233 | if ((flags(i)&PARAM_DONTSAVE)&&(!force)) return 0; |
---|
234 | const char *typ=type(i); |
---|
235 | if ((*typ=='p')||(*typ=='o')) return 0; |
---|
236 | |
---|
237 | const char *t,*w; |
---|
238 | SString ws; |
---|
239 | int err=0,cr; |
---|
240 | |
---|
241 | err|=(fputs(p,f)==EOF); fputc(':',f); |
---|
242 | cr=0; |
---|
243 | ws=get(i); |
---|
244 | quoteTilde(ws); |
---|
245 | w=ws; |
---|
246 | if (ws.len()>50) cr=1; |
---|
247 | else for (t=w;*t;t++) if ((*t==10)||(*t==13)) {cr=1; break;} |
---|
248 | if (cr) fputs("~\n",f); |
---|
249 | err|=(fputs(w,f)==EOF); |
---|
250 | err|=(fputs(cr ? "~\n" : "\n",f)==EOF); |
---|
251 | return err; |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | int SimpleAbstractParam::isequal(int i,void* defdata) |
---|
256 | { // defdata->member == object->member ? |
---|
257 | void *backup=object; |
---|
258 | switch(type(i)[0]) |
---|
259 | { |
---|
260 | case 'd': |
---|
261 | { |
---|
262 | select(defdata); |
---|
263 | long x=getInt(i); |
---|
264 | select(backup); |
---|
265 | return x==getInt(i); |
---|
266 | } |
---|
267 | case 'f': |
---|
268 | { |
---|
269 | select(defdata); |
---|
270 | double x=getDouble(i); |
---|
271 | select(backup); |
---|
272 | return x==getDouble(i); |
---|
273 | } |
---|
274 | case 's': |
---|
275 | { |
---|
276 | select(defdata); |
---|
277 | SString x=getString(i); |
---|
278 | select(backup); |
---|
279 | return x==getString(i); |
---|
280 | } |
---|
281 | } |
---|
282 | return 1; |
---|
283 | } |
---|
284 | |
---|
285 | void SimpleAbstractParam::save2(SString& f,void *defdata,bool addcr,bool all_names) |
---|
286 | { // defdata!=NULL -> nie zapisuje wartosci domyslnych |
---|
287 | const char *p; |
---|
288 | int i; |
---|
289 | int needlabel=0; |
---|
290 | int first=1; |
---|
291 | SString val; |
---|
292 | SString t; |
---|
293 | int fl; |
---|
294 | // t+=SString(getName()); t+=':'; |
---|
295 | for (i=0;p=id(i);i++) |
---|
296 | if (!((fl=flags(i))&PARAM_DONTSAVE)) |
---|
297 | { |
---|
298 | if (defdata && isequal(i,defdata)) |
---|
299 | needlabel=1; |
---|
300 | else |
---|
301 | { |
---|
302 | if (!first) t+=", "; |
---|
303 | #ifndef SAVE_ALL_NAMES |
---|
304 | #ifdef SAVE_SELECTED_NAMES |
---|
305 | if (needlabel || all_names || !(fl & PARAM_CANOMITNAME)) |
---|
306 | #else |
---|
307 | if (needlabel) |
---|
308 | #endif |
---|
309 | #endif |
---|
310 | { t+=p; t+="="; needlabel=0; } |
---|
311 | if (type(i)[0]=='s') |
---|
312 | { // string - special case |
---|
313 | SString str=getString(i); |
---|
314 | if (strContainsOneOf(str,", \\\n\r\t\"")) |
---|
315 | { |
---|
316 | t+="\""; |
---|
317 | sstringQuote(str); |
---|
318 | t+=str; |
---|
319 | t+="\""; |
---|
320 | } |
---|
321 | else |
---|
322 | t+=str; |
---|
323 | } |
---|
324 | else |
---|
325 | t+=get(i); |
---|
326 | first=0; |
---|
327 | } |
---|
328 | } |
---|
329 | if (addcr) |
---|
330 | t+="\n"; |
---|
331 | f+=t; |
---|
332 | } |
---|
333 | |
---|
334 | void ParamInterface::load(VirtFILE* f) |
---|
335 | { |
---|
336 | char t[100]; |
---|
337 | int i; |
---|
338 | const char *p,*p0; |
---|
339 | int p_len; |
---|
340 | bool eol,loaded; |
---|
341 | int ret; |
---|
342 | while(fgets0(t,100,f,eol)) |
---|
343 | { |
---|
344 | p0=t; while ((*p0==' ')||(*p0=='\t')) p0++; |
---|
345 | if (!*p0) break; |
---|
346 | p=strchr(p0,':'); if (!p) continue; |
---|
347 | p_len=p-p0; |
---|
348 | loaded=false; |
---|
349 | if (p_len&&((i=findIdn(p0,p_len))>=0)&&(!(flags(i)&PARAM_DONTLOAD))) |
---|
350 | { |
---|
351 | if (p0[p_len+1]=='~') |
---|
352 | { |
---|
353 | SString s; |
---|
354 | czytdotyldy(f,s); |
---|
355 | int ch; while((ch=fgetc(f))!=EOF) if (ch=='\n') break; |
---|
356 | unquoteTilde(s); |
---|
357 | ret=set(i,(const char*)s); |
---|
358 | } |
---|
359 | else |
---|
360 | { |
---|
361 | if (eol) |
---|
362 | ret=set(i,p0+p_len+1); |
---|
363 | else |
---|
364 | { |
---|
365 | SString tmp=p0+p_len+1; |
---|
366 | while(!eol) |
---|
367 | { |
---|
368 | if (!fgets0(t,100,f,eol)) break; |
---|
369 | tmp+=t; |
---|
370 | } |
---|
371 | ret=set(i,(const char*)tmp); |
---|
372 | } |
---|
373 | } |
---|
374 | if (ret & (PSET_HITMIN | PSET_HITMAX)) |
---|
375 | FMprintf("Param","load2",FMLV_WARN,"Adjusted '%s' in '%s' (was too %s)", |
---|
376 | id(i),getName(),(ret&PSET_HITMAX)?"big":"small"); |
---|
377 | loaded=true; |
---|
378 | } |
---|
379 | if ((!loaded) && (p0[p_len+1]=='~')) |
---|
380 | { // eat unrecognized multiline field |
---|
381 | SString s; |
---|
382 | czytdotyldy(f,s); |
---|
383 | int ch; while((ch=fgetc(f))!=EOF) if (ch=='\n') break; |
---|
384 | } |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | |
---|
389 | /* |
---|
390 | SString SimpleAbstractParam::getString(int i) |
---|
391 | { |
---|
392 | char *t; |
---|
393 | switch (*(t=type(i))) |
---|
394 | { |
---|
395 | case 'd': |
---|
396 | { |
---|
397 | for (i=atol(get(i));i>=0;i--) if (t) t=strchr(t+1,'~'); |
---|
398 | if (t) |
---|
399 | { |
---|
400 | t++; |
---|
401 | char *t2=strchr(t,'~'); |
---|
402 | if (!t2) t2=t+strlen(t); |
---|
403 | SString str; |
---|
404 | strncpy(str.directWrite(t2-t),t,t2-t); |
---|
405 | str.endWrite(t2-t); |
---|
406 | return str; |
---|
407 | } |
---|
408 | } |
---|
409 | } |
---|
410 | return get(i); |
---|
411 | } |
---|
412 | */ |
---|
413 | |
---|
414 | int ParamInterface::findId(const char* n) |
---|
415 | { |
---|
416 | int i; const char *p; |
---|
417 | for (i=0;p=id(i);i++) if (!strcmp(n,p)) return i; |
---|
418 | return -1; |
---|
419 | } |
---|
420 | |
---|
421 | int ParamInterface::findIdn(const char* naz,int n) |
---|
422 | { |
---|
423 | int i; const char *p; |
---|
424 | for (i=0;p=id(i);i++) if ((!strncmp(naz,p,n))&&(!p[n])) return i; |
---|
425 | return -1; |
---|
426 | } |
---|
427 | |
---|
428 | void ParamInterface::get(int i,ExtValue &ret) |
---|
429 | { |
---|
430 | switch(type(i)[0]) |
---|
431 | { |
---|
432 | case 'd': ret.setInt(getInt(i)); break; |
---|
433 | case 'f': ret.setDouble(getDouble(i)); break; |
---|
434 | case 's': ret.setString(getString(i)); break; |
---|
435 | case 'o': ret.setObject(getObject(i)); break; |
---|
436 | case 'x': ret=getExtValue(i); break; |
---|
437 | default: FMprintf("ParamInterface","get",FMLV_ERROR,"'%s.%s' is not a field",getName(),id(i)); |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | static bool stringIsNumeric(const char* str) |
---|
442 | {// /-?.?[0-9]+/ |
---|
443 | if (!str) return false; |
---|
444 | if (*str=='-') str++; |
---|
445 | if (*str=='.') str++; |
---|
446 | return isdigit(*str); |
---|
447 | } |
---|
448 | |
---|
449 | int ParamInterface::setInt(int i,const char* str) |
---|
450 | { |
---|
451 | if (!stringIsNumeric(str)) |
---|
452 | { |
---|
453 | long a,b,c; |
---|
454 | if (getMinMax(i,a,b,c)>=3) |
---|
455 | return setInt(i,c); |
---|
456 | else |
---|
457 | return setInt(i,(long)0); |
---|
458 | } |
---|
459 | else |
---|
460 | return setInt(i,atol(str)); |
---|
461 | } |
---|
462 | |
---|
463 | int ParamInterface::setDouble(int i,const char* str) |
---|
464 | { |
---|
465 | if (!stringIsNumeric(str)) |
---|
466 | { |
---|
467 | double a,b,c; |
---|
468 | if (getMinMax(i,a,b,c)>=3) |
---|
469 | return setDouble(i,c); |
---|
470 | else |
---|
471 | return setDouble(i,(double)0); |
---|
472 | } |
---|
473 | else |
---|
474 | return setDouble(i,atof(str)); |
---|
475 | } |
---|
476 | |
---|
477 | int ParamInterface::set(int i,const ExtValue &v) |
---|
478 | { |
---|
479 | switch(type(i)[0]) |
---|
480 | { |
---|
481 | case 'd': if ((v.type==TInt)||(v.type==TDouble)) return setInt(i,v.getInt()); else return setInt(i,(const char*)v.getString()); |
---|
482 | case 'f': if ((v.type==TInt)||(v.type==TDouble)) return setDouble(i,v.getDouble()); else return setDouble(i,(const char*)v.getString()); |
---|
483 | case 's': { SString t=v.getString(); return setString(i,t); } |
---|
484 | case 'o': return setObject(i,v.getObject()); |
---|
485 | case 'x': return setExtValue(i,v); |
---|
486 | default: FMprintf("ParamInterface","get",FMLV_ERROR,"'%s.%s' is not a field",getName(),id(i)); |
---|
487 | } |
---|
488 | return 0; |
---|
489 | } |
---|
490 | |
---|
491 | int ParamInterface::set(int i,const char *v) |
---|
492 | { |
---|
493 | switch(type(i)[0]) |
---|
494 | { |
---|
495 | case 'd': return setInt(i,v); |
---|
496 | case 'f': return setDouble(i,v); |
---|
497 | case 's': { SString t(v); return setString(i,t); } |
---|
498 | case 'x': |
---|
499 | { |
---|
500 | ExtValue e; |
---|
501 | if (isdigit(*v)||((*v=='-')&&isdigit(v[1]))) |
---|
502 | { |
---|
503 | if (strchr(v,'.')) e.setDouble(atof(v)); |
---|
504 | else e.setInt(atol(v)); |
---|
505 | } |
---|
506 | else |
---|
507 | { |
---|
508 | e.setString(SString(v)); |
---|
509 | } |
---|
510 | return setExtValue(i,e); |
---|
511 | } |
---|
512 | } |
---|
513 | return 0; |
---|
514 | } |
---|
515 | |
---|
516 | SString ParamInterface::getText(int i) |
---|
517 | { |
---|
518 | const char *t; |
---|
519 | if ((*(t=type(i)))=='d') |
---|
520 | { |
---|
521 | for (int j=getInt(i);j>=0;j--) if (t) t=strchr(t+1,'~'); |
---|
522 | if (t) |
---|
523 | { |
---|
524 | t++; |
---|
525 | const char *t2=strchr(t,'~'); |
---|
526 | if (!t2) t2=t+strlen(t); |
---|
527 | return SString(t,t2-t); |
---|
528 | } |
---|
529 | } |
---|
530 | return get(i); |
---|
531 | } |
---|
532 | |
---|
533 | SString ParamInterface::get(int i) |
---|
534 | { |
---|
535 | switch(type(i)[0]) |
---|
536 | { |
---|
537 | case 'd': |
---|
538 | { |
---|
539 | SString tmp; |
---|
540 | sprintf(tmp.directWrite(20),"%ld",getInt(i)); tmp.endWrite(); |
---|
541 | return tmp; |
---|
542 | } |
---|
543 | case 'f': |
---|
544 | { |
---|
545 | SString tmp; |
---|
546 | sprintf(tmp.directWrite(20),"%lg",getDouble(i)); tmp.endWrite(); |
---|
547 | return tmp; |
---|
548 | } |
---|
549 | case 's': |
---|
550 | return getString(i); |
---|
551 | } |
---|
552 | ExtValue v; |
---|
553 | get(i,v); |
---|
554 | return v.getString(); |
---|
555 | } |
---|
556 | |
---|
557 | |
---|
558 | //////////////////////////////// PARAM //////////////////////////////////// |
---|
559 | |
---|
560 | void *SimpleAbstractParam::getTarget(int i) |
---|
561 | { |
---|
562 | return (void*)(((char*)object)+entry(i)->offset); |
---|
563 | //return &(object->*(entry(i)->fldptr)); |
---|
564 | } |
---|
565 | |
---|
566 | ///////// get |
---|
567 | |
---|
568 | long SimpleAbstractParam::getInt(int i) |
---|
569 | { |
---|
570 | static ExtValue v; |
---|
571 | ParamEntry *pe=entry(i); |
---|
572 | if (pe->fun1) |
---|
573 | { |
---|
574 | (*(void(*)(void*,ExtValue*))pe->fun1)(object,&v); |
---|
575 | return v.getInt(); |
---|
576 | } |
---|
577 | else |
---|
578 | { |
---|
579 | void *target=getTarget(i); |
---|
580 | return *((long*)target); |
---|
581 | } |
---|
582 | } |
---|
583 | |
---|
584 | double SimpleAbstractParam::getDouble(int i) |
---|
585 | { |
---|
586 | static ExtValue v; |
---|
587 | ParamEntry *pe=entry(i); |
---|
588 | if (pe->fun1) |
---|
589 | { |
---|
590 | (*(void(*)(void*,ExtValue*))pe->fun1)(object,&v); |
---|
591 | return v.getDouble(); |
---|
592 | } |
---|
593 | else |
---|
594 | { |
---|
595 | void *target=getTarget(i); |
---|
596 | return *((double*)target); |
---|
597 | } |
---|
598 | } |
---|
599 | |
---|
600 | SString SimpleAbstractParam::getString(int i) |
---|
601 | { |
---|
602 | static ExtValue v; |
---|
603 | ParamEntry *pe=entry(i); |
---|
604 | if (pe->fun1) |
---|
605 | { |
---|
606 | (*(void(*)(void*,ExtValue*))pe->fun1)(object,&v); |
---|
607 | return v.getString(); |
---|
608 | } |
---|
609 | else |
---|
610 | { |
---|
611 | void *target=getTarget(i); |
---|
612 | return *((SString*)target); |
---|
613 | } |
---|
614 | } |
---|
615 | |
---|
616 | ExtObject SimpleAbstractParam::getObject(int i) |
---|
617 | { |
---|
618 | static ExtValue v; |
---|
619 | ParamEntry *pe=entry(i); |
---|
620 | if (pe->fun1) |
---|
621 | { |
---|
622 | (*(void(*)(void*,ExtValue*))pe->fun1)(object,&v); |
---|
623 | return v.getObject(); |
---|
624 | } |
---|
625 | else |
---|
626 | { |
---|
627 | void *target=getTarget(i); |
---|
628 | return *((ExtObject*)target); |
---|
629 | } |
---|
630 | } |
---|
631 | |
---|
632 | ExtValue SimpleAbstractParam::getExtValue(int i) |
---|
633 | { |
---|
634 | static ExtValue v; |
---|
635 | ParamEntry *pe=entry(i); |
---|
636 | if (pe->fun1) |
---|
637 | { |
---|
638 | (*(void(*)(void*,ExtValue*))pe->fun1)(object,&v); |
---|
639 | return v; |
---|
640 | } |
---|
641 | else |
---|
642 | { |
---|
643 | void *target=getTarget(i); |
---|
644 | return *((ExtValue*)target); |
---|
645 | } |
---|
646 | } |
---|
647 | |
---|
648 | |
---|
649 | //////// set |
---|
650 | |
---|
651 | int SimpleAbstractParam::setInt(int i,long x) |
---|
652 | { |
---|
653 | static ExtValue v; |
---|
654 | ParamEntry *pe=entry(i); |
---|
655 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
656 | long a=0,b=0,result=0; |
---|
657 | const char* t=pe->type+1; |
---|
658 | while(*t) if (*t==' ') break; else t++; |
---|
659 | if (sscanf(t,"%ld %ld",&a,&b)==2) |
---|
660 | if (a<=b) // jezeli max<min to znaczy ze min/max nie obowiazuje |
---|
661 | { |
---|
662 | if (x<a) {x=a; result=PSET_HITMIN;} |
---|
663 | else if (x>b) {x=b; result=PSET_HITMAX;} |
---|
664 | } |
---|
665 | |
---|
666 | if (pe->fun2) |
---|
667 | { |
---|
668 | v.setInt(x); |
---|
669 | return result | (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&v); |
---|
670 | } |
---|
671 | else |
---|
672 | { |
---|
673 | void *target=getTarget(i); |
---|
674 | if (dontcheckchanges || (*((long*)target)!=x)) |
---|
675 | { |
---|
676 | result |= PSET_CHANGED; |
---|
677 | *((long*)target)=x; |
---|
678 | } |
---|
679 | return result; |
---|
680 | } |
---|
681 | } |
---|
682 | |
---|
683 | int SimpleAbstractParam::setDouble(int i,double x) |
---|
684 | { |
---|
685 | static ExtValue v; |
---|
686 | ParamEntry *pe=entry(i); |
---|
687 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
688 | double a=0,b=0; int result=0; |
---|
689 | const char* t=pe->type+1; |
---|
690 | while(*t) if (*t==' ') break; else t++; |
---|
691 | if (sscanf(t,"%lg %lg",&a,&b)==2) |
---|
692 | if (a<=b) // jezeli max<min to znaczy ze min/max nie obowiazuje |
---|
693 | { |
---|
694 | if (x<a) {x=a; result=PSET_HITMIN;} |
---|
695 | else if (x>b) {x=b; result=PSET_HITMAX;} |
---|
696 | } |
---|
697 | |
---|
698 | if (pe->fun2) |
---|
699 | { |
---|
700 | v.setDouble(x); |
---|
701 | return result | (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&v); |
---|
702 | } |
---|
703 | else |
---|
704 | { |
---|
705 | void *target=getTarget(i); |
---|
706 | if (dontcheckchanges || (*((double*)target)!=x)) |
---|
707 | { |
---|
708 | result|=PSET_CHANGED; |
---|
709 | *((double*)target)=x; |
---|
710 | } |
---|
711 | return result; |
---|
712 | } |
---|
713 | } |
---|
714 | |
---|
715 | int SimpleAbstractParam::setString(int i,const SString& x) |
---|
716 | { |
---|
717 | static ExtValue v; |
---|
718 | static SString vs; |
---|
719 | const SString *xx=&x; |
---|
720 | ParamEntry *pe=entry(i); |
---|
721 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
722 | const char* t=pe->type+1; |
---|
723 | while(*t) if (*t==' ') break; else t++; |
---|
724 | long a=0,b=0,result=0; |
---|
725 | if (sscanf(t,"%ld %ld",&a,&b)==2) |
---|
726 | { |
---|
727 | if ((x.len()>b)&&(b>0)) |
---|
728 | { |
---|
729 | vs=x.substr(0,b); |
---|
730 | xx=&vs; |
---|
731 | result|=PSET_HITMAX; |
---|
732 | } |
---|
733 | } |
---|
734 | |
---|
735 | if (pe->fun2) |
---|
736 | { |
---|
737 | v.setString(*xx); |
---|
738 | return result | (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&v); |
---|
739 | } |
---|
740 | else |
---|
741 | { |
---|
742 | void *target=getTarget(i); |
---|
743 | if (dontcheckchanges || (!(*((SString*)target) == *xx))) |
---|
744 | { |
---|
745 | result|=PSET_CHANGED; |
---|
746 | *((SString*)target)=x; |
---|
747 | } |
---|
748 | return result; |
---|
749 | } |
---|
750 | } |
---|
751 | |
---|
752 | int SimpleAbstractParam::setObject(int i,const ExtObject& x) |
---|
753 | { |
---|
754 | static ExtValue v; |
---|
755 | ParamEntry *pe=entry(i); |
---|
756 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
757 | if (pe->fun2) |
---|
758 | { |
---|
759 | v.setObject(x); |
---|
760 | return (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&v); |
---|
761 | } |
---|
762 | else |
---|
763 | { |
---|
764 | void *target=getTarget(i); |
---|
765 | *((ExtObject*)target)=x; |
---|
766 | return PSET_CHANGED; |
---|
767 | } |
---|
768 | } |
---|
769 | |
---|
770 | int SimpleAbstractParam::setExtValue(int i,const ExtValue& x) |
---|
771 | { |
---|
772 | ParamEntry *pe=entry(i); |
---|
773 | if (pe->flags&PARAM_READONLY) return PSET_RONLY; |
---|
774 | if (pe->fun2) |
---|
775 | { |
---|
776 | return (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&x); |
---|
777 | } |
---|
778 | else |
---|
779 | { |
---|
780 | void *target=getTarget(i); |
---|
781 | *((ExtValue*)target)=x; |
---|
782 | return PSET_CHANGED; |
---|
783 | } |
---|
784 | } |
---|
785 | |
---|
786 | void SimpleAbstractParam::call(int i,ExtValue *args,ExtValue *ret) |
---|
787 | { |
---|
788 | ParamEntry *pe=entry(i); |
---|
789 | if (!pe) return; |
---|
790 | if (pe->fun1 && (pe->type[0]=='p')) |
---|
791 | (*(void(*)(void*,ExtValue*,ExtValue*))pe->fun1)(object,args,ret); |
---|
792 | else |
---|
793 | { |
---|
794 | FMprintf("SimpleAbstractParam","call",FMLV_ERROR, |
---|
795 | (*pe->type!='p')?"'%s.%s' is not a function":"internal error - undefined function pointer for '%s.%s'",getName(),pe->id); |
---|
796 | } |
---|
797 | } |
---|
798 | |
---|
799 | void SimpleAbstractParam::setDefault(bool numericonly) |
---|
800 | { |
---|
801 | bool save=dontcheckchanges; |
---|
802 | dontcheckchanges=1; |
---|
803 | ParamInterface::setDefault(numericonly); |
---|
804 | dontcheckchanges=save; |
---|
805 | } |
---|
806 | |
---|
807 | void SimpleAbstractParam::setDefault(int i,bool numericonly) |
---|
808 | { |
---|
809 | bool save=dontcheckchanges; |
---|
810 | dontcheckchanges=1; |
---|
811 | ParamInterface::setDefault(i,numericonly); |
---|
812 | dontcheckchanges=save; |
---|
813 | } |
---|
814 | |
---|
815 | // zwraca adres poczatku linii |
---|
816 | // len = dlugosc linii (bez \n) |
---|
817 | // 0 moze oznaczac linie dlugosci 0 lub koniec SStringa |
---|
818 | // poz jest przesuwane na poczatek nastepnej linii |
---|
819 | // typowa petla: for(poz=0;poz<s.d;) {line=getline(s,poz,len);... |
---|
820 | static const char *getline(const SString &s,int &poz,int &len) |
---|
821 | { |
---|
822 | const char *beg=(const char*)s+poz; |
---|
823 | if (poz>=s.len()) {poz=s.len(); len=0; return (const char*)s+s.len();} |
---|
824 | const char *lf=strchr(beg,'\n'); |
---|
825 | if (!lf) { lf=(const char*)s+s.len()-1; poz=s.len(); } |
---|
826 | else {poz=(lf-(const char*)s)+1; if (poz>s.len()) poz=s.len();} |
---|
827 | while (lf>=beg) if ((*lf=='\n')||(*lf=='\r')) lf--; else break; |
---|
828 | len=lf-beg+1; |
---|
829 | return beg; |
---|
830 | } |
---|
831 | |
---|
832 | void ParamInterface::load2(const SString &s,int &poz) |
---|
833 | { |
---|
834 | int i; // numer akt. parametru |
---|
835 | int tmpi; |
---|
836 | int len; |
---|
837 | int ret; |
---|
838 | const char *t,*lin,*end; |
---|
839 | const char *rownasie,*przecinek; |
---|
840 | char remember; |
---|
841 | const char *quote,*quote2; |
---|
842 | const char *value,*valstop; |
---|
843 | SString tmpvalue; |
---|
844 | if (poz>=s.len()) return; |
---|
845 | t=(const char*)s+poz; |
---|
846 | |
---|
847 | // na razie wszystko musi byc w jednej linii... |
---|
848 | lin=getline(s,poz,len); |
---|
849 | if (!len) return; // pusta linia = koniec |
---|
850 | i=0; |
---|
851 | end=lin+len; |
---|
852 | while(t<end) |
---|
853 | { |
---|
854 | // przetwarzanie jednego par |
---|
855 | while (strchr(" \n\r\t",*t)) if (t<end) t++; else return; |
---|
856 | |
---|
857 | przecinek=strchrlimit(t,',',end); if (!przecinek) przecinek=end; |
---|
858 | quote=strchrlimit(t,'\"',przecinek); |
---|
859 | if (quote) |
---|
860 | { |
---|
861 | quote2=skipQuoteString(quote+1,end); |
---|
862 | if (quote2>przecinek) |
---|
863 | { |
---|
864 | przecinek=strchrlimit(quote2+1,',',end); |
---|
865 | if (!przecinek) przecinek=end; |
---|
866 | } |
---|
867 | rownasie=strchrlimit(t,'=',quote); |
---|
868 | } |
---|
869 | else |
---|
870 | { |
---|
871 | rownasie=strchrlimit(t,'=',przecinek); |
---|
872 | quote2=0; |
---|
873 | } |
---|
874 | if (rownasie==t) { t++; rownasie=0; } |
---|
875 | if (przecinek==t) // skip empty value |
---|
876 | { |
---|
877 | t++; i++; |
---|
878 | continue; |
---|
879 | } |
---|
880 | if (rownasie) // have parameter name |
---|
881 | { |
---|
882 | tmpi=findIdn(t,rownasie-t); |
---|
883 | i=tmpi; |
---|
884 | if (tmpi<0) |
---|
885 | FMprintf("Param","load2",FMLV_WARN,"Unknown property name for '%s' (ignored)",getName()); |
---|
886 | t=rownasie+1; // t=value |
---|
887 | } |
---|
888 | #ifdef WARN_MISSING_NAME |
---|
889 | else |
---|
890 | #ifdef SAVE_SELECTED_NAMES |
---|
891 | if (!(flags(i)&PARAM_CANOMITNAME)) |
---|
892 | #endif |
---|
893 | { |
---|
894 | FMprintf("Param","load2",FMLV_WARN,"Missing property name in '%s' (assuming '%s')", |
---|
895 | getName(),id(i)?id(i):"unknown property?"); |
---|
896 | } |
---|
897 | #endif |
---|
898 | if ((i>=0)&&id(i)) |
---|
899 | { |
---|
900 | value=t; |
---|
901 | if (quote) |
---|
902 | { |
---|
903 | tmpvalue.copyFrom(quote+1,quote2-quote-1); |
---|
904 | sstringUnquote(tmpvalue); |
---|
905 | value=tmpvalue; |
---|
906 | valstop=quote2; |
---|
907 | } |
---|
908 | else |
---|
909 | if (przecinek<end) valstop=przecinek; else valstop=end; |
---|
910 | |
---|
911 | remember=*valstop; |
---|
912 | *(char*)valstop=0; |
---|
913 | ret=set(i,value); |
---|
914 | if (ret&(PSET_HITMAX|PSET_HITMIN)) |
---|
915 | FMprintf("Param","load2",FMLV_WARN,"Adjusted '%s' in '%s' (was too %s)", |
---|
916 | id(i),getName(),(ret&PSET_HITMAX)?"big":"small"); |
---|
917 | *(char*)valstop=remember; |
---|
918 | } |
---|
919 | |
---|
920 | if (i>=0) i++; |
---|
921 | #ifdef __CODEGUARD__ |
---|
922 | if (przecinek<end-1) t=przecinek+1; else return; |
---|
923 | #else |
---|
924 | t=przecinek+1; |
---|
925 | #endif |
---|
926 | } |
---|
927 | return; |
---|
928 | } |
---|
929 | |
---|
930 | int Param::grmember(int g,int a) |
---|
931 | { |
---|
932 | if ((getGroupCount()<2)&&(!g)) |
---|
933 | return (a<getPropCount())?a:-9999; |
---|
934 | |
---|
935 | ParamEntry *e=entry(0); |
---|
936 | int x=0,i=0; |
---|
937 | for (;e->id;i++,e++) |
---|
938 | { |
---|
939 | if (e->group==g) |
---|
940 | if (a==x) return i; else x++; |
---|
941 | } |
---|
942 | return -9999; |
---|
943 | } |
---|