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
Line 
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.
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
19ParamEntry empty_paramtab[] =
20{ { "Empty", 1, 0, "Empty", }, { 0, 0, 0, }, };
21
22static void czytdotyldy(VirtFILE *f, SString &s)
23{
24        SString temp;
25        int z;
26        char last_char = 0;
27        while ((z = fgetc(f)) != EOF)
28        {
29                if (z == '~')
30                        if (last_char != '\\') break;
31                last_char = (char)z;
32                temp += last_char;
33        }
34        s = temp;
35}
36
37static const char *strchrlimit(const char *t, int ch, const char *limit)
38{
39        int n = (int)(limit - t);
40        for (; (n > 0) && *t; t++, n--)
41                if (*t == ch) return t;
42        return 0;
43}
44
45void ParamInterface::copyFrom(ParamInterface *src)
46{
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                }
59}
60
61void ParamInterface::quickCopyFrom(ParamInterface *src)
62{
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                }
72}
73
74int ParamInterface::getMinMax(int prop, paInt& minumum, paInt& maximum, paInt &def)
75{
76        const char* t = type(prop) + 1;
77        while (*t) if (*t == ' ') break; else t++;
78        return sscanf(t, PA_INT_SCANF " " PA_INT_SCANF " " PA_INT_SCANF, &minumum, &maximum, &def);
79}
80
81int ParamInterface::getMinMax(int prop, double& minumum, double& maximum, double& def)
82{
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);
86}
87
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* end=strchr(t,'~');
105                        if (!end)
106                                end=t+strlen(t);
107                        while ((end>t)&&(end[-1]==' ')) end--;
108                        def=SString(t,end-t);
109                        }
110                return 3;
111                }
112        else
113                return ret;
114}
115
116void ParamInterface::setDefault()
117{
118        for (int i = 0; i < getPropCount(); i++)
119                setDefault(i);
120}
121
122void ParamInterface::setMin()
123{
124        for (int i = 0; i < getPropCount(); i++)
125                setMin(i);
126}
127
128void ParamInterface::setMax()
129{
130        for (int i = 0; i < getPropCount(); i++)
131                setMax(i);
132}
133
134void ParamInterface::setDefault(int i)
135{
136        const char *t = type(i);
137        switch (*t)
138        {
139        case 'f':
140        {
141                double a = 0, b = 0, c = 0;
142                if (getMinMax(i, a, b, c) < 3) c = a;
143                setDouble(i, c);
144        }
145                break;
146        case 'd':
147        {
148                paInt a = 0, b = 0, c = 0;
149                if (getMinMax(i, a, b, c) < 3) c = a;
150                setInt(i, c);
151        }
152                break;
153        case 's': case 'x':
154        {
155                int a,b; SString c;
156                getMinMax(i,a,b,c);
157                if (*t=='s')
158                        setString(i,c);
159                else
160                        { if (c.len()>0) setExtValue(i,ExtValue(c)); else setExtValue(i,ExtValue::empty()); }
161        }
162                break;
163        case 'o':
164                setObject(i,ExtObject::empty());
165                break;
166        }
167}
168
169void ParamInterface::setMin(int i)
170{
171        const char *t = type(i);
172        switch (*t)
173        {
174        case 'f':
175        {
176                double a = 0, b = 0, c = 0;
177                getMinMax(i, a, b, c);
178                setDouble(i, a);
179        }
180                break;
181        case 'd':
182        {
183                paInt a = 0, b = 0, c = 0;
184                getMinMax(i, a, b, c);
185                setInt(i, a);
186        }
187                break;
188        default: set(i, "");
189        }
190}
191
192void ParamInterface::setMax(int i)
193{
194        const char *t = type(i);
195        switch (*t)
196        {
197        case 'f':
198        {
199                double a = 0, b = 0, c = 0;
200                getMinMax(i, a, b, c);
201                setDouble(i, b);
202        }
203                break;
204        case 'd':
205        {
206                paInt a = 0, b = 0, c = 0;
207                getMinMax(i, a, b, c);
208                setInt(i, b);
209        }
210                break;
211        default: set(i, "");
212        }
213}
214
215SString ParamInterface::getStringById(const char*prop)
216{int i=findId(prop); if (i>=0) return getString(i); else return SString();}
217paInt ParamInterface::getIntById(const char*prop)
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
226int ParamInterface::setIntById(const char* prop,paInt v)
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
239int ParamInterface::save(VirtFILE* f, const char* altname, bool force)
240{
241        const char *p;
242        SString ws;
243        int err = 0, i;
244        bool withname = false;
245        if ((altname == NULL) || (altname[0] != 0))
246        {
247                err |= (fputs(altname ? altname : getName(), f) == EOF);
248                err |= (fputs(":\n", f) == EOF);
249                withname = true;
250        }
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;
256}
257
258const char* ParamInterface::SERIALIZATION_PREFIX = "@Serialized:";
259
260int ParamInterface::saveprop(VirtFILE* f, int i, const char* p, bool force)
261{
262        if ((flags(i)&PARAM_DONTSAVE) && (!force)) return 0;
263        const char *typ = type(i);
264        if (*typ == 'p') return 0;
265
266        const char *t, *w;
267        SString ws;
268        int err = 0, cr;
269
270        err |= (fputs(p, f) == EOF); fputc(':', f);
271        cr = 0;
272        if ((*typ == 'x')||(*typ == 'o'))
273        {
274                ExtValue ex;
275                get(i, ex);
276                ws = SString(SERIALIZATION_PREFIX) + ex.serialize();
277        }
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;
288}
289
290
291int SimpleAbstractParam::isequal(int i, void* defdata)
292{ // defdata->member == object->member ?
293        void *backup = object;
294        switch (type(i)[0])
295        {
296        case 'd':
297        {
298                select(defdata);
299                paInt x = getInt(i);
300                select(backup);
301                return x == getInt(i);
302        }
303        case 'f':
304        {
305                select(defdata);
306                double x = getDouble(i);
307                select(backup);
308                return x == getDouble(i);
309        }
310        case 's':
311        {
312                select(defdata);
313                SString x = getString(i);
314                select(backup);
315                return x == getString(i);
316        }
317        }
318        return 1;
319}
320
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))
333                {
334                        if (defdata && isequal(i, defdata))
335                                needlabel = 1;
336                        else
337                        {
338                                if (!first) t += ", ";
339#ifndef SAVE_ALL_NAMES
340#ifdef SAVE_SELECTED_NAMES
341                                if (needlabel || all_names || !(fl & PARAM_CANOMITNAME))
342#else
343                                if (needlabel)
344#endif
345#endif
346                                {
347                                        t += p; t += "="; needlabel = 0;
348                                }
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;
365                        }
366                }
367        if (addcr)
368                t += "\n";
369        f += t;
370}
371
372int ParamInterface::load(VirtFILE* f,bool warn_unknown_fields,bool *abortable)
373{
374        SString buf;
375        int i;
376        const char *p, *p0;
377        int p_len;
378        bool loaded;
379        int fields_loaded = 0;
380        while ( ((!abortable)||(!*abortable)) && loadSStringLine(f, buf) )
381        {
382                const char* t = (const char*)buf;
383                p0 = t; while ((*p0 == ' ') || (*p0 == '\t')) p0++;
384                if (!*p0) break;
385                if (p0[0]=='#') continue;
386                p = strchr(p0, ':'); if (!p) continue;
387                p_len = (int)(p - p0);
388                loaded = false;
389                if (p_len && ((i = findIdn(p0, p_len)) >= 0))
390                {
391                if (!(flags(i)&PARAM_DONTLOAD))
392                   {
393                        if (p0[p_len + 1] == '~')
394                        {
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);
401                        }
402                        else
403                        {
404                                set(i, p0 + p_len + 1);
405                        }
406                        fields_loaded++;
407                        loaded = true;
408                   }
409                }
410                else if (warn_unknown_fields)
411                        {
412                        SString name(p0,p_len);
413                        FMprintf("ParamInterface","load",FMLV_WARN,"Unknown property '%s' while reading object '%s' (ignored)",(const char*)name,getName());
414                        }
415
416                if ((!loaded) && (p0[p_len + 1] == '~'))
417                { // eat unrecognized multiline field
418                        SString s;
419                        czytdotyldy(f, s);
420                        int ch; while ((ch = fgetc(f)) != EOF) if (ch == '\n') break;
421                }
422        }
423        return fields_loaded;
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{
454        int i; const char *p;
455        for (i = 0; p = id(i); i++) if (!strcmp(n, p)) return i;
456        return -1;
457}
458
459int ParamInterface::findIdn(const char* naz, int n)
460{
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;
464}
465
466void ParamInterface::get(int i, ExtValue &ret)
467{
468        switch (type(i)[0])
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;
474        case 'x':       ret = getExtValue(i); break;
475        default: FMprintf("ParamInterface", "get", FMLV_ERROR, "'%s.%s' is not a field", getName(), id(i));
476        }
477}
478
479static bool stringIsNumeric(const char* str)
480{//   /-?.?[0-9]+/
481        if (!str) return false;
482        if (*str == '-') str++;
483        if (*str == '.') str++;
484        return isdigit(*str) != 0;
485}
486
487int ParamInterface::setInt(int i, const char* str)
488{
489        if (!stringIsNumeric(str))
490        {
491                paInt a, b, c;
492                if (getMinMax(i, a, b, c) >= 3)
493                        return setInt(i, c);
494                else
495                        return setInt(i, (paInt)0);
496        }
497        else
498                return setInt(i, ExtValue::getInt(str));
499}
500
501int ParamInterface::setDouble(int i, const char* str)
502{
503        if (!stringIsNumeric(str))
504        {
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        }
511        else
512                return setDouble(i, ExtValue::getDouble(str));
513}
514
515int ParamInterface::set(int i, const ExtValue &v)
516{
517        switch (type(i)[0])
518        {
519        case 'd':
520                if ((v.type == TInt) || (v.type == TDouble)) return setInt(i, v.getInt());
521                else
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                }
527        case 'f':
528                if ((v.type == TInt) || (v.type == TDouble)) return setDouble(i, v.getDouble());
529                else
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));
539        }
540        return 0;
541}
542
543int ParamInterface::set(int i, const char *v)
544{
545        char typ=type(i)[0];
546        switch (typ)
547        {
548        case 'd': return setInt(i, v);
549        case 'f': return setDouble(i, v);
550        case 's': { SString t(v); return setString(i, t); }
551        case 'x': case 'o':
552        {
553                ExtValue e;
554                const char* after;
555                if (!strncmp(v, SERIALIZATION_PREFIX, strlen(SERIALIZATION_PREFIX)))
556                {
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));
560                }
561                else if ((after = e.parseNumber(v)) && (*after == 0)) //consumed the whole string
562                {
563                        //OK!
564                }
565                else
566                {
567                        e.setString(SString(v));
568                }
569                if (typ=='x')
570                        return setExtValue(i, e);
571                else
572                        return setObject(i, e.getObject());
573        }
574        }
575        return 0;
576}
577
578SString ParamInterface::getText(int i)
579{
580        const char *t;
581        if ((*(t = type(i))) == 'd')
582        {
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, '~');
593                if (t)
594                {
595                        t++;
596                        const char *t2 = strchr(t, '~');
597                        if (!t2) t2 = t + strlen(t);
598                        return SString(t, (int)(t2 - t));
599                }
600        }
601        return get(i);
602}
603
604SString ParamInterface::get(int i)
605{
606        switch (type(i)[0])
607        {
608        case 'd': return SString::valueOf(getInt(i));
609        case 'f': return SString::valueOf(getDouble(i));
610        case 's': return getString(i);
611        }
612        ExtValue v;
613        get(i, v);
614        return v.getString();
615}
616
617
618//////////////////////////////// PARAM ////////////////////////////////////
619
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
647void *SimpleAbstractParam::getTarget(int i)
648{
649        return (void*)(((char*)object) + entry(i)->offset);
650        //return &(object->*(entry(i)->fldptr));
651}
652
653///////// get
654
655#ifdef DEBUG
656#define SANITY_CHECK(i) sanityCheck(i)
657#else
658#define SANITY_CHECK(i)
659#endif
660
661paInt SimpleAbstractParam::getInt(int i)
662{
663        SANITY_CHECK(i);
664        ExtValue v;
665        ParamEntry *pe = entry(i);
666        if (pe->fun1)
667        {
668                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
669                return v.getInt();
670        }
671        else
672        {
673                void *target = getTarget(i);
674                return *((paInt*)target);
675        }
676}
677
678double SimpleAbstractParam::getDouble(int i)
679{
680        SANITY_CHECK(i);
681        ExtValue v;
682        ParamEntry *pe = entry(i);
683        if (pe->fun1)
684        {
685                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
686                return v.getDouble();
687        }
688        else
689        {
690                void *target = getTarget(i);
691                return *((double*)target);
692        }
693}
694
695SString SimpleAbstractParam::getString(int i)
696{
697        SANITY_CHECK(i);
698        ExtValue v;
699        ParamEntry *pe = entry(i);
700        if (pe->fun1)
701        {
702                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
703                return v.getString();
704        }
705        else
706        {
707                void *target = getTarget(i);
708                return *((SString*)target);
709        }
710}
711
712ExtObject SimpleAbstractParam::getObject(int i)
713{
714        SANITY_CHECK(i);
715        ExtValue v;
716        ParamEntry *pe = entry(i);
717        if (pe->fun1)
718        {
719                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
720                return v.getObject();
721        }
722        else
723        {
724                void *target = getTarget(i);
725                return *((ExtObject*)target);
726        }
727}
728
729ExtValue SimpleAbstractParam::getExtValue(int i)
730{
731        SANITY_CHECK(i);
732        ExtValue v;
733        ParamEntry *pe = entry(i);
734        if (pe->fun1)
735        {
736                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
737                return v;
738        }
739        else
740        {
741                void *target = getTarget(i);
742                return *((ExtValue*)target);
743        }
744}
745
746
747//////// set
748
749int SimpleAbstractParam::setInt(int i, paInt x)
750{
751        SANITY_CHECK(i);
752        ExtValue v;
753        ParamEntry *pe = entry(i);
754        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
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;
757        int result = 0;
758        const char* t = pe->type + 1;
759        while (*t) if (*t == ' ') break; else t++;
760        if (sscanf(t, PA_INT_SCANF " " PA_INT_SCANF, &a, &b) == 2)
761                if (a <= b) // if max<min then the min/max constraint check is not supported
762                {
763                        if (x<a) { x = a; result = PSET_HITMIN; }
764                        else if (x>b) { x = b; result = PSET_HITMAX; }
765                }
766
767        if (pe->fun2)
768        {
769                v.setInt(x);
770                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
771        }
772        else
773        {
774                void *target = getTarget(i);
775                if (dontcheckchanges || (*((paInt*)target) != x))
776                {
777                        result |= PSET_CHANGED;
778                        *((paInt*)target) = x;
779                }
780        }
781        messageOnExceedRange(i, result, xcopy);
782        return result;
783}
784
785int SimpleAbstractParam::setDouble(int i, double x)
786{
787        SANITY_CHECK(i);
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
798                {
799                        if (x<a) { x = a; result = PSET_HITMIN; }
800                        else if (x>b) { x = b; result = PSET_HITMAX; }
801                }
802
803        if (pe->fun2)
804        {
805                v.setDouble(x);
806                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
807        }
808        else
809        {
810                void *target = getTarget(i);
811                if (dontcheckchanges || (*((double*)target) != x))
812                {
813                        result |= PSET_CHANGED;
814                        *((double*)target) = x;
815                }
816        }
817        messageOnExceedRange(i, result, xcopy);
818        return result;
819}
820
821int SimpleAbstractParam::setString(int i, const SString& x)
822{
823        SANITY_CHECK(i);
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++;
832        int a = 0, b = 0;
833        int result = 0;
834        if (sscanf(t, "%d %d", &a, &b) == 2) //using getMinMax would also get default value, which is not needed here
835        {
836                if ((x.len() > b) && (b > 0))
837                {
838                        vs = x.substr(0, b);
839                        xx = &vs;
840                        result |= PSET_HITMAX;
841                }
842        }
843
844        if (pe->fun2)
845        {
846                v.setString(*xx);
847                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
848        }
849        else
850        {
851                void *target = getTarget(i);
852                if (dontcheckchanges || (!(*((SString*)target) == *xx)))
853                {
854                        result |= PSET_CHANGED;
855                        *((SString*)target) = *xx;
856                }
857        }
858        messageOnExceedRange(i, result, xcopy);
859        return result;
860}
861
862int SimpleAbstractParam::setObject(int i, const ExtObject& x)
863{
864        SANITY_CHECK(i);
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)
870        {
871                v.setObject(x);
872                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
873                messageOnExceedRange(i, result, xcopy);
874                return result;
875        }
876        else
877        {
878                void *target = getTarget(i);
879                *((ExtObject*)target) = x;
880                return PSET_CHANGED;
881        }
882}
883
884int SimpleAbstractParam::setExtValue(int i, const ExtValue& x)
885{
886        SANITY_CHECK(i);
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)
891        {
892                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x);
893                messageOnExceedRange(i, result, xcopy);
894                return result;
895        }
896        else
897        {
898                void *target = getTarget(i);
899                *((ExtValue*)target) = x;
900                return PSET_CHANGED;
901        }
902}
903
904void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret)
905{
906        SANITY_CHECK(i);
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
912        {
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);
915                ret->setInvalid();
916        }
917}
918
919void SimpleAbstractParam::setDefault()
920{
921        bool save = dontcheckchanges;
922        dontcheckchanges = 1;
923        ParamInterface::setDefault();
924        dontcheckchanges = save;
925}
926
927void SimpleAbstractParam::setDefault(int i)
928{
929        bool save = dontcheckchanges;
930        dontcheckchanges = 1;
931        ParamInterface::setDefault(i);
932        dontcheckchanges = save;
933}
934
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)
941{
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(); }
946        else { poz = (int)(lf - (const char*)s) + 1; if (poz > s.len()) poz = s.len(); }
947        while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break;
948        len = (int)(lf - beg) + 1;
949        return beg;
950}
951
952int ParamInterface::load2(const SString &s, int &poz)
953{
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;
967
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;
976
977                comma_sign = strchrlimit(t, ',', end); if (!comma_sign) comma_sign = end;
978                quote = strchrlimit(t, '\"', comma_sign);
979                if (quote)
980                {
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);
988                }
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                {
1002                        tmpi = findIdn(t, (int)(equals_sign - t));
1003                        i = tmpi;
1004                        if (tmpi < 0)
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                                }
1009                        t = equals_sign + 1; // t=value
1010                }
1011#ifdef WARN_MISSING_NAME
1012                else
1013#ifdef SAVE_SELECTED_NAMES
1014                        if (!(flags(i)&PARAM_CANOMITNAME))
1015#endif
1016                        {
1017                                FMprintf("Param", "load2", FMLV_WARN, "Missing property name in '%s' (assuming '%s')",
1018                                        getName(), id(i) ? id(i) : "unknown property?");
1019                        }
1020#endif
1021                if ((i >= 0) && id(i))
1022                {
1023                        value = t;
1024                        if (quote)
1025                        {
1026                                tmpvalue.copyFrom(quote + 1, (int)(quote2 - quote) - 1);
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;
1042                }
1043
1044                if (i >= 0) i++;
1045#ifdef __CODEGUARD__
1046                if (comma_sign<end-1) t=comma_sign+1; else return fields_loaded;
1047#else
1048                t = comma_sign + 1;
1049#endif
1050        }
1051        return fields_loaded;
1052}
1053
1054int Param::grmember(int g, int a)
1055{
1056        if ((getGroupCount() < 2) && (!g))
1057                return (a < getPropCount()) ? a : -9999;
1058
1059        ParamEntry *e = entry(0);
1060        int x = 0, i = 0;
1061        for (; e->id; i++, e++)
1062        {
1063                if (e->group == g)
1064                        if (a == x) return i; else x++;
1065        }
1066        return -9999;
1067}
Note: See TracBrowser for help on using the repository browser.