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

Last change on this file since 574 was 574, checked in by Maciej Komosinski, 8 years ago

More strict validation of numeric type definitions: min without max is now invalid. More code is now reused.

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