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

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

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

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