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

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

String-type properties can now have a default value stored in their definition, just like numerics

  • Property svn:eol-style set to native
File size: 23.4 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)
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                p = strchr(p0, ':'); if (!p) continue;
387                p_len = (int)(p - p0);
388                loaded = false;
389                if (p_len && ((i = findIdn(p0, p_len)) >= 0) && (!(flags(i)&PARAM_DONTLOAD)))
390                {
391                        if (p0[p_len + 1] == '~')
392                        {
393                                SString s;
394                                czytdotyldy(f, s);
395                                removeCR(s);
396                                int ch; while ((ch = fgetc(f)) != EOF) if (ch == '\n') break;
397                                unquoteTilde(s);
398                                set(i, (const char*)s);
399                        }
400                        else
401                        {
402                                set(i, p0 + p_len + 1);
403                        }
404                        fields_loaded++;
405                        loaded = true;
406                }
407                if ((!loaded) && (p0[p_len + 1] == '~'))
408                { // eat unrecognized multiline field
409                        SString s;
410                        czytdotyldy(f, s);
411                        int ch; while ((ch = fgetc(f)) != EOF) if (ch == '\n') break;
412                }
413        }
414        return fields_loaded;
415}
416
417
418/*
419SString SimpleAbstractParam::getString(int i)
420{
421char *t;
422switch (*(t=type(i)))
423        {
424        case 'd':
425        {
426        for (i=atol(get(i));i>=0;i--) if (t) t=strchr(t+1,'~');
427        if (t)
428                {
429                t++;
430                char *t2=strchr(t,'~');
431                if (!t2) t2=t+strlen(t);
432                SString str;
433                strncpy(str.directWrite(t2-t),t,t2-t);
434                str.endWrite(t2-t);
435                return str;
436                }
437        }
438        }
439return get(i);
440}
441*/
442
443int ParamInterface::findId(const char* n)
444{
445        int i; const char *p;
446        for (i = 0; p = id(i); i++) if (!strcmp(n, p)) return i;
447        return -1;
448}
449
450int ParamInterface::findIdn(const char* naz, int n)
451{
452        int i; const char *p;
453        for (i = 0; p = id(i); i++) if ((!strncmp(naz, p, n)) && (!p[n])) return i;
454        return -1;
455}
456
457void ParamInterface::get(int i, ExtValue &ret)
458{
459        switch (type(i)[0])
460        {
461        case 'd':       ret.setInt(getInt(i)); break;
462        case 'f':       ret.setDouble(getDouble(i)); break;
463        case 's':       ret.setString(getString(i)); break;
464        case 'o':       ret.setObject(getObject(i)); break;
465        case 'x':       ret = getExtValue(i); break;
466        default: FMprintf("ParamInterface", "get", FMLV_ERROR, "'%s.%s' is not a field", getName(), id(i));
467        }
468}
469
470static bool stringIsNumeric(const char* str)
471{//   /-?.?[0-9]+/
472        if (!str) return false;
473        if (*str == '-') str++;
474        if (*str == '.') str++;
475        return isdigit(*str) != 0;
476}
477
478int ParamInterface::setInt(int i, const char* str)
479{
480        if (!stringIsNumeric(str))
481        {
482                paInt a, b, c;
483                if (getMinMax(i, a, b, c) >= 3)
484                        return setInt(i, c);
485                else
486                        return setInt(i, (paInt)0);
487        }
488        else
489                return setInt(i, ExtValue::getInt(str));
490}
491
492int ParamInterface::setDouble(int i, const char* str)
493{
494        if (!stringIsNumeric(str))
495        {
496                double a, b, c;
497                if (getMinMax(i, a, b, c) >= 3)
498                        return setDouble(i, c);
499                else
500                        return setDouble(i, (double)0);
501        }
502        else
503                return setDouble(i, ExtValue::getDouble(str));
504}
505
506int ParamInterface::set(int i, const ExtValue &v)
507{
508        switch (type(i)[0])
509        {
510        case 'd':
511                if ((v.type == TInt) || (v.type == TDouble)) return setInt(i, v.getInt());
512                else
513                {
514                        if (v.type == TObj)
515                                FMprintf("ParamInterface", "set", FMLV_WARN, "Getting integer value from object reference (%s)", (const char*)v.getString());
516                        return setInt(i, (const char*)v.getString());
517                }
518        case 'f':
519                if ((v.type == TInt) || (v.type == TDouble)) return setDouble(i, v.getDouble());
520                else
521                {
522                        if (v.type == TObj)
523                                FMprintf("ParamInterface", "set", FMLV_WARN, "Getting floating point value from object reference (%s)", (const char*)v.getString());
524                        return setDouble(i, (const char*)v.getString());
525                }
526        case 's': { SString t = v.getString(); return setString(i, t); }
527        case 'o': return setObject(i, v.getObject());
528        case 'x': return setExtValue(i, v);
529        default: FMprintf("ParamInterface", "set", FMLV_ERROR, "'%s.%s' is not a field", getName(), id(i));
530        }
531        return 0;
532}
533
534int ParamInterface::set(int i, const char *v)
535{
536        switch (type(i)[0])
537        {
538        case 'd': return setInt(i, v);
539        case 'f': return setDouble(i, v);
540        case 's': { SString t(v); return setString(i, t); }
541        case 'x':
542        {
543                ExtValue e;
544                const char* after;
545                if (!strncmp(v, SERIALIZATION_PREFIX, strlen(SERIALIZATION_PREFIX)))
546                {
547                        after = e.deserialize(v + strlen(SERIALIZATION_PREFIX));
548                        if ((after == NULL) || (*after))
549                                FMprintf("ParamInterface", "set", FMLV_WARN, "serialization format mismatch in %s.%s", (getName() ? getName() : "<Unknown>"), id(i));
550                }
551                else if ((after = e.parseNumber(v)) && (*after == 0)) //consumed the whole string
552                {
553                        //OK!
554                }
555                else
556                {
557                        e.setString(SString(v));
558                }
559                return setExtValue(i, e);
560        }
561        }
562        return 0;
563}
564
565SString ParamInterface::getText(int i)
566{
567        const char *t;
568        if ((*(t = type(i))) == 'd')
569        {
570                for (int j = getInt(i); j >= 0; j--) if (t) t = strchr(t + 1, '~');
571                if (t)
572                {
573                        t++;
574                        const char *t2 = strchr(t, '~');
575                        if (!t2) t2 = t + strlen(t);
576                        return SString(t, (int)(t2 - t));
577                }
578        }
579        return get(i);
580}
581
582SString ParamInterface::get(int i)
583{
584        switch (type(i)[0])
585        {
586        case 'd': return SString::valueOf(getInt(i));
587        case 'f': return SString::valueOf(getDouble(i));
588        case 's': return getString(i);
589        }
590        ExtValue v;
591        get(i, v);
592        return v.getString();
593}
594
595
596//////////////////////////////// PARAM ////////////////////////////////////
597
598#ifdef DEBUG
599void SimpleAbstractParam::sanityCheck(int i)
600{
601ParamEntry *pe=entry(i);
602
603const char* t=pe->type;
604const char* err=NULL;
605
606if (*t=='p')
607        {
608        if (pe->fun1==NULL)
609                err="no procedure defined";
610        }
611else
612        {
613        if (!(pe->flags & PARAM_READONLY))
614                { //write access
615                if ((pe->fun2==NULL)&&(pe->offset==PARAM_ILLEGAL_OFFSET))
616                        err="no field defined (GETONLY without PARAM_READONLY?)";
617                }
618        }
619if (err!=NULL)
620        FMprintf("SimpleAbstractParam","sanityCheck", FMLV_ERROR,
621                 "Invalid ParamEntry for %s.%s (%s)", getName(), pe->id, err);
622}       
623#endif
624
625void *SimpleAbstractParam::getTarget(int i)
626{
627        return (void*)(((char*)object) + entry(i)->offset);
628        //return &(object->*(entry(i)->fldptr));
629}
630
631///////// get
632
633#ifdef DEBUG
634#define SANITY_CHECK(i) sanityCheck(i)
635#else
636#define SANITY_CHECK(i)
637#endif
638
639paInt SimpleAbstractParam::getInt(int i)
640{
641        SANITY_CHECK(i);
642        ExtValue v;
643        ParamEntry *pe = entry(i);
644        if (pe->fun1)
645        {
646                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
647                return v.getInt();
648        }
649        else
650        {
651                void *target = getTarget(i);
652                return *((paInt*)target);
653        }
654}
655
656double SimpleAbstractParam::getDouble(int i)
657{
658        SANITY_CHECK(i);
659        ExtValue v;
660        ParamEntry *pe = entry(i);
661        if (pe->fun1)
662        {
663                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
664                return v.getDouble();
665        }
666        else
667        {
668                void *target = getTarget(i);
669                return *((double*)target);
670        }
671}
672
673SString SimpleAbstractParam::getString(int i)
674{
675        SANITY_CHECK(i);
676        ExtValue v;
677        ParamEntry *pe = entry(i);
678        if (pe->fun1)
679        {
680                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
681                return v.getString();
682        }
683        else
684        {
685                void *target = getTarget(i);
686                return *((SString*)target);
687        }
688}
689
690ExtObject SimpleAbstractParam::getObject(int i)
691{
692        SANITY_CHECK(i);
693        ExtValue v;
694        ParamEntry *pe = entry(i);
695        if (pe->fun1)
696        {
697                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
698                return v.getObject();
699        }
700        else
701        {
702                void *target = getTarget(i);
703                return *((ExtObject*)target);
704        }
705}
706
707ExtValue SimpleAbstractParam::getExtValue(int i)
708{
709        SANITY_CHECK(i);
710        ExtValue v;
711        ParamEntry *pe = entry(i);
712        if (pe->fun1)
713        {
714                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
715                return v;
716        }
717        else
718        {
719                void *target = getTarget(i);
720                return *((ExtValue*)target);
721        }
722}
723
724
725//////// set
726
727int SimpleAbstractParam::setInt(int i, paInt x)
728{
729        SANITY_CHECK(i);
730        ExtValue v;
731        ParamEntry *pe = entry(i);
732        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
733        paInt xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
734        paInt a = 0, b = 0;
735        int result = 0;
736        const char* t = pe->type + 1;
737        while (*t) if (*t == ' ') break; else t++;
738        if (sscanf(t, PA_INT_SCANF " " PA_INT_SCANF, &a, &b) == 2)
739                if (a <= b) // if max<min then the min/max constraint check is not supported
740                {
741                        if (x<a) { x = a; result = PSET_HITMIN; }
742                        else if (x>b) { x = b; result = PSET_HITMAX; }
743                }
744
745        if (pe->fun2)
746        {
747                v.setInt(x);
748                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
749        }
750        else
751        {
752                void *target = getTarget(i);
753                if (dontcheckchanges || (*((paInt*)target) != x))
754                {
755                        result |= PSET_CHANGED;
756                        *((paInt*)target) = x;
757                }
758        }
759        messageOnExceedRange(i, result, xcopy);
760        return result;
761}
762
763int SimpleAbstractParam::setDouble(int i, double x)
764{
765        SANITY_CHECK(i);
766        ExtValue v;
767        ParamEntry *pe = entry(i);
768        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
769        double xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
770        double a = 0, b = 0;
771        int result = 0;
772        const char* t = pe->type + 1;
773        while (*t) if (*t == ' ') break; else t++;
774        if (sscanf(t, "%lg %lg", &a, &b) == 2)
775                if (a <= b) // if max<min then the min/max constraint check is not supported
776                {
777                        if (x<a) { x = a; result = PSET_HITMIN; }
778                        else if (x>b) { x = b; result = PSET_HITMAX; }
779                }
780
781        if (pe->fun2)
782        {
783                v.setDouble(x);
784                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
785        }
786        else
787        {
788                void *target = getTarget(i);
789                if (dontcheckchanges || (*((double*)target) != x))
790                {
791                        result |= PSET_CHANGED;
792                        *((double*)target) = x;
793                }
794        }
795        messageOnExceedRange(i, result, xcopy);
796        return result;
797}
798
799int SimpleAbstractParam::setString(int i, const SString& x)
800{
801        SANITY_CHECK(i);
802        ExtValue v;
803        SString vs;
804        const SString *xx = &x;
805        ParamEntry *pe = entry(i);
806        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
807        SString xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
808        const char* t = pe->type + 1;
809        while (*t) if (*t == ' ') break; else t++;
810        int a = 0, b = 0;
811        int result = 0;
812        if (sscanf(t, "%d %d", &a, &b) == 2) //using getMinMax would also get default value, which is not needed here
813        {
814                if ((x.len() > b) && (b > 0))
815                {
816                        vs = x.substr(0, b);
817                        xx = &vs;
818                        result |= PSET_HITMAX;
819                }
820        }
821
822        if (pe->fun2)
823        {
824                v.setString(*xx);
825                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
826        }
827        else
828        {
829                void *target = getTarget(i);
830                if (dontcheckchanges || (!(*((SString*)target) == *xx)))
831                {
832                        result |= PSET_CHANGED;
833                        *((SString*)target) = x;
834                }
835        }
836        messageOnExceedRange(i, result, xcopy);
837        return result;
838}
839
840int SimpleAbstractParam::setObject(int i, const ExtObject& x)
841{
842        SANITY_CHECK(i);
843        ExtValue v;
844        ParamEntry *pe = entry(i);
845        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
846        ExtObject xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
847        if (pe->fun2)
848        {
849                v.setObject(x);
850                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
851                messageOnExceedRange(i, result, xcopy);
852                return result;
853        }
854        else
855        {
856                void *target = getTarget(i);
857                *((ExtObject*)target) = x;
858                return PSET_CHANGED;
859        }
860}
861
862int SimpleAbstractParam::setExtValue(int i, const ExtValue& x)
863{
864        SANITY_CHECK(i);
865        ParamEntry *pe = entry(i);
866        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
867        ExtValue xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
868        if (pe->fun2)
869        {
870                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x);
871                messageOnExceedRange(i, result, xcopy);
872                return result;
873        }
874        else
875        {
876                void *target = getTarget(i);
877                *((ExtValue*)target) = x;
878                return PSET_CHANGED;
879        }
880}
881
882void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret)
883{
884        SANITY_CHECK(i);
885        ParamEntry *pe = entry(i);
886        if (!pe) return;
887        if (pe->fun1 && (pe->type[0] == 'p'))
888                (*(void(*)(void*, ExtValue*, ExtValue*))pe->fun1)(object, args, ret);
889        else
890        {
891                FMprintf("SimpleAbstractParam", "call", FMLV_ERROR,
892                        (*pe->type != 'p') ? "'%s.%s' is not a function" : "Internal error - undefined function pointer for '%s.%s'", getName(), pe->id);
893        }
894}
895
896void SimpleAbstractParam::setDefault(bool numericonly)
897{
898        bool save = dontcheckchanges;
899        dontcheckchanges = 1;
900        ParamInterface::setDefault(numericonly);
901        dontcheckchanges = save;
902}
903
904void SimpleAbstractParam::setDefault(int i, bool numericonly)
905{
906        bool save = dontcheckchanges;
907        dontcheckchanges = 1;
908        ParamInterface::setDefault(i, numericonly);
909        dontcheckchanges = save;
910}
911
912// Returns the address of the beginning of the line.
913// len = line length (without \n).
914// 0 may mean the line with length=0 or the end of the SString.
915// poz is advanced to the beginning of the next line.
916// A typical loop: for(poz=0;poz<s.d;) {line=getline(s,poz,len);...
917static const char *getline(const SString &s, int &poz, int &len)
918{
919        const char *beg = (const char*)s + poz;
920        if (poz >= s.len()) { poz = s.len(); len = 0; return (const char*)s + s.len(); }
921        const char *lf = strchr(beg, '\n');
922        if (!lf) { lf = (const char*)s + s.len() - 1; poz = s.len(); }
923        else { poz = (int)(lf - (const char*)s) + 1; if (poz > s.len()) poz = s.len(); }
924        while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break;
925        len = (int)(lf - beg) + 1;
926        return beg;
927}
928
929int ParamInterface::load2(const SString &s, int &poz)
930{
931        int i; // the index number of the parameter
932        int tmpi;
933        int len;
934        int ret;
935        int fields_loaded = 0;
936        const char *t, *lin, *end;
937        const char *equals_sign, *comma_sign;
938        char remember;
939        const char *quote, *quote2;
940        const char *value, *valstop;
941        SString tmpvalue;
942        if (poz >= s.len()) return fields_loaded;
943        t = (const char*)s + poz;
944
945        lin = getline(s, poz, len); // all fields must be encoded in a single line
946        if (!len) return fields_loaded; // empty line = end
947        i = 0;
948        end = lin + len;
949        while (t < end)
950        {
951                // processing a single field
952                while (strchr(" \n\r\t", *t)) if (t<end) t++; else return fields_loaded;
953
954                comma_sign = strchrlimit(t, ',', end); if (!comma_sign) comma_sign = end;
955                quote = strchrlimit(t, '\"', comma_sign);
956                if (quote)
957                {
958                        quote2 = skipQuoteString(quote + 1, end);
959                        if (quote2>comma_sign)
960                        {
961                                comma_sign = strchrlimit(quote2 + 1, ',', end);
962                                if (!comma_sign) comma_sign = end;
963                        }
964                        equals_sign = strchrlimit(t, '=', quote);
965                }
966                else
967                {
968                        equals_sign = strchrlimit(t, '=', comma_sign);
969                        quote2 = 0;
970                }
971                if (equals_sign == t) { t++; equals_sign = 0; }
972                if (comma_sign == t)    // skip empty value
973                {
974                        t++; i++;
975                        continue;
976                }
977                if (equals_sign) // have parameter name
978                {
979                        tmpi = findIdn(t, (int)(equals_sign - t));
980                        i = tmpi;
981                        if (tmpi < 0)
982                                FMprintf("Param", "load2", FMLV_WARN, "Unknown property name for '%s' (ignored)", getName());
983                        t = equals_sign + 1; // t=value
984                }
985#ifdef WARN_MISSING_NAME
986                else
987#ifdef SAVE_SELECTED_NAMES
988                        if (!(flags(i)&PARAM_CANOMITNAME))
989#endif
990                        {
991                                FMprintf("Param", "load2", FMLV_WARN, "Missing property name in '%s' (assuming '%s')",
992                                        getName(), id(i) ? id(i) : "unknown property?");
993                        }
994#endif
995                if ((i >= 0) && id(i))
996                {
997                        value = t;
998                        if (quote)
999                        {
1000                                tmpvalue.copyFrom(quote + 1, (int)(quote2 - quote) - 1);
1001                                sstringUnquote(tmpvalue);
1002                                value = tmpvalue;
1003                                valstop = quote2;
1004                        }
1005                        else
1006                                if (comma_sign < end) valstop = comma_sign; else valstop = end;
1007
1008                        remember = *valstop;
1009                        *(char*)valstop = 0;
1010                        ret = set(i, value);
1011                        fields_loaded++;
1012                        if (ret&(PSET_HITMAX | PSET_HITMIN))
1013                                FMprintf("Param", "load2", FMLV_WARN, "Adjusted '%s' in '%s' (was too %s)",
1014                                id(i), getName(), (ret&PSET_HITMAX) ? "big" : "small");
1015                        *(char*)valstop = remember;
1016                }
1017
1018                if (i >= 0) i++;
1019#ifdef __CODEGUARD__
1020                if (comma_sign<end-1) t=comma_sign+1; else return fields_loaded;
1021#else
1022                t = comma_sign + 1;
1023#endif
1024        }
1025        return fields_loaded;
1026}
1027
1028int Param::grmember(int g, int a)
1029{
1030        if ((getGroupCount() < 2) && (!g))
1031                return (a < getPropCount()) ? a : -9999;
1032
1033        ParamEntry *e = entry(0);
1034        int x = 0, i = 0;
1035        for (; e->id; i++, e++)
1036        {
1037                if (e->group == g)
1038                        if (a == x) return i; else x++;
1039        }
1040        return -9999;
1041}
Note: See TracBrowser for help on using the repository browser.