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

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

Sources support both 32-bit and 64-bit, and more compilers

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