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

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

Unquote tilde \~ needed when loading single-line Param values

  • Property svn:eol-style set to native
File size: 33.9 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[883]2// Copyright (C) 1999-2019  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                                {
[883]589                                        SString s = SString(p0 + p_len + 1);
590                                        unquoteTilde(s);
591                                        setFromString(i, s.c_str(), false);
[312]592                                }
593                                fields_loaded++;
594                                loaded = true;
[109]595                        }
596                }
[720]597                else if (options.warn_unknown_fields)
[312]598                {
599                        SString name(p0, p_len);
[796]600                        logPrintf("ParamInterface", "load", LOG_WARN, "Ignored unknown property '%s.%s'", getName(), name.c_str());
[312]601                }
[268]602
[154]603                if ((!loaded) && (p0[p_len + 1] == '~'))
[109]604                { // eat unrecognized multiline field
[154]605                        SString s;
[650]606                        if (!readUntilTilde(f, s))
607                                closingTildeError(this, f, -1);
[720]608                        if (options.linenum)
[333]609                        {
[348]610                                const char* tmp = s.c_str();
[333]611                                int lfcount = 1;
612                                while (tmp)
613                                        if ((tmp = strchr(tmp, '\n')))
614                                        {
[822]615                                                lfcount++; tmp++;
[333]616                                        }
[720]617                                (*options.linenum) += lfcount;
[333]618                        }
[523]619                        int ch; while ((ch = f->Vgetc()) != EOF) if (ch == '\n') break;
[109]620                }
621        }
[650]622        if ((i = findId("afterLoad")) >= 0)
623                call(i, NULL, NULL);
[154]624        return fields_loaded;
[109]625}
626
627
628/*
629SString SimpleAbstractParam::getString(int i)
630{
631char *t;
632switch (*(t=type(i)))
[312]633{
[333]634case 'd':
635{
636for (i=atol(get(i));i>=0;i--) if (t) t=strchr(t+1,'~');
637if (t)
638{
639t++;
640char *t2=strchr(t,'~');
641if (!t2) t2=t+strlen(t);
642SString str;
643strncpy(str.directWrite(t2-t),t,t2-t);
644str.endWrite(t2-t);
645return str;
[312]646}
[333]647}
648}
[109]649return get(i);
650}
651*/
652
653int ParamInterface::findId(const char* n)
654{
[154]655        int i; const char *p;
656        for (i = 0; p = id(i); i++) if (!strcmp(n, p)) return i;
657        return -1;
[109]658}
659
[154]660int ParamInterface::findIdn(const char* naz, int n)
[109]661{
[154]662        int i; const char *p;
663        for (i = 0; p = id(i); i++) if ((!strncmp(naz, p, n)) && (!p[n])) return i;
664        return -1;
[109]665}
666
[154]667void ParamInterface::get(int i, ExtValue &ret)
[109]668{
[154]669        switch (type(i)[0])
[109]670        {
671        case 'd':       ret.setInt(getInt(i)); break;
672        case 'f':       ret.setDouble(getDouble(i)); break;
673        case 's':       ret.setString(getString(i)); break;
674        case 'o':       ret.setObject(getObject(i)); break;
[154]675        case 'x':       ret = getExtValue(i); break;
[796]676        default: logPrintf("ParamInterface", "get", LOG_ERROR, "'%s.%s' is not a property", getName(), id(i));
[109]677        }
678}
679
[743]680int ParamInterface::setIntFromString(int i, const char* str, bool strict)
[109]681{
[326]682        paInt value;
[645]683        if (!ExtValue::parseInt(str, value, strict, true))
[109]684        {
[314]685                paInt mn, mx, def;
[743]686                if (getMinMaxInt(i, mn, mx, def) >= 3)
[393]687                        return setInt(i, def) | PSET_PARSEFAILED;
[154]688                else
[393]689                        return setInt(i, (paInt)0) | PSET_PARSEFAILED;
[154]690        }
[109]691        else
[326]692                return setInt(i, value);
[109]693}
694
[743]695int ParamInterface::setDoubleFromString(int i, const char* str)
[109]696{
[326]697        double value;
[333]698        if (!ExtValue::parseDouble(str, value, true))
[109]699        {
[314]700                double mn, mx, def;
[743]701                if (getMinMaxDouble(i, mn, mx, def) >= 3)
[393]702                        return setDouble(i, def) | PSET_PARSEFAILED;
[154]703                else
[393]704                        return setDouble(i, (double)0) | PSET_PARSEFAILED;
[154]705        }
[109]706        else
[326]707                return setDouble(i, value);
[109]708}
709
[154]710int ParamInterface::set(int i, const ExtValue &v)
[109]711{
[154]712        switch (type(i)[0])
[109]713        {
[144]714        case 'd':
[154]715                if ((v.type == TInt) || (v.type == TDouble)) return setInt(i, v.getInt());
[144]716                else
[154]717                {
718                        if (v.type == TObj)
[333]719                        {
[478]720                                logPrintf("ParamInterface", "set", LOG_ERROR, "Setting int '%s.%s' from object reference (%s)", getName(), id(i), v.getString().c_str());
[333]721                                return 0;
722                        }
723                        else
[743]724                                return setIntFromString(i, v.getString().c_str(), false);
[154]725                }
[144]726        case 'f':
[154]727                if ((v.type == TInt) || (v.type == TDouble)) return setDouble(i, v.getDouble());
[144]728                else
[154]729                {
730                        if (v.type == TObj)
[333]731                        {
[478]732                                logPrintf("ParamInterface", "set", LOG_ERROR, "Setting float '%s.%s' from object reference (%s)", getName(), id(i), v.getString().c_str());
[333]733                                return 0;
734                        }
735                        else
[743]736                                return setDoubleFromString(i, v.getString().c_str());
[154]737                }
738        case 's': { SString t = v.getString(); return setString(i, t); }
[478]739        case 'o':
[650]740                if ((v.type != TUnknown) && (v.type != TObj))
[478]741                        logPrintf("ParamInterface", "set", LOG_ERROR, "Setting object '%s.%s' from %s", getName(), id(i), v.typeAndValue().c_str());
742                else
743                        return setObject(i, v.getObject());
744                break;
[154]745        case 'x': return setExtValue(i, v);
[796]746        default: logPrintf("ParamInterface", "set", LOG_ERROR, "'%s.%s' is not a property", getName(), id(i));
[109]747        }
[154]748        return 0;
[109]749}
750
[743]751int ParamInterface::setFromString(int i, const char *v, bool strict)
[109]752{
[312]753        char typ = type(i)[0];
[273]754        switch (typ)
[109]755        {
[743]756        case 'd': return setIntFromString(i, v, strict);
757        case 'f': return setDoubleFromString(i, v);
[154]758        case 's': { SString t(v); return setString(i, t); }
[273]759        case 'x': case 'o':
[109]760        {
[154]761                ExtValue e;
762                const char* after;
763                if (!strncmp(v, SERIALIZATION_PREFIX, strlen(SERIALIZATION_PREFIX)))
[109]764                {
[154]765                        after = e.deserialize(v + strlen(SERIALIZATION_PREFIX));
766                        if ((after == NULL) || (*after))
[343]767                        {
[478]768                                logPrintf("ParamInterface", "set", LOG_ERROR, "serialization format mismatch in %s.%s", (getName() ? getName() : "<Unknown>"), id(i));
[334]769                                e.setEmpty();
[343]770                        }
[109]771                }
[154]772                else if ((after = e.parseNumber(v)) && (*after == 0)) //consumed the whole string
[109]773                {
[154]774                        //OK!
[109]775                }
[154]776                else
[109]777                {
[154]778                        e.setString(SString(v));
[109]779                }
[312]780                if (typ == 'x')
[273]781                        return setExtValue(i, e);
782                else
783                        return setObject(i, e.getObject());
[109]784        }
785        }
[154]786        return 0;
[109]787}
788
[704]789SString ParamInterface::getText(int i) //find the current enum text or call get(i) if not enum
[109]790{
[154]791        const char *t;
[720]792        if (((*(t = type(i))) == 'd') && (strchr(t, '~') != NULL)) //type is int and contains enum labels
[109]793        {
[314]794                paInt mn, mx, def;
[312]795                int value = getInt(i);
[743]796                if (getMinMaxIntFromTypeDef(t, mn, mx, def) >= 2)
[312]797                {
[314]798                        if (value > mx)
[704]799                                return get(i);//unexpected value of out bounds (should never happen) -> fallback
[314]800                        value -= mn;
[312]801                }
[704]802                if (value < 0) return get(i); //unexpected value of out bounds (should never happen) -> fallback
803                // now value is 0-based index of ~text
804                for (; value >= 0; value--) if (t) t = strchr(t + 1, '~'); else break;
805                if (t) // found n-th ~text in type description (else: not enough ~texts in type description)
[109]806                {
[154]807                        t++;
808                        const char *t2 = strchr(t, '~');
809                        if (!t2) t2 = t + strlen(t);
[247]810                        return SString(t, (int)(t2 - t));
[109]811                }
812        }
[704]813        return get(i); //fallback - return int value as string
[109]814}
815
816SString ParamInterface::get(int i)
817{
[154]818        switch (type(i)[0])
[109]819        {
820        case 'd': return SString::valueOf(getInt(i));
821        case 'f': return SString::valueOf(getDouble(i));
822        case 's': return getString(i);
823        }
[154]824        ExtValue v;
825        get(i, v);
826        return v.getString();
[109]827}
828
[535]829bool ParamInterface::isValidTypeDescription(const char* t)
830{
[650]831        if (t == NULL) return false;
832        if (*t == 0) return false;
833        if (strchr("dfsoxp", *t) == NULL) return false;
834        switch (*t)
[574]835        {
836        case 'd':
[754]837        {
838                paInt a, b, c;
839                int have = getMinMaxIntFromTypeDef(t, a, b, c);
840                if (have == 1) return false;
841                if ((have >= 2) && (b < a) && (a != 0) && (b != -1)) return false; // max<min meaning 'undefined' is only allowed as "d 0 -1"
842        }
[822]843        break;
[574]844        case 'f':
[754]845        {
846                double a, b, c;
847                int have = getMinMaxDoubleFromTypeDef(t, a, b, c);
848                if (have == 1) return false;
849                if ((have >= 2) && (b < a) && (a != 0) && (b != -1)) return false; // max<min meaning 'undefined' is only allowed as "f 0 -1"
850        }
[822]851        break;
[754]852        case 's':
853        {
854                int a, b; SString c;
855                int have = getMinMaxStringFromTypeDef(t, a, b, c);
856                //if (have == 1) return false; //not sure?
857                if ((have >= 1) && (!((a == 0) || (a == 1)))) return false; // 'min' for string (single/multi) can be only 0 or 1
858                if ((have >= 2) && (b < 0)) return false; // max=0 means unlimited, max<0 is not allowed
[574]859        }
[822]860        break;
[754]861        }
[650]862        return true;
[535]863}
[109]864
[743]865SString ParamInterface::friendlyTypeDescrFromTypeDef(const char* type)
[640]866{
[650]867        SString t;
868        switch (type[0])
[640]869        {
[650]870        case 'd': t += "integer";
[743]871        {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]872        break;
[650]873        case 'f': t += "float";
[743]874        {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]875        break;
[650]876        case 's': t += "string";
[743]877        {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]878        break;
[650]879        case 'x': t += "untyped value"; break;
880        case 'p': t += "function"; break;
881        case 'o': t += "object"; if (type[1]) { t += " of class "; t += type + 1; } break;
[640]882        default: return "unknown type";
883        }
[650]884        return t;
[640]885}
886
[109]887//////////////////////////////// PARAM ////////////////////////////////////
888
[483]889#ifdef _DEBUG
[230]890void SimpleAbstractParam::sanityCheck(int i)
891{
[650]892        ParamEntry *pe = entry(i);
[230]893
[650]894        const char* t = pe->type;
895        const char* err = NULL;
[230]896
[535]897        if (!isValidTypeDescription(t))
[650]898                err = "invalid type description";
899        if (*t == 'p')
[230]900        {
[650]901                if (pe->fun1 == NULL)
[822]902                {
903                        MutableParamInterface *mpi = dynamic_cast<MutableParamInterface*>(this);
904                        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.
905                                err = "no procedure defined";
906                }
[654]907                if (pe->flags & PARAM_READONLY)
908                        err = "function can't be PARAM_READONLY";
[230]909        }
[312]910        else
[230]911        {
[732]912                if ((t[0] == 'o') && (t[1] == ' '))
913                {
914                        err = "space after 'o'";
915                }
[659]916                if (!(pe->flags & (PARAM_READONLY | PARAM_DONTSAVE | PARAM_USERREADONLY | PARAM_CONST | PARAM_DONTLOAD | PARAM_LINECOMMENT | PARAM_OBJECTSET)))
[230]917                { //write access
[650]918                        if ((pe->fun2 == NULL) && (pe->offset == PARAM_ILLEGAL_OFFSET))
919                                err = "no field defined (GETONLY without PARAM_READONLY?)";
[230]920                }
921        }
[650]922        if (err != NULL)
923                logPrintf("SimpleAbstractParam", "sanityCheck", LOG_ERROR,
[822]924                        "Invalid ParamEntry for %s.%s (%s)", getName(), pe->id, err);
[650]925}
[230]926#endif
927
[109]928void *SimpleAbstractParam::getTarget(int i)
929{
[154]930        return (void*)(((char*)object) + entry(i)->offset);
931        //return &(object->*(entry(i)->fldptr));
[109]932}
933
934///////// get
935
[483]936#ifdef _DEBUG
[230]937#define SANITY_CHECK(i) sanityCheck(i)
938#else
939#define SANITY_CHECK(i)
940#endif
941
[247]942paInt SimpleAbstractParam::getInt(int i)
[109]943{
[230]944        SANITY_CHECK(i);
[154]945        ExtValue v;
946        ParamEntry *pe = entry(i);
947        if (pe->fun1)
[109]948        {
[154]949                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
950                return v.getInt();
[109]951        }
[154]952        else
[109]953        {
[154]954                void *target = getTarget(i);
[247]955                return *((paInt*)target);
[109]956        }
957}
958
959double SimpleAbstractParam::getDouble(int i)
960{
[230]961        SANITY_CHECK(i);
[154]962        ExtValue v;
963        ParamEntry *pe = entry(i);
964        if (pe->fun1)
[109]965        {
[154]966                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
967                return v.getDouble();
[109]968        }
[154]969        else
[109]970        {
[154]971                void *target = getTarget(i);
972                return *((double*)target);
[109]973        }
974}
975
976SString SimpleAbstractParam::getString(int i)
977{
[230]978        SANITY_CHECK(i);
[154]979        ExtValue v;
980        ParamEntry *pe = entry(i);
981        if (pe->fun1)
[109]982        {
[154]983                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
984                return v.getString();
[109]985        }
[154]986        else
[109]987        {
[154]988                void *target = getTarget(i);
989                return *((SString*)target);
[109]990        }
991}
992
993ExtObject SimpleAbstractParam::getObject(int i)
994{
[230]995        SANITY_CHECK(i);
[154]996        ExtValue v;
997        ParamEntry *pe = entry(i);
998        if (pe->fun1)
[109]999        {
[154]1000                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
1001                return v.getObject();
[109]1002        }
[154]1003        else
[109]1004        {
[154]1005                void *target = getTarget(i);
1006                return *((ExtObject*)target);
[109]1007        }
1008}
1009
1010ExtValue SimpleAbstractParam::getExtValue(int i)
1011{
[230]1012        SANITY_CHECK(i);
[154]1013        ExtValue v;
1014        ParamEntry *pe = entry(i);
1015        if (pe->fun1)
[109]1016        {
[154]1017                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
1018                return v;
[109]1019        }
[154]1020        else
[109]1021        {
[154]1022                void *target = getTarget(i);
1023                return *((ExtValue*)target);
[109]1024        }
1025}
1026
1027
1028//////// set
1029
[247]1030int SimpleAbstractParam::setInt(int i, paInt x)
[109]1031{
[230]1032        SANITY_CHECK(i);
[154]1033        ExtValue v;
1034        ParamEntry *pe = entry(i);
1035        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
[247]1036        paInt xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
[574]1037        paInt mn = 0, mx = 0, de = 0;
[154]1038        int result = 0;
[743]1039        if (getMinMaxIntFromTypeDef(pe->type, mn, mx, de) >= 2)
[316]1040                if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking
[109]1041                {
[822]1042                        if (x < mn) { x = mn; result = PSET_HITMIN; }
1043                        else if (x > mx) { x = mx; result = PSET_HITMAX; }
[109]1044                }
1045
[154]1046        if (pe->fun2)
[109]1047        {
[154]1048                v.setInt(x);
1049                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[109]1050        }
[154]1051        else
[109]1052        {
[154]1053                void *target = getTarget(i);
[247]1054                if (dontcheckchanges || (*((paInt*)target) != x))
[154]1055                {
[109]1056                        result |= PSET_CHANGED;
[247]1057                        *((paInt*)target) = x;
[154]1058                }
[109]1059        }
[805]1060        ::messageOnExceedRange(this, i, result, xcopy);
[109]1061        return result;
1062}
1063
[154]1064int SimpleAbstractParam::setDouble(int i, double x)
[109]1065{
[230]1066        SANITY_CHECK(i);
[154]1067        ExtValue v;
1068        ParamEntry *pe = entry(i);
1069        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
1070        double xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
[574]1071        double mn = 0, mx = 0, de = 0;
[154]1072        int result = 0;
[743]1073        if (getMinMaxDoubleFromTypeDef(pe->type, mn, mx, de) >= 2)
[316]1074                if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking
[109]1075                {
[822]1076                        if (x < mn) { x = mn; result = PSET_HITMIN; }
1077                        else if (x > mx) { x = mx; result = PSET_HITMAX; }
[109]1078                }
1079
[154]1080        if (pe->fun2)
[109]1081        {
[154]1082                v.setDouble(x);
1083                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[109]1084        }
[154]1085        else
[109]1086        {
[154]1087                void *target = getTarget(i);
1088                if (dontcheckchanges || (*((double*)target) != x))
[109]1089                {
[154]1090                        result |= PSET_CHANGED;
1091                        *((double*)target) = x;
[109]1092                }
1093        }
[805]1094        ::messageOnExceedRange(this, i, result, xcopy);
[109]1095        return result;
1096}
1097
[154]1098int SimpleAbstractParam::setString(int i, const SString& x)
[109]1099{
[230]1100        SANITY_CHECK(i);
[154]1101        ExtValue v;
1102        SString vs;
1103        const SString *xx = &x;
1104        ParamEntry *pe = entry(i);
1105        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
1106        SString xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
1107        const char* t = pe->type + 1;
1108        while (*t) if (*t == ' ') break; else t++;
[314]1109        int mn = 0, mx = 0;
[154]1110        int result = 0;
[314]1111        if (sscanf(t, "%d %d", &mn, &mx) == 2) //using getMinMax would also get default value, which is not needed here
[109]1112        {
[314]1113                if ((x.len() > mx) && (mx > 0))
[109]1114                {
[314]1115                        vs = x.substr(0, mx);
[154]1116                        xx = &vs;
1117                        result |= PSET_HITMAX;
[109]1118                }
1119        }
1120
[154]1121        if (pe->fun2)
[109]1122        {
[154]1123                v.setString(*xx);
1124                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[109]1125        }
[154]1126        else
[109]1127        {
[154]1128                void *target = getTarget(i);
1129                if (dontcheckchanges || (!(*((SString*)target) == *xx)))
[109]1130                {
[154]1131                        result |= PSET_CHANGED;
[306]1132                        *((SString*)target) = *xx;
[109]1133                }
1134        }
[805]1135        ::messageOnExceedRange(this, i, result, xcopy);
[109]1136        return result;
1137}
1138
[154]1139int SimpleAbstractParam::setObject(int i, const ExtObject& x)
[109]1140{
[230]1141        SANITY_CHECK(i);
[154]1142        ExtValue v;
1143        ParamEntry *pe = entry(i);
1144        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
[478]1145        if (pe->flags&PARAM_OBJECTSET)
[650]1146        {
1147                ExtObject o = getObject(i);
[478]1148                Param tmp;
[650]1149                ParamInterface* oif = o.getParamInterface(tmp);
[478]1150                int ass;
[650]1151                if (oif && ((ass = oif->findId("assign")) >= 0))
1152                {
1153                        ExtValue arg = x;
1154                        oif->call(ass, &arg, &v);
1155                }
[478]1156                else
1157                        logPrintf("SimpleAbstractParam", "setObject", LOG_ERROR,
[822]1158                                "'%s.%s' is PARAM_OBJECTSET but no 'assign()' in %s", getName(), pe->id, o.interfaceName());
[478]1159                return PSET_CHANGED;
[650]1160        }
[154]1161        ExtObject xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
1162        if (pe->fun2)
[109]1163        {
[154]1164                v.setObject(x);
1165                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[805]1166                ::messageOnExceedRange(this, i, result, xcopy);
[154]1167                return result;
[109]1168        }
[154]1169        else
[109]1170        {
[154]1171                void *target = getTarget(i);
1172                *((ExtObject*)target) = x;
1173                return PSET_CHANGED;
[109]1174        }
1175}
1176
[154]1177int SimpleAbstractParam::setExtValue(int i, const ExtValue& x)
[109]1178{
[230]1179        SANITY_CHECK(i);
[154]1180        ParamEntry *pe = entry(i);
1181        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
1182        ExtValue xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
1183        if (pe->fun2)
[109]1184        {
[154]1185                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x);
[805]1186                ::messageOnExceedRange(this, i, result, xcopy);
[154]1187                return result;
[109]1188        }
[154]1189        else
[109]1190        {
[154]1191                void *target = getTarget(i);
1192                *((ExtValue*)target) = x;
1193                return PSET_CHANGED;
[109]1194        }
1195}
1196
[154]1197void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret)
[109]1198{
[230]1199        SANITY_CHECK(i);
[154]1200        ParamEntry *pe = entry(i);
1201        if (!pe) return;
1202        if (pe->fun1 && (pe->type[0] == 'p'))
1203                (*(void(*)(void*, ExtValue*, ExtValue*))pe->fun1)(object, args, ret);
1204        else
[109]1205        {
[375]1206                logPrintf("SimpleAbstractParam", "call", LOG_ERROR,
[154]1207                        (*pe->type != 'p') ? "'%s.%s' is not a function" : "Internal error - undefined function pointer for '%s.%s'", getName(), pe->id);
[290]1208                ret->setInvalid();
[109]1209        }
1210}
1211
[278]1212void SimpleAbstractParam::setDefault()
[109]1213{
[154]1214        bool save = dontcheckchanges;
1215        dontcheckchanges = 1;
[278]1216        ParamInterface::setDefault();
[154]1217        dontcheckchanges = save;
[109]1218}
1219
[278]1220void SimpleAbstractParam::setDefault(int i)
[109]1221{
[154]1222        bool save = dontcheckchanges;
1223        dontcheckchanges = 1;
[278]1224        ParamInterface::setDefault(i);
[154]1225        dontcheckchanges = save;
[109]1226}
1227
[154]1228// Returns the address of the beginning of the line.
1229// len = line length (without \n).
1230// 0 may mean the line with length=0 or the end of the SString.
1231// poz is advanced to the beginning of the next line.
1232// A typical loop: for(poz=0;poz<s.d;) {line=getline(s,poz,len);...
1233static const char *getline(const SString &s, int &poz, int &len)
[109]1234{
[348]1235        const char *beg = s.c_str() + poz;
1236        if (poz >= s.len()) { poz = s.len(); len = 0; return s.c_str() + s.len(); }
[154]1237        const char *lf = strchr(beg, '\n');
[348]1238        if (!lf) { lf = s.c_str() + s.len() - 1; poz = s.len(); }
1239        else { poz = (int)(lf - s.c_str()) + 1; if (poz > s.len()) poz = s.len(); }
[154]1240        while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break;
[247]1241        len = (int)(lf - beg) + 1;
[154]1242        return beg;
[109]1243}
1244
[720]1245int ParamInterface::loadSingleLine(const SString &s, LoadOptions &options)
[109]1246{
[154]1247        int i; // the index number of the parameter
1248        int tmpi;
1249        int len;
1250        int ret;
1251        int fields_loaded = 0;
1252        const char *t, *lin, *end;
[320]1253        const char *equals_sign, *field_end, *next_field;
[154]1254        char remember;
1255        const char *quote, *quote2;
1256        const char *value, *valstop;
1257        SString tmpvalue;
[650]1258        bool parse_failed = false;
[720]1259        if (options.offset >= s.len()) return fields_loaded;
1260        t = s.c_str() + options.offset;
[109]1261
[720]1262        lin = getline(s, options.offset, len); // all fields must be encoded in a single line
[154]1263        if (!len) return fields_loaded; // empty line = end
1264        i = 0;
1265        end = lin + len;
1266        while (t < end)
1267        {
1268                // processing a single field
[320]1269                // "p:name=field_value,  field_name=field_value  , name=value..."
1270                //                     ^ ^-t (after)           ^ ^_next_field
1271                //                     \_t (before)            \_field_end
[325]1272                while (isspace(*t)) if (t < end) t++; else return fields_loaded;
[109]1273
[320]1274                field_end = strchrlimit(t, ',', end); if (!field_end) field_end = end;
[333]1275                next_field = field_end;
[822]1276                while ((field_end > t) && isblank(field_end[-1])) field_end--;
[320]1277                quote = strchrlimit(t, '\"', field_end);
[154]1278                if (quote)
[109]1279                {
[154]1280                        quote2 = skipQuoteString(quote + 1, end);
[320]1281                        if (quote2 > field_end)
[154]1282                        {
[320]1283                                field_end = strchrlimit(quote2 + 1, ',', end);
[393]1284                                if (!field_end) field_end = end;
1285                                next_field = field_end;
[154]1286                        }
1287                        equals_sign = strchrlimit(t, '=', quote);
[109]1288                }
[154]1289                else
1290                {
[320]1291                        equals_sign = strchrlimit(t, '=', field_end);
[154]1292                        quote2 = 0;
1293                }
1294                if (equals_sign == t) { t++; equals_sign = 0; }
[320]1295                if (field_end == t)     // skip empty value
[154]1296                {
1297                        t++; i++;
1298                        continue;
1299                }
1300                if (equals_sign) // have parameter name
1301                {
[247]1302                        tmpi = findIdn(t, (int)(equals_sign - t));
[154]1303                        i = tmpi;
1304                        if (tmpi < 0)
[312]1305                        {
1306                                SString name(t, (int)(equals_sign - t));
[796]1307                                logPrintf("Param", "loadSingleLine", LOG_WARN, "Unknown property '%s.%s' (ignored)", getName(), name.c_str());
[312]1308                        }
[154]1309                        t = equals_sign + 1; // t=value
1310                }
[109]1311#ifdef WARN_MISSING_NAME
[154]1312                else
[109]1313#ifdef SAVE_SELECTED_NAMES
[650]1314                        if ((i >= getPropCount()) || !(flags(i)&PARAM_CANOMITNAME))
[109]1315#endif
[154]1316                        {
[822]1317                                if (id(i))
1318                                        logPrintf("Param", "loadSingleLine", LOG_WARN, "Missing property name in '%s' (assuming '%s')", getName(), id(i));
1319                                else
1320                                        logPrintf("Param", "loadSingleLine", LOG_WARN, "Value after the last property of '%s'", getName());
[154]1321                        }
[109]1322#endif
[154]1323                if ((i >= 0) && id(i))
[109]1324                {
[154]1325                        value = t;
1326                        if (quote)
1327                        {
[247]1328                                tmpvalue.copyFrom(quote + 1, (int)(quote2 - quote) - 1);
[154]1329                                sstringUnquote(tmpvalue);
[348]1330                                value = tmpvalue.c_str();
[154]1331                                valstop = quote2;
1332                        }
1333                        else
[320]1334                                if (field_end < end) valstop = field_end; else valstop = end;
[154]1335
1336                        remember = *valstop;
1337                        *(char*)valstop = 0;
[743]1338                        ret = setFromString(i, value, true);
[154]1339                        fields_loaded++;
[393]1340                        if (ret&PSET_PARSEFAILED)
[650]1341                                parse_failed = true;
[154]1342                        *(char*)valstop = remember;
[109]1343                }
1344
[154]1345                if (i >= 0) i++;
[109]1346#ifdef __CODEGUARD__
[732]1347                if (next_field < end - 1) t = next_field + 1; else return fields_loaded;
[109]1348#else
[320]1349                t = next_field + 1;
[109]1350#endif
[154]1351        }
[720]1352        if (parse_failed) options.parse_failed = true;
1353        return fields_loaded;
[109]1354}
1355
[154]1356int Param::grmember(int g, int a)
[109]1357{
[154]1358        if ((getGroupCount() < 2) && (!g))
1359                return (a < getPropCount()) ? a : -9999;
[109]1360
[154]1361        ParamEntry *e = entry(0);
1362        int x = 0, i = 0;
1363        for (; e->id; i++, e++)
[109]1364        {
[154]1365                if (e->group == g)
1366                        if (a == x) return i; else x++;
[109]1367        }
[154]1368        return -9999;
[109]1369}
Note: See TracBrowser for help on using the repository browser.