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

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

More strict parsing of int and float numbers from string

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