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

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

ParamInterface::load warns on unknown fields and skips comments

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