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

Last change on this file since 154 was 154, checked in by sz, 10 years ago

reformatting, english translation

  • Property svn:eol-style set to native
File size: 21.6 KB
RevLine 
[121]1// This file is a part of the Framsticks GDK.
2// Copyright (C) 2002-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{
[154]39        int n = limit - t;
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
[154]74int ParamInterface::getMinMax(int prop, long& minumum, long& maximum, long &def)
[109]75{
[154]76        const char* t = type(prop) + 1;
77        while (*t) if (*t == ' ') break; else t++;
78        return sscanf(t, "%ld %ld %ld", &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        {
[154]123                long a = 0, b = 0, c = 0;
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        {
[154]146                long a = 0, b = 0, c = 0;
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        {
[154]169                long a = 0, b = 0, c = 0;
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();}
180long 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,long 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
[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);
[154]262                long 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;
349                p_len = p - p0;
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        {
[154]444                long a, b, c;
445                if (getMinMax(i, a, b, c) >= 3)
446                        return setInt(i, c);
447                else
448                        return setInt(i, (long)0);
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);
538                        return SString(t, 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
560void *SimpleAbstractParam::getTarget(int i)
561{
[154]562        return (void*)(((char*)object) + entry(i)->offset);
563        //return &(object->*(entry(i)->fldptr));
[109]564}
565
566///////// get
567
568long SimpleAbstractParam::getInt(int i)
569{
[154]570        ExtValue v;
571        ParamEntry *pe = entry(i);
572        if (pe->fun1)
[109]573        {
[154]574                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
575                return v.getInt();
[109]576        }
[154]577        else
[109]578        {
[154]579                void *target = getTarget(i);
580                return *((long*)target);
[109]581        }
582}
583
584double SimpleAbstractParam::getDouble(int i)
585{
[154]586        ExtValue v;
587        ParamEntry *pe = entry(i);
588        if (pe->fun1)
[109]589        {
[154]590                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
591                return v.getDouble();
[109]592        }
[154]593        else
[109]594        {
[154]595                void *target = getTarget(i);
596                return *((double*)target);
[109]597        }
598}
599
600SString SimpleAbstractParam::getString(int i)
601{
[154]602        ExtValue v;
603        ParamEntry *pe = entry(i);
604        if (pe->fun1)
[109]605        {
[154]606                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
607                return v.getString();
[109]608        }
[154]609        else
[109]610        {
[154]611                void *target = getTarget(i);
612                return *((SString*)target);
[109]613        }
614}
615
616ExtObject SimpleAbstractParam::getObject(int i)
617{
[154]618        ExtValue v;
619        ParamEntry *pe = entry(i);
620        if (pe->fun1)
[109]621        {
[154]622                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
623                return v.getObject();
[109]624        }
[154]625        else
[109]626        {
[154]627                void *target = getTarget(i);
628                return *((ExtObject*)target);
[109]629        }
630}
631
632ExtValue SimpleAbstractParam::getExtValue(int i)
633{
[154]634        ExtValue v;
635        ParamEntry *pe = entry(i);
636        if (pe->fun1)
[109]637        {
[154]638                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
639                return v;
[109]640        }
[154]641        else
[109]642        {
[154]643                void *target = getTarget(i);
644                return *((ExtValue*)target);
[109]645        }
646}
647
648
649//////// set
650
[154]651int SimpleAbstractParam::setInt(int i, long x)
[109]652{
[154]653        ExtValue v;
654        ParamEntry *pe = entry(i);
655        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
656        long xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
657        long a = 0, b = 0;
658        int result = 0;
659        const char* t = pe->type + 1;
660        while (*t) if (*t == ' ') break; else t++;
661        if (sscanf(t, "%ld %ld", &a, &b) == 2)
662                if (a <= b) // if max<min then the min/max constraint check is not supported
[109]663                {
[154]664                        if (x<a) { x = a; result = PSET_HITMIN; }
665                        else if (x>b) { x = b; result = PSET_HITMAX; }
[109]666                }
667
[154]668        if (pe->fun2)
[109]669        {
[154]670                v.setInt(x);
671                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[109]672        }
[154]673        else
[109]674        {
[154]675                void *target = getTarget(i);
676                if (dontcheckchanges || (*((long*)target) != x))
677                {
[109]678                        result |= PSET_CHANGED;
[154]679                        *((long*)target) = x;
680                }
[109]681        }
[154]682        messageOnExceedRange(i, result, xcopy);
[109]683        return result;
684}
685
[154]686int SimpleAbstractParam::setDouble(int i, double x)
[109]687{
[154]688        ExtValue v;
689        ParamEntry *pe = entry(i);
690        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
691        double xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
692        double a = 0, b = 0;
693        int result = 0;
694        const char* t = pe->type + 1;
695        while (*t) if (*t == ' ') break; else t++;
696        if (sscanf(t, "%lg %lg", &a, &b) == 2)
697                if (a <= b) // if max<min then the min/max constraint check is not supported
[109]698                {
[154]699                        if (x<a) { x = a; result = PSET_HITMIN; }
700                        else if (x>b) { x = b; result = PSET_HITMAX; }
[109]701                }
702
[154]703        if (pe->fun2)
[109]704        {
[154]705                v.setDouble(x);
706                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[109]707        }
[154]708        else
[109]709        {
[154]710                void *target = getTarget(i);
711                if (dontcheckchanges || (*((double*)target) != x))
[109]712                {
[154]713                        result |= PSET_CHANGED;
714                        *((double*)target) = x;
[109]715                }
716        }
[154]717        messageOnExceedRange(i, result, xcopy);
[109]718        return result;
719}
720
[154]721int SimpleAbstractParam::setString(int i, const SString& x)
[109]722{
[154]723        ExtValue v;
724        SString vs;
725        const SString *xx = &x;
726        ParamEntry *pe = entry(i);
727        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
728        SString xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
729        const char* t = pe->type + 1;
730        while (*t) if (*t == ' ') break; else t++;
731        long a = 0, b = 0;
732        int result = 0;
733        if (sscanf(t, "%ld %ld", &a, &b) == 2)
[109]734        {
[154]735                if ((x.len() > b) && (b > 0))
[109]736                {
[154]737                        vs = x.substr(0, b);
738                        xx = &vs;
739                        result |= PSET_HITMAX;
[109]740                }
741        }
742
[154]743        if (pe->fun2)
[109]744        {
[154]745                v.setString(*xx);
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 || (!(*((SString*)target) == *xx)))
[109]752                {
[154]753                        result |= PSET_CHANGED;
754                        *((SString*)target) = x;
[109]755                }
756        }
[154]757        messageOnExceedRange(i, result, xcopy);
[109]758        return result;
759}
760
[154]761int SimpleAbstractParam::setObject(int i, const ExtObject& x)
[109]762{
[154]763        ExtValue v;
764        ParamEntry *pe = entry(i);
765        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
766        ExtObject xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
767        if (pe->fun2)
[109]768        {
[154]769                v.setObject(x);
770                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
771                messageOnExceedRange(i, result, xcopy);
772                return result;
[109]773        }
[154]774        else
[109]775        {
[154]776                void *target = getTarget(i);
777                *((ExtObject*)target) = x;
778                return PSET_CHANGED;
[109]779        }
780}
781
[154]782int SimpleAbstractParam::setExtValue(int i, const ExtValue& x)
[109]783{
[154]784        ParamEntry *pe = entry(i);
785        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
786        ExtValue xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
787        if (pe->fun2)
[109]788        {
[154]789                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x);
790                messageOnExceedRange(i, result, xcopy);
791                return result;
[109]792        }
[154]793        else
[109]794        {
[154]795                void *target = getTarget(i);
796                *((ExtValue*)target) = x;
797                return PSET_CHANGED;
[109]798        }
799}
800
[154]801void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret)
[109]802{
[154]803        ParamEntry *pe = entry(i);
804        if (!pe) return;
805        if (pe->fun1 && (pe->type[0] == 'p'))
806                (*(void(*)(void*, ExtValue*, ExtValue*))pe->fun1)(object, args, ret);
807        else
[109]808        {
[154]809                FMprintf("SimpleAbstractParam", "call", FMLV_ERROR,
810                        (*pe->type != 'p') ? "'%s.%s' is not a function" : "Internal error - undefined function pointer for '%s.%s'", getName(), pe->id);
[109]811        }
812}
813
814void SimpleAbstractParam::setDefault(bool numericonly)
815{
[154]816        bool save = dontcheckchanges;
817        dontcheckchanges = 1;
818        ParamInterface::setDefault(numericonly);
819        dontcheckchanges = save;
[109]820}
821
[154]822void SimpleAbstractParam::setDefault(int i, bool numericonly)
[109]823{
[154]824        bool save = dontcheckchanges;
825        dontcheckchanges = 1;
826        ParamInterface::setDefault(i, numericonly);
827        dontcheckchanges = save;
[109]828}
829
[154]830// Returns the address of the beginning of the line.
831// len = line length (without \n).
832// 0 may mean the line with length=0 or the end of the SString.
833// poz is advanced to the beginning of the next line.
834// A typical loop: for(poz=0;poz<s.d;) {line=getline(s,poz,len);...
835static const char *getline(const SString &s, int &poz, int &len)
[109]836{
[154]837        const char *beg = (const char*)s + poz;
838        if (poz >= s.len()) { poz = s.len(); len = 0; return (const char*)s + s.len(); }
839        const char *lf = strchr(beg, '\n');
840        if (!lf) { lf = (const char*)s + s.len() - 1; poz = s.len(); }
841        else { poz = (lf - (const char*)s) + 1; if (poz > s.len()) poz = s.len(); }
842        while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break;
843        len = lf - beg + 1;
844        return beg;
[109]845}
846
[154]847int ParamInterface::load2(const SString &s, int &poz)
[109]848{
[154]849        int i; // the index number of the parameter
850        int tmpi;
851        int len;
852        int ret;
853        int fields_loaded = 0;
854        const char *t, *lin, *end;
855        const char *equals_sign, *comma_sign;
856        char remember;
857        const char *quote, *quote2;
858        const char *value, *valstop;
859        SString tmpvalue;
860        if (poz >= s.len()) return fields_loaded;
861        t = (const char*)s + poz;
[109]862
[154]863        lin = getline(s, poz, len); // all fields must be encoded in a single line
864        if (!len) return fields_loaded; // empty line = end
865        i = 0;
866        end = lin + len;
867        while (t < end)
868        {
869                // processing a single field
870                while (strchr(" \n\r\t", *t)) if (t<end) t++; else return fields_loaded;
[109]871
[154]872                comma_sign = strchrlimit(t, ',', end); if (!comma_sign) comma_sign = end;
873                quote = strchrlimit(t, '\"', comma_sign);
874                if (quote)
[109]875                {
[154]876                        quote2 = skipQuoteString(quote + 1, end);
877                        if (quote2>comma_sign)
878                        {
879                                comma_sign = strchrlimit(quote2 + 1, ',', end);
880                                if (!comma_sign) comma_sign = end;
881                        }
882                        equals_sign = strchrlimit(t, '=', quote);
[109]883                }
[154]884                else
885                {
886                        equals_sign = strchrlimit(t, '=', comma_sign);
887                        quote2 = 0;
888                }
889                if (equals_sign == t) { t++; equals_sign = 0; }
890                if (comma_sign == t)    // skip empty value
891                {
892                        t++; i++;
893                        continue;
894                }
895                if (equals_sign) // have parameter name
896                {
897                        tmpi = findIdn(t, equals_sign - t);
898                        i = tmpi;
899                        if (tmpi < 0)
900                                FMprintf("Param", "load2", FMLV_WARN, "Unknown property name for '%s' (ignored)", getName());
901                        t = equals_sign + 1; // t=value
902                }
[109]903#ifdef WARN_MISSING_NAME
[154]904                else
[109]905#ifdef SAVE_SELECTED_NAMES
[154]906                        if (!(flags(i)&PARAM_CANOMITNAME))
[109]907#endif
[154]908                        {
909                                FMprintf("Param", "load2", FMLV_WARN, "Missing property name in '%s' (assuming '%s')",
910                                        getName(), id(i) ? id(i) : "unknown property?");
911                        }
[109]912#endif
[154]913                if ((i >= 0) && id(i))
[109]914                {
[154]915                        value = t;
916                        if (quote)
917                        {
918                                tmpvalue.copyFrom(quote + 1, quote2 - quote - 1);
919                                sstringUnquote(tmpvalue);
920                                value = tmpvalue;
921                                valstop = quote2;
922                        }
923                        else
924                                if (comma_sign < end) valstop = comma_sign; else valstop = end;
925
926                        remember = *valstop;
927                        *(char*)valstop = 0;
928                        ret = set(i, value);
929                        fields_loaded++;
930                        if (ret&(PSET_HITMAX | PSET_HITMIN))
931                                FMprintf("Param", "load2", FMLV_WARN, "Adjusted '%s' in '%s' (was too %s)",
932                                id(i), getName(), (ret&PSET_HITMAX) ? "big" : "small");
933                        *(char*)valstop = remember;
[109]934                }
935
[154]936                if (i >= 0) i++;
[109]937#ifdef __CODEGUARD__
[154]938                if (comma_sign<end-1) t=comma_sign+1; else return fields_loaded;
[109]939#else
[154]940                t = comma_sign + 1;
[109]941#endif
[154]942        }
943        return fields_loaded;
[109]944}
945
[154]946int Param::grmember(int g, int a)
[109]947{
[154]948        if ((getGroupCount() < 2) && (!g))
949                return (a < getPropCount()) ? a : -9999;
[109]950
[154]951        ParamEntry *e = entry(0);
952        int x = 0, i = 0;
953        for (; e->id; i++, e++)
[109]954        {
[154]955                if (e->group == g)
956                        if (a == x) return i; else x++;
[109]957        }
[154]958        return -9999;
[109]959}
Note: See TracBrowser for help on using the repository browser.