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

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