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

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