source: cpp/frams/param/param.cpp @ 306

Last change on this file since 306 was 306, checked in by Maciej Komosinski, 9 years ago

String length limit in Param::setString() was only enforced for procedural members but not for simple fields, even though it was correctly detected in both cases

  • Property svn:eol-style set to native
File size: 23.8 KB
Line 
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.
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
17char MakeCodeGuardHappy;
18
19ParamEntry empty_paramtab[] =
20{ { "Empty", 1, 0, "Empty", }, { 0, 0, 0, }, };
21
22static 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
37static const char *strchrlimit(const char *t, int ch, const char *limit)
38{
39        int n = (int)(limit - t);
40        for (; (n > 0) && *t; t++, n--)
41                if (*t == ch) return t;
42        return 0;
43}
44
45void ParamInterface::copyFrom(ParamInterface *src)
46{
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                }
59}
60
61void ParamInterface::quickCopyFrom(ParamInterface *src)
62{
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                }
72}
73
74int ParamInterface::getMinMax(int prop, paInt& minumum, paInt& maximum, paInt &def)
75{
76        const char* t = type(prop) + 1;
77        while (*t) if (*t == ' ') break; else t++;
78        return sscanf(t, PA_INT_SCANF " " PA_INT_SCANF " " PA_INT_SCANF, &minumum, &maximum, &def);
79}
80
81int ParamInterface::getMinMax(int prop, double& minumum, double& maximum, double& def)
82{
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);
86}
87
88int ParamInterface::getMinMax(int prop, int& minumum, int& maximum, SString& def)
89{
90        const char* t = type(prop) + 1;
91        while (*t) if (*t == ' ') break; else t++;
92        int ret=sscanf(t, "%d %d", &minumum, &maximum);
93        def=SString::empty();
94        if (ret==2)
95                {
96                while (*t==' ') t++;
97                for(int skip_fields=2;skip_fields>0;skip_fields--)
98                        {
99                        while (*t) if (*t == ' ') break; else t++;
100                        while (*t==' ') t++;
101                        }
102                if (*t)
103                        {
104                        const char* end=strchr(t,'~');
105                        if (!end)
106                                end=t+strlen(t);
107                        while ((end>t)&&(end[-1]==' ')) end--;
108                        def=SString(t,end-t);
109                        }
110                return 3;
111                }
112        else
113                return ret;
114}
115
116void ParamInterface::setDefault()
117{
118        for (int i = 0; i < getPropCount(); i++)
119                setDefault(i);
120}
121
122void ParamInterface::setMin()
123{
124        for (int i = 0; i < getPropCount(); i++)
125                setMin(i);
126}
127
128void ParamInterface::setMax()
129{
130        for (int i = 0; i < getPropCount(); i++)
131                setMax(i);
132}
133
134void ParamInterface::setDefault(int i)
135{
136        const char *t = type(i);
137        switch (*t)
138        {
139        case 'f':
140        {
141                double a = 0, b = 0, c = 0;
142                if (getMinMax(i, a, b, c) < 3) c = a;
143                setDouble(i, c);
144        }
145                break;
146        case 'd':
147        {
148                paInt a = 0, b = 0, c = 0;
149                if (getMinMax(i, a, b, c) < 3) c = a;
150                setInt(i, c);
151        }
152                break;
153        case 's': case 'x':
154        {
155                int a,b; SString c;
156                getMinMax(i,a,b,c);
157                if (*t=='s')
158                        setString(i,c);
159                else
160                        { if (c.len()>0) setExtValue(i,ExtValue(c)); else setExtValue(i,ExtValue::empty()); }
161        }
162                break;
163        case 'o':
164                setObject(i,ExtObject::empty());
165                break;
166        }
167}
168
169void ParamInterface::setMin(int i)
170{
171        const char *t = type(i);
172        switch (*t)
173        {
174        case 'f':
175        {
176                double a = 0, b = 0, c = 0;
177                getMinMax(i, a, b, c);
178                setDouble(i, a);
179        }
180                break;
181        case 'd':
182        {
183                paInt a = 0, b = 0, c = 0;
184                getMinMax(i, a, b, c);
185                setInt(i, a);
186        }
187                break;
188        default: set(i, "");
189        }
190}
191
192void ParamInterface::setMax(int i)
193{
194        const char *t = type(i);
195        switch (*t)
196        {
197        case 'f':
198        {
199                double a = 0, b = 0, c = 0;
200                getMinMax(i, a, b, c);
201                setDouble(i, b);
202        }
203                break;
204        case 'd':
205        {
206                paInt a = 0, b = 0, c = 0;
207                getMinMax(i, a, b, c);
208                setInt(i, b);
209        }
210                break;
211        default: set(i, "");
212        }
213}
214
215SString ParamInterface::getStringById(const char*prop)
216{int i=findId(prop); if (i>=0) return getString(i); else return SString();}
217paInt ParamInterface::getIntById(const char*prop)
218{int i=findId(prop); if (i>=0) return getInt(i); else return 0;}
219double ParamInterface::getDoubleById(const char*prop)
220{int i=findId(prop); if (i>=0) return getDouble(i); else return 0;}
221ExtObject ParamInterface::getObjectById(const char*prop)
222{int i=findId(prop); if (i>=0) return getObject(i); else return ExtObject();}
223ExtValue ParamInterface::getExtValueById(const char*prop)
224{int i=findId(prop); if (i>=0) return getExtValue(i); else return ExtValue();}
225
226int ParamInterface::setIntById(const char* prop,paInt v)
227{int i=findId(prop); if (i>=0) return setInt(i,v); else return PSET_NOPROPERTY;}
228int ParamInterface::setDoubleById(const char* prop,double v)
229{int i=findId(prop); if (i>=0) return setDouble(i,v); else return PSET_NOPROPERTY;}
230int ParamInterface::setStringById(const char* prop,const SString &v)
231{int i=findId(prop); if (i>=0) return setString(i,v); else return PSET_NOPROPERTY;}
232int ParamInterface::setObjectById(const char* prop,const ExtObject &v)
233{int i=findId(prop); if (i>=0) return setObject(i,v); else return PSET_NOPROPERTY;}
234int ParamInterface::setExtValueById(const char* prop,const ExtValue &v)
235{int i=findId(prop); if (i>=0) return setExtValue(i,v); else return PSET_NOPROPERTY;}
236int 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
239int ParamInterface::save(VirtFILE* f, const char* altname, bool force)
240{
241        const char *p;
242        SString ws;
243        int err = 0, i;
244        bool withname = false;
245        if ((altname == NULL) || (altname[0] != 0))
246        {
247                err |= (fputs(altname ? altname : getName(), f) == EOF);
248                err |= (fputs(":\n", f) == EOF);
249                withname = true;
250        }
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;
256}
257
258const char* ParamInterface::SERIALIZATION_PREFIX = "@Serialized:";
259
260int ParamInterface::saveprop(VirtFILE* f, int i, const char* p, bool force)
261{
262        if ((flags(i)&PARAM_DONTSAVE) && (!force)) return 0;
263        const char *typ = type(i);
264        if (*typ == 'p') return 0;
265
266        const char *t, *w;
267        SString ws;
268        int err = 0, cr;
269
270        err |= (fputs(p, f) == EOF); fputc(':', f);
271        cr = 0;
272        if ((*typ == 'x')||(*typ == 'o'))
273        {
274                ExtValue ex;
275                get(i, ex);
276                ws = SString(SERIALIZATION_PREFIX) + ex.serialize();
277        }
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;
288}
289
290
291int SimpleAbstractParam::isequal(int i, void* defdata)
292{ // defdata->member == object->member ?
293        void *backup = object;
294        switch (type(i)[0])
295        {
296        case 'd':
297        {
298                select(defdata);
299                paInt x = getInt(i);
300                select(backup);
301                return x == getInt(i);
302        }
303        case 'f':
304        {
305                select(defdata);
306                double x = getDouble(i);
307                select(backup);
308                return x == getDouble(i);
309        }
310        case 's':
311        {
312                select(defdata);
313                SString x = getString(i);
314                select(backup);
315                return x == getString(i);
316        }
317        }
318        return 1;
319}
320
321void 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))
333                {
334                        if (defdata && isequal(i, defdata))
335                                needlabel = 1;
336                        else
337                        {
338                                if (!first) t += ", ";
339#ifndef SAVE_ALL_NAMES
340#ifdef SAVE_SELECTED_NAMES
341                                if (needlabel || all_names || !(fl & PARAM_CANOMITNAME))
342#else
343                                if (needlabel)
344#endif
345#endif
346                                {
347                                        t += p; t += "="; needlabel = 0;
348                                }
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;
365                        }
366                }
367        if (addcr)
368                t += "\n";
369        f += t;
370}
371
372int ParamInterface::load(VirtFILE* f,bool warn_unknown_fields,bool *abortable)
373{
374        SString buf;
375        int i;
376        const char *p, *p0;
377        int p_len;
378        bool loaded;
379        int fields_loaded = 0;
380        while ( ((!abortable)||(!*abortable)) && loadSStringLine(f, buf) )
381        {
382                const char* t = (const char*)buf;
383                p0 = t; while ((*p0 == ' ') || (*p0 == '\t')) p0++;
384                if (!*p0) break;
385                if (p0[0]=='#') continue;
386                p = strchr(p0, ':'); if (!p) continue;
387                p_len = (int)(p - p0);
388                loaded = false;
389                if (p_len && ((i = findIdn(p0, p_len)) >= 0))
390                {
391                if (!(flags(i)&PARAM_DONTLOAD))
392                   {
393                        if (p0[p_len + 1] == '~')
394                        {
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);
401                        }
402                        else
403                        {
404                                set(i, p0 + p_len + 1);
405                        }
406                        fields_loaded++;
407                        loaded = true;
408                   }
409                }
410                else if (warn_unknown_fields)
411                        {
412                        SString name(p0,p_len);
413                        FMprintf("ParamInterface","load",FMLV_WARN,"Unknown property '%s' while reading object '%s' (ignored)",(const char*)name,getName());
414                        }
415
416                if ((!loaded) && (p0[p_len + 1] == '~'))
417                { // eat unrecognized multiline field
418                        SString s;
419                        czytdotyldy(f, s);
420                        int ch; while ((ch = fgetc(f)) != EOF) if (ch == '\n') break;
421                }
422        }
423        return fields_loaded;
424}
425
426
427/*
428SString SimpleAbstractParam::getString(int i)
429{
430char *t;
431switch (*(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        }
448return get(i);
449}
450*/
451
452int ParamInterface::findId(const char* n)
453{
454        int i; const char *p;
455        for (i = 0; p = id(i); i++) if (!strcmp(n, p)) return i;
456        return -1;
457}
458
459int ParamInterface::findIdn(const char* naz, int n)
460{
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;
464}
465
466void ParamInterface::get(int i, ExtValue &ret)
467{
468        switch (type(i)[0])
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;
474        case 'x':       ret = getExtValue(i); break;
475        default: FMprintf("ParamInterface", "get", FMLV_ERROR, "'%s.%s' is not a field", getName(), id(i));
476        }
477}
478
479static bool stringIsNumeric(const char* str)
480{//   /-?.?[0-9]+/
481        if (!str) return false;
482        if (*str == '-') str++;
483        if (*str == '.') str++;
484        return isdigit(*str) != 0;
485}
486
487int ParamInterface::setInt(int i, const char* str)
488{
489        if (!stringIsNumeric(str))
490        {
491                paInt a, b, c;
492                if (getMinMax(i, a, b, c) >= 3)
493                        return setInt(i, c);
494                else
495                        return setInt(i, (paInt)0);
496        }
497        else
498                return setInt(i, ExtValue::getInt(str));
499}
500
501int ParamInterface::setDouble(int i, const char* str)
502{
503        if (!stringIsNumeric(str))
504        {
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        }
511        else
512                return setDouble(i, ExtValue::getDouble(str));
513}
514
515int ParamInterface::set(int i, const ExtValue &v)
516{
517        switch (type(i)[0])
518        {
519        case 'd':
520                if ((v.type == TInt) || (v.type == TDouble)) return setInt(i, v.getInt());
521                else
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                }
527        case 'f':
528                if ((v.type == TInt) || (v.type == TDouble)) return setDouble(i, v.getDouble());
529                else
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));
539        }
540        return 0;
541}
542
543int ParamInterface::set(int i, const char *v)
544{
545        char typ=type(i)[0];
546        switch (typ)
547        {
548        case 'd': return setInt(i, v);
549        case 'f': return setDouble(i, v);
550        case 's': { SString t(v); return setString(i, t); }
551        case 'x': case 'o':
552        {
553                ExtValue e;
554                const char* after;
555                if (!strncmp(v, SERIALIZATION_PREFIX, strlen(SERIALIZATION_PREFIX)))
556                {
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));
560                }
561                else if ((after = e.parseNumber(v)) && (*after == 0)) //consumed the whole string
562                {
563                        //OK!
564                }
565                else
566                {
567                        e.setString(SString(v));
568                }
569                if (typ=='x')
570                        return setExtValue(i, e);
571                else
572                        return setObject(i, e.getObject());
573        }
574        }
575        return 0;
576}
577
578SString ParamInterface::getText(int i)
579{
580        const char *t;
581        if ((*(t = type(i))) == 'd')
582        {
583                for (int j = getInt(i); j >= 0; j--) if (t) t = strchr(t + 1, '~');
584                if (t)
585                {
586                        t++;
587                        const char *t2 = strchr(t, '~');
588                        if (!t2) t2 = t + strlen(t);
589                        return SString(t, (int)(t2 - t));
590                }
591        }
592        return get(i);
593}
594
595SString ParamInterface::get(int i)
596{
597        switch (type(i)[0])
598        {
599        case 'd': return SString::valueOf(getInt(i));
600        case 'f': return SString::valueOf(getDouble(i));
601        case 's': return getString(i);
602        }
603        ExtValue v;
604        get(i, v);
605        return v.getString();
606}
607
608
609//////////////////////////////// PARAM ////////////////////////////////////
610
611#ifdef DEBUG
612void SimpleAbstractParam::sanityCheck(int i)
613{
614ParamEntry *pe=entry(i);
615
616const char* t=pe->type;
617const char* err=NULL;
618
619if (*t=='p')
620        {
621        if (pe->fun1==NULL)
622                err="no procedure defined";
623        }
624else
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        }
632if (err!=NULL)
633        FMprintf("SimpleAbstractParam","sanityCheck", FMLV_ERROR,
634                 "Invalid ParamEntry for %s.%s (%s)", getName(), pe->id, err);
635}       
636#endif
637
638void *SimpleAbstractParam::getTarget(int i)
639{
640        return (void*)(((char*)object) + entry(i)->offset);
641        //return &(object->*(entry(i)->fldptr));
642}
643
644///////// get
645
646#ifdef DEBUG
647#define SANITY_CHECK(i) sanityCheck(i)
648#else
649#define SANITY_CHECK(i)
650#endif
651
652paInt SimpleAbstractParam::getInt(int i)
653{
654        SANITY_CHECK(i);
655        ExtValue v;
656        ParamEntry *pe = entry(i);
657        if (pe->fun1)
658        {
659                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
660                return v.getInt();
661        }
662        else
663        {
664                void *target = getTarget(i);
665                return *((paInt*)target);
666        }
667}
668
669double SimpleAbstractParam::getDouble(int i)
670{
671        SANITY_CHECK(i);
672        ExtValue v;
673        ParamEntry *pe = entry(i);
674        if (pe->fun1)
675        {
676                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
677                return v.getDouble();
678        }
679        else
680        {
681                void *target = getTarget(i);
682                return *((double*)target);
683        }
684}
685
686SString SimpleAbstractParam::getString(int i)
687{
688        SANITY_CHECK(i);
689        ExtValue v;
690        ParamEntry *pe = entry(i);
691        if (pe->fun1)
692        {
693                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
694                return v.getString();
695        }
696        else
697        {
698                void *target = getTarget(i);
699                return *((SString*)target);
700        }
701}
702
703ExtObject SimpleAbstractParam::getObject(int i)
704{
705        SANITY_CHECK(i);
706        ExtValue v;
707        ParamEntry *pe = entry(i);
708        if (pe->fun1)
709        {
710                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
711                return v.getObject();
712        }
713        else
714        {
715                void *target = getTarget(i);
716                return *((ExtObject*)target);
717        }
718}
719
720ExtValue SimpleAbstractParam::getExtValue(int i)
721{
722        SANITY_CHECK(i);
723        ExtValue v;
724        ParamEntry *pe = entry(i);
725        if (pe->fun1)
726        {
727                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
728                return v;
729        }
730        else
731        {
732                void *target = getTarget(i);
733                return *((ExtValue*)target);
734        }
735}
736
737
738//////// set
739
740int SimpleAbstractParam::setInt(int i, paInt x)
741{
742        SANITY_CHECK(i);
743        ExtValue v;
744        ParamEntry *pe = entry(i);
745        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
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;
748        int result = 0;
749        const char* t = pe->type + 1;
750        while (*t) if (*t == ' ') break; else t++;
751        if (sscanf(t, PA_INT_SCANF " " PA_INT_SCANF, &a, &b) == 2)
752                if (a <= b) // if max<min then the min/max constraint check is not supported
753                {
754                        if (x<a) { x = a; result = PSET_HITMIN; }
755                        else if (x>b) { x = b; result = PSET_HITMAX; }
756                }
757
758        if (pe->fun2)
759        {
760                v.setInt(x);
761                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
762        }
763        else
764        {
765                void *target = getTarget(i);
766                if (dontcheckchanges || (*((paInt*)target) != x))
767                {
768                        result |= PSET_CHANGED;
769                        *((paInt*)target) = x;
770                }
771        }
772        messageOnExceedRange(i, result, xcopy);
773        return result;
774}
775
776int SimpleAbstractParam::setDouble(int i, double x)
777{
778        SANITY_CHECK(i);
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
789                {
790                        if (x<a) { x = a; result = PSET_HITMIN; }
791                        else if (x>b) { x = b; result = PSET_HITMAX; }
792                }
793
794        if (pe->fun2)
795        {
796                v.setDouble(x);
797                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
798        }
799        else
800        {
801                void *target = getTarget(i);
802                if (dontcheckchanges || (*((double*)target) != x))
803                {
804                        result |= PSET_CHANGED;
805                        *((double*)target) = x;
806                }
807        }
808        messageOnExceedRange(i, result, xcopy);
809        return result;
810}
811
812int SimpleAbstractParam::setString(int i, const SString& x)
813{
814        SANITY_CHECK(i);
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++;
823        int a = 0, b = 0;
824        int result = 0;
825        if (sscanf(t, "%d %d", &a, &b) == 2) //using getMinMax would also get default value, which is not needed here
826        {
827                if ((x.len() > b) && (b > 0))
828                {
829                        vs = x.substr(0, b);
830                        xx = &vs;
831                        result |= PSET_HITMAX;
832                }
833        }
834
835        if (pe->fun2)
836        {
837                v.setString(*xx);
838                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
839        }
840        else
841        {
842                void *target = getTarget(i);
843                if (dontcheckchanges || (!(*((SString*)target) == *xx)))
844                {
845                        result |= PSET_CHANGED;
846                        *((SString*)target) = *xx;
847                }
848        }
849        messageOnExceedRange(i, result, xcopy);
850        return result;
851}
852
853int SimpleAbstractParam::setObject(int i, const ExtObject& x)
854{
855        SANITY_CHECK(i);
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)
861        {
862                v.setObject(x);
863                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
864                messageOnExceedRange(i, result, xcopy);
865                return result;
866        }
867        else
868        {
869                void *target = getTarget(i);
870                *((ExtObject*)target) = x;
871                return PSET_CHANGED;
872        }
873}
874
875int SimpleAbstractParam::setExtValue(int i, const ExtValue& x)
876{
877        SANITY_CHECK(i);
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)
882        {
883                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x);
884                messageOnExceedRange(i, result, xcopy);
885                return result;
886        }
887        else
888        {
889                void *target = getTarget(i);
890                *((ExtValue*)target) = x;
891                return PSET_CHANGED;
892        }
893}
894
895void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret)
896{
897        SANITY_CHECK(i);
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
903        {
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);
906                ret->setInvalid();
907        }
908}
909
910void SimpleAbstractParam::setDefault()
911{
912        bool save = dontcheckchanges;
913        dontcheckchanges = 1;
914        ParamInterface::setDefault();
915        dontcheckchanges = save;
916}
917
918void SimpleAbstractParam::setDefault(int i)
919{
920        bool save = dontcheckchanges;
921        dontcheckchanges = 1;
922        ParamInterface::setDefault(i);
923        dontcheckchanges = save;
924}
925
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);...
931static const char *getline(const SString &s, int &poz, int &len)
932{
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(); }
937        else { poz = (int)(lf - (const char*)s) + 1; if (poz > s.len()) poz = s.len(); }
938        while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break;
939        len = (int)(lf - beg) + 1;
940        return beg;
941}
942
943int ParamInterface::load2(const SString &s, int &poz)
944{
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;
958
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;
967
968                comma_sign = strchrlimit(t, ',', end); if (!comma_sign) comma_sign = end;
969                quote = strchrlimit(t, '\"', comma_sign);
970                if (quote)
971                {
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);
979                }
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                {
993                        tmpi = findIdn(t, (int)(equals_sign - t));
994                        i = tmpi;
995                        if (tmpi < 0)
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                                }
1000                        t = equals_sign + 1; // t=value
1001                }
1002#ifdef WARN_MISSING_NAME
1003                else
1004#ifdef SAVE_SELECTED_NAMES
1005                        if (!(flags(i)&PARAM_CANOMITNAME))
1006#endif
1007                        {
1008                                FMprintf("Param", "load2", FMLV_WARN, "Missing property name in '%s' (assuming '%s')",
1009                                        getName(), id(i) ? id(i) : "unknown property?");
1010                        }
1011#endif
1012                if ((i >= 0) && id(i))
1013                {
1014                        value = t;
1015                        if (quote)
1016                        {
1017                                tmpvalue.copyFrom(quote + 1, (int)(quote2 - quote) - 1);
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;
1033                }
1034
1035                if (i >= 0) i++;
1036#ifdef __CODEGUARD__
1037                if (comma_sign<end-1) t=comma_sign+1; else return fields_loaded;
1038#else
1039                t = comma_sign + 1;
1040#endif
1041        }
1042        return fields_loaded;
1043}
1044
1045int Param::grmember(int g, int a)
1046{
1047        if ((getGroupCount() < 2) && (!g))
1048                return (a < getPropCount()) ? a : -9999;
1049
1050        ParamEntry *e = entry(0);
1051        int x = 0, i = 0;
1052        for (; e->id; i++, e++)
1053        {
1054                if (e->group == g)
1055                        if (a == x) return i; else x++;
1056        }
1057        return -9999;
1058}
Note: See TracBrowser for help on using the repository browser.