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

Last change on this file since 247 was 247, checked in by Maciej Komosinski, 10 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
RevLine 
[121]1// This file is a part of the Framsticks GDK.
[197]2// Copyright (C) 1999-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
[109]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
[154]19ParamEntry empty_paramtab[] =
20{ { "Empty", 1, 0, "Empty", }, { 0, 0, 0, }, };
[109]21
[154]22static void czytdotyldy(VirtFILE *f, SString &s)
[109]23{
[154]24        SString temp;
25        int z;
26        char last_char = 0;
27        while ((z = fgetc(f)) != EOF)
[109]28        {
[154]29                if (z == '~')
30                        if (last_char != '\\') break;
31                last_char = (char)z;
32                temp += last_char;
[109]33        }
[154]34        s = temp;
[109]35}
36
[154]37static const char *strchrlimit(const char *t, int ch, const char *limit)
[109]38{
[247]39        int n = (int)(limit - t);
[154]40        for (; (n > 0) && *t; t++, n--)
41                if (*t == ch) return t;
42        return 0;
[109]43}
44
45void ParamInterface::copyFrom(ParamInterface *src)
46{
[154]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                }
[109]59}
60
61void ParamInterface::quickCopyFrom(ParamInterface *src)
62{
[154]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                }
[109]72}
73
[247]74int ParamInterface::getMinMax(int prop, paInt& minumum, paInt& maximum, paInt &def)
[109]75{
[154]76        const char* t = type(prop) + 1;
77        while (*t) if (*t == ' ') break; else t++;
[247]78        return sscanf(t, PA_INT_SCANF " " PA_INT_SCANF " " PA_INT_SCANF, &minumum, &maximum, &def);
[109]79}
80
[154]81int ParamInterface::getMinMax(int prop, double& minumum, double& maximum, double& def)
[109]82{
[154]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);
[109]86}
87
88void ParamInterface::setDefault(bool numericonly)
89{
[154]90        int n = getPropCount();
91        for (int i = 0; i < n; i++)
92                setDefault(i, numericonly);
[109]93}
94
95void ParamInterface::setMin()
96{
[154]97        int n = getPropCount();
98        for (int i = 0; i < n; i++)
99                setMin(i);
[109]100}
101
102void ParamInterface::setMax()
103{
[154]104        int n = getPropCount();
105        for (int i = 0; i < n; i++)
106                setMax(i);
[109]107}
108
[154]109void ParamInterface::setDefault(int i, bool numericonly)
[109]110{
[154]111        const char *t = type(i);
112        switch (*t)
[109]113        {
114        case 'f':
115        {
[154]116                double a = 0, b = 0, c = 0;
117                if (getMinMax(i, a, b, c) < 3) c = a;
118                setDouble(i, c);
[109]119        }
[154]120                break;
[109]121        case 'd':
122        {
[247]123                paInt a = 0, b = 0, c = 0;
[154]124                if (getMinMax(i, a, b, c) < 3) c = a;
125                setInt(i, c);
[109]126        }
[154]127                break;
128        default: if (!numericonly) set(i, "");
[109]129        }
130}
131
132void ParamInterface::setMin(int i)
133{
[154]134        const char *t = type(i);
135        switch (*t)
[109]136        {
137        case 'f':
138        {
[154]139                double a = 0, b = 0, c = 0;
140                getMinMax(i, a, b, c);
141                setDouble(i, a);
[109]142        }
[154]143                break;
[109]144        case 'd':
145        {
[247]146                paInt a = 0, b = 0, c = 0;
[154]147                getMinMax(i, a, b, c);
148                setInt(i, a);
[109]149        }
[154]150                break;
151        default: set(i, "");
[109]152        }
153}
154
155void ParamInterface::setMax(int i)
156{
[154]157        const char *t = type(i);
158        switch (*t)
[109]159        {
160        case 'f':
161        {
[154]162                double a = 0, b = 0, c = 0;
163                getMinMax(i, a, b, c);
164                setDouble(i, b);
[109]165        }
[154]166                break;
[109]167        case 'd':
168        {
[247]169                paInt a = 0, b = 0, c = 0;
[154]170                getMinMax(i, a, b, c);
171                setInt(i, b);
[109]172        }
[154]173                break;
174        default: set(i, "");
[109]175        }
176}
177
178SString ParamInterface::getStringById(const char*prop)
179{int i=findId(prop); if (i>=0) return getString(i); else return SString();}
[247]180paInt ParamInterface::getIntById(const char*prop)
[109]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
[247]189int ParamInterface::setIntById(const char* prop,paInt v)
[109]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
[154]202int ParamInterface::save(VirtFILE* f, const char* altname, bool force)
[109]203{
[154]204        const char *p;
205        SString ws;
206        int err = 0, i;
207        bool withname = false;
208        if ((altname == NULL) || (altname[0] != 0))
[109]209        {
[154]210                err |= (fputs(altname ? altname : getName(), f) == EOF);
211                err |= (fputs(":\n", f) == EOF);
212                withname = true;
[109]213        }
[154]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;
[109]219}
220
[154]221const char* ParamInterface::SERIALIZATION_PREFIX = "@Serialized:";
[109]222
[154]223int ParamInterface::saveprop(VirtFILE* f, int i, const char* p, bool force)
[109]224{
[154]225        if ((flags(i)&PARAM_DONTSAVE) && (!force)) return 0;
226        const char *typ = type(i);
227        if ((*typ == 'p') || (*typ == 'o')) return 0;
[109]228
[154]229        const char *t, *w;
230        SString ws;
231        int err = 0, cr;
[109]232
[154]233        err |= (fputs(p, f) == EOF); fputc(':', f);
234        cr = 0;
235        if (*typ == 'x')
[109]236        {
[154]237                ExtValue ex;
238                get(i, ex);
239                ws = SString(SERIALIZATION_PREFIX) + ex.serialize();
[109]240        }
[154]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;
[109]251}
252
253
[154]254int SimpleAbstractParam::isequal(int i, void* defdata)
[109]255{ // defdata->member == object->member ?
[154]256        void *backup = object;
257        switch (type(i)[0])
[109]258        {
259        case 'd':
[154]260        {
[109]261                select(defdata);
[247]262                paInt x = getInt(i);
[109]263                select(backup);
[154]264                return x == getInt(i);
265        }
[109]266        case 'f':
[154]267        {
[109]268                select(defdata);
[154]269                double x = getDouble(i);
[109]270                select(backup);
[154]271                return x == getDouble(i);
272        }
[109]273        case 's':
[154]274        {
[109]275                select(defdata);
[154]276                SString x = getString(i);
[109]277                select(backup);
[154]278                return x == getString(i);
[109]279        }
[154]280        }
281        return 1;
[109]282}
283
[154]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))
[109]296                {
[154]297                        if (defdata && isequal(i, defdata))
298                                needlabel = 1;
299                        else
300                        {
301                                if (!first) t += ", ";
[109]302#ifndef SAVE_ALL_NAMES
303#ifdef SAVE_SELECTED_NAMES
[154]304                                if (needlabel || all_names || !(fl & PARAM_CANOMITNAME))
[109]305#else
[154]306                                if (needlabel)
[109]307#endif
308#endif
309                                {
[154]310                                        t += p; t += "="; needlabel = 0;
[109]311                                }
[154]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;
[109]328                        }
329                }
[154]330        if (addcr)
331                t += "\n";
332        f += t;
[109]333}
334
[144]335int ParamInterface::load(VirtFILE* f)
[109]336{
[154]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))
[109]344        {
[154]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;
[247]349                p_len = (int)(p - p0);
[154]350                loaded = false;
351                if (p_len && ((i = findIdn(p0, p_len)) >= 0) && (!(flags(i)&PARAM_DONTLOAD)))
[109]352                {
[154]353                        if (p0[p_len + 1] == '~')
[109]354                        {
[154]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);
[109]361                        }
[154]362                        else
[109]363                        {
[154]364                                set(i, p0 + p_len + 1);
[109]365                        }
[154]366                        fields_loaded++;
367                        loaded = true;
[109]368                }
[154]369                if ((!loaded) && (p0[p_len + 1] == '~'))
[109]370                { // eat unrecognized multiline field
[154]371                        SString s;
372                        czytdotyldy(f, s);
373                        int ch; while ((ch = fgetc(f)) != EOF) if (ch == '\n') break;
[109]374                }
375        }
[154]376        return fields_loaded;
[109]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{
[154]407        int i; const char *p;
408        for (i = 0; p = id(i); i++) if (!strcmp(n, p)) return i;
409        return -1;
[109]410}
411
[154]412int ParamInterface::findIdn(const char* naz, int n)
[109]413{
[154]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;
[109]417}
418
[154]419void ParamInterface::get(int i, ExtValue &ret)
[109]420{
[154]421        switch (type(i)[0])
[109]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;
[154]427        case 'x':       ret = getExtValue(i); break;
428        default: FMprintf("ParamInterface", "get", FMLV_ERROR, "'%s.%s' is not a field", getName(), id(i));
[109]429        }
430}
431
432static bool stringIsNumeric(const char* str)
433{//   /-?.?[0-9]+/
[154]434        if (!str) return false;
435        if (*str == '-') str++;
436        if (*str == '.') str++;
437        return isdigit(*str) != 0;
[109]438}
439
[154]440int ParamInterface::setInt(int i, const char* str)
[109]441{
[154]442        if (!stringIsNumeric(str))
[109]443        {
[247]444                paInt a, b, c;
[154]445                if (getMinMax(i, a, b, c) >= 3)
446                        return setInt(i, c);
447                else
[247]448                        return setInt(i, (paInt)0);
[154]449        }
[109]450        else
[154]451                return setInt(i, ExtValue::getInt(str));
[109]452}
453
[154]454int ParamInterface::setDouble(int i, const char* str)
[109]455{
[154]456        if (!stringIsNumeric(str))
[109]457        {
[154]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        }
[109]464        else
[154]465                return setDouble(i, ExtValue::getDouble(str));
[109]466}
467
[154]468int ParamInterface::set(int i, const ExtValue &v)
[109]469{
[154]470        switch (type(i)[0])
[109]471        {
[144]472        case 'd':
[154]473                if ((v.type == TInt) || (v.type == TDouble)) return setInt(i, v.getInt());
[144]474                else
[154]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                }
[144]480        case 'f':
[154]481                if ((v.type == TInt) || (v.type == TDouble)) return setDouble(i, v.getDouble());
[144]482                else
[154]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));
[109]492        }
[154]493        return 0;
[109]494}
495
[154]496int ParamInterface::set(int i, const char *v)
[109]497{
[154]498        switch (type(i)[0])
[109]499        {
[154]500        case 'd': return setInt(i, v);
501        case 'f': return setDouble(i, v);
502        case 's': { SString t(v); return setString(i, t); }
[109]503        case 'x':
504        {
[154]505                ExtValue e;
506                const char* after;
507                if (!strncmp(v, SERIALIZATION_PREFIX, strlen(SERIALIZATION_PREFIX)))
[109]508                {
[154]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));
[109]512                }
[154]513                else if ((after = e.parseNumber(v)) && (*after == 0)) //consumed the whole string
[109]514                {
[154]515                        //OK!
[109]516                }
[154]517                else
[109]518                {
[154]519                        e.setString(SString(v));
[109]520                }
[154]521                return setExtValue(i, e);
[109]522        }
523        }
[154]524        return 0;
[109]525}
526
527SString ParamInterface::getText(int i)
528{
[154]529        const char *t;
530        if ((*(t = type(i))) == 'd')
[109]531        {
[154]532                for (int j = getInt(i); j >= 0; j--) if (t) t = strchr(t + 1, '~');
533                if (t)
[109]534                {
[154]535                        t++;
536                        const char *t2 = strchr(t, '~');
537                        if (!t2) t2 = t + strlen(t);
[247]538                        return SString(t, (int)(t2 - t));
[109]539                }
540        }
[154]541        return get(i);
[109]542}
543
544SString ParamInterface::get(int i)
545{
[154]546        switch (type(i)[0])
[109]547        {
548        case 'd': return SString::valueOf(getInt(i));
549        case 'f': return SString::valueOf(getDouble(i));
550        case 's': return getString(i);
551        }
[154]552        ExtValue v;
553        get(i, v);
554        return v.getString();
[109]555}
556
557
558//////////////////////////////// PARAM ////////////////////////////////////
559
[230]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
[109]587void *SimpleAbstractParam::getTarget(int i)
588{
[154]589        return (void*)(((char*)object) + entry(i)->offset);
590        //return &(object->*(entry(i)->fldptr));
[109]591}
592
593///////// get
594
[230]595#ifdef DEBUG
596#define SANITY_CHECK(i) sanityCheck(i)
597#else
598#define SANITY_CHECK(i)
599#endif
600
[247]601paInt SimpleAbstractParam::getInt(int i)
[109]602{
[230]603        SANITY_CHECK(i);
[154]604        ExtValue v;
605        ParamEntry *pe = entry(i);
606        if (pe->fun1)
[109]607        {
[154]608                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
609                return v.getInt();
[109]610        }
[154]611        else
[109]612        {
[154]613                void *target = getTarget(i);
[247]614                return *((paInt*)target);
[109]615        }
616}
617
618double SimpleAbstractParam::getDouble(int i)
619{
[230]620        SANITY_CHECK(i);
[154]621        ExtValue v;
622        ParamEntry *pe = entry(i);
623        if (pe->fun1)
[109]624        {
[154]625                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
626                return v.getDouble();
[109]627        }
[154]628        else
[109]629        {
[154]630                void *target = getTarget(i);
631                return *((double*)target);
[109]632        }
633}
634
635SString SimpleAbstractParam::getString(int i)
636{
[230]637        SANITY_CHECK(i);
[154]638        ExtValue v;
639        ParamEntry *pe = entry(i);
640        if (pe->fun1)
[109]641        {
[154]642                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
643                return v.getString();
[109]644        }
[154]645        else
[109]646        {
[154]647                void *target = getTarget(i);
648                return *((SString*)target);
[109]649        }
650}
651
652ExtObject SimpleAbstractParam::getObject(int i)
653{
[230]654        SANITY_CHECK(i);
[154]655        ExtValue v;
656        ParamEntry *pe = entry(i);
657        if (pe->fun1)
[109]658        {
[154]659                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
660                return v.getObject();
[109]661        }
[154]662        else
[109]663        {
[154]664                void *target = getTarget(i);
665                return *((ExtObject*)target);
[109]666        }
667}
668
669ExtValue SimpleAbstractParam::getExtValue(int i)
670{
[230]671        SANITY_CHECK(i);
[154]672        ExtValue v;
673        ParamEntry *pe = entry(i);
674        if (pe->fun1)
[109]675        {
[154]676                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
677                return v;
[109]678        }
[154]679        else
[109]680        {
[154]681                void *target = getTarget(i);
682                return *((ExtValue*)target);
[109]683        }
684}
685
686
687//////// set
688
[247]689int SimpleAbstractParam::setInt(int i, paInt x)
[109]690{
[230]691        SANITY_CHECK(i);
[154]692        ExtValue v;
693        ParamEntry *pe = entry(i);
694        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
[247]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;
[154]697        int result = 0;
698        const char* t = pe->type + 1;
699        while (*t) if (*t == ' ') break; else t++;
[247]700        if (sscanf(t, PA_INT_SCANF " " PA_INT_SCANF, &a, &b) == 2)
[154]701                if (a <= b) // if max<min then the min/max constraint check is not supported
[109]702                {
[154]703                        if (x<a) { x = a; result = PSET_HITMIN; }
704                        else if (x>b) { x = b; result = PSET_HITMAX; }
[109]705                }
706
[154]707        if (pe->fun2)
[109]708        {
[154]709                v.setInt(x);
710                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[109]711        }
[154]712        else
[109]713        {
[154]714                void *target = getTarget(i);
[247]715                if (dontcheckchanges || (*((paInt*)target) != x))
[154]716                {
[109]717                        result |= PSET_CHANGED;
[247]718                        *((paInt*)target) = x;
[154]719                }
[109]720        }
[154]721        messageOnExceedRange(i, result, xcopy);
[109]722        return result;
723}
724
[154]725int SimpleAbstractParam::setDouble(int i, double x)
[109]726{
[230]727        SANITY_CHECK(i);
[154]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
[109]738                {
[154]739                        if (x<a) { x = a; result = PSET_HITMIN; }
740                        else if (x>b) { x = b; result = PSET_HITMAX; }
[109]741                }
742
[154]743        if (pe->fun2)
[109]744        {
[154]745                v.setDouble(x);
746                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[109]747        }
[154]748        else
[109]749        {
[154]750                void *target = getTarget(i);
751                if (dontcheckchanges || (*((double*)target) != x))
[109]752                {
[154]753                        result |= PSET_CHANGED;
754                        *((double*)target) = x;
[109]755                }
756        }
[154]757        messageOnExceedRange(i, result, xcopy);
[109]758        return result;
759}
760
[154]761int SimpleAbstractParam::setString(int i, const SString& x)
[109]762{
[230]763        SANITY_CHECK(i);
[154]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++;
[247]772        paInt a = 0, b = 0;
[154]773        int result = 0;
[247]774        if (sscanf(t, PA_INT_SCANF " " PA_INT_SCANF, &a, &b) == 2)
[109]775        {
[154]776                if ((x.len() > b) && (b > 0))
[109]777                {
[154]778                        vs = x.substr(0, b);
779                        xx = &vs;
780                        result |= PSET_HITMAX;
[109]781                }
782        }
783
[154]784        if (pe->fun2)
[109]785        {
[154]786                v.setString(*xx);
787                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[109]788        }
[154]789        else
[109]790        {
[154]791                void *target = getTarget(i);
792                if (dontcheckchanges || (!(*((SString*)target) == *xx)))
[109]793                {
[154]794                        result |= PSET_CHANGED;
795                        *((SString*)target) = x;
[109]796                }
797        }
[154]798        messageOnExceedRange(i, result, xcopy);
[109]799        return result;
800}
801
[154]802int SimpleAbstractParam::setObject(int i, const ExtObject& x)
[109]803{
[230]804        SANITY_CHECK(i);
[154]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)
[109]810        {
[154]811                v.setObject(x);
812                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
813                messageOnExceedRange(i, result, xcopy);
814                return result;
[109]815        }
[154]816        else
[109]817        {
[154]818                void *target = getTarget(i);
819                *((ExtObject*)target) = x;
820                return PSET_CHANGED;
[109]821        }
822}
823
[154]824int SimpleAbstractParam::setExtValue(int i, const ExtValue& x)
[109]825{
[230]826        SANITY_CHECK(i);
[154]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)
[109]831        {
[154]832                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x);
833                messageOnExceedRange(i, result, xcopy);
834                return result;
[109]835        }
[154]836        else
[109]837        {
[154]838                void *target = getTarget(i);
839                *((ExtValue*)target) = x;
840                return PSET_CHANGED;
[109]841        }
842}
843
[154]844void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret)
[109]845{
[230]846        SANITY_CHECK(i);
[154]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
[109]852        {
[154]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);
[109]855        }
856}
857
858void SimpleAbstractParam::setDefault(bool numericonly)
859{
[154]860        bool save = dontcheckchanges;
861        dontcheckchanges = 1;
862        ParamInterface::setDefault(numericonly);
863        dontcheckchanges = save;
[109]864}
865
[154]866void SimpleAbstractParam::setDefault(int i, bool numericonly)
[109]867{
[154]868        bool save = dontcheckchanges;
869        dontcheckchanges = 1;
870        ParamInterface::setDefault(i, numericonly);
871        dontcheckchanges = save;
[109]872}
873
[154]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)
[109]880{
[154]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(); }
[247]885        else { poz = (int)(lf - (const char*)s) + 1; if (poz > s.len()) poz = s.len(); }
[154]886        while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break;
[247]887        len = (int)(lf - beg) + 1;
[154]888        return beg;
[109]889}
890
[154]891int ParamInterface::load2(const SString &s, int &poz)
[109]892{
[154]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;
[109]906
[154]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;
[109]915
[154]916                comma_sign = strchrlimit(t, ',', end); if (!comma_sign) comma_sign = end;
917                quote = strchrlimit(t, '\"', comma_sign);
918                if (quote)
[109]919                {
[154]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);
[109]927                }
[154]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                {
[247]941                        tmpi = findIdn(t, (int)(equals_sign - t));
[154]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                }
[109]947#ifdef WARN_MISSING_NAME
[154]948                else
[109]949#ifdef SAVE_SELECTED_NAMES
[154]950                        if (!(flags(i)&PARAM_CANOMITNAME))
[109]951#endif
[154]952                        {
953                                FMprintf("Param", "load2", FMLV_WARN, "Missing property name in '%s' (assuming '%s')",
954                                        getName(), id(i) ? id(i) : "unknown property?");
955                        }
[109]956#endif
[154]957                if ((i >= 0) && id(i))
[109]958                {
[154]959                        value = t;
960                        if (quote)
961                        {
[247]962                                tmpvalue.copyFrom(quote + 1, (int)(quote2 - quote) - 1);
[154]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;
[109]978                }
979
[154]980                if (i >= 0) i++;
[109]981#ifdef __CODEGUARD__
[154]982                if (comma_sign<end-1) t=comma_sign+1; else return fields_loaded;
[109]983#else
[154]984                t = comma_sign + 1;
[109]985#endif
[154]986        }
987        return fields_loaded;
[109]988}
989
[154]990int Param::grmember(int g, int a)
[109]991{
[154]992        if ((getGroupCount() < 2) && (!g))
993                return (a < getPropCount()) ? a : -9999;
[109]994
[154]995        ParamEntry *e = entry(0);
996        int x = 0, i = 0;
997        for (; e->id; i++, e++)
[109]998        {
[154]999                if (e->group == g)
1000                        if (a == x) return i; else x++;
[109]1001        }
[154]1002        return -9999;
[109]1003}
Note: See TracBrowser for help on using the repository browser.