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

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

Param::getText(int) can be safely used in all cases (previously min<0 was not supported)

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