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

Last change on this file since 822 was 822, checked in by Maciej Komosinski, 5 years ago

Avoid false positives for script-driven mutable param sanity check ("Invalid ParamEntry? for ExpProperties?.cleardata (no procedure defined)")

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