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

Last change on this file since 973 was 973, checked in by Maciej Komosinski, 4 years ago

Increased SString and std::string compatibility: introduced length(), size(), and capacity(), and removed legacy methods that have std::string equivalents

  • Property svn:eol-style set to native
File size: 34.9 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[973]2// Copyright (C) 1999-2020  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++)
[973]58                if ((!(flags(i) & PARAM_READONLY))
[154]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++)
[973]73                if ((!(flags(i) & PARAM_READONLY))
[154]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                {
[973]180                        if (def.length() > 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{
[973]305        if ((flags(i) & PARAM_DONTSAVE) && (!force)) return 0;
[154]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();
[973]325        if (ws.length() > 50) cr = 1;
[154]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
[950]333SString ParamInterface::nameDotProperty(int prop)
334{
335        SString ret = getName();
[973]336        ret += ".";
337        ret += id(prop);
[950]338        return ret;
339}
[109]340
[950]341SString ParamInterface::nameDotPropertyForMessages(int prop)
342{
343        SString name_dot_prop = nameDotProperty(prop);
[973]344        if (strcmp(getName(), getLongName()) == 0)
345        {
346                if (strcmp(id(prop), name(prop)) == 0)
[950]347                        return name_dot_prop;
348                else
[973]349                        return SString("'") + name(prop) + "': " + name_dot_prop;
350        }
[950]351        else
[973]352                return SString("'") + name(prop) + "' in '" + getLongName() + "': " + name_dot_prop;
[950]353}
354
[154]355int SimpleAbstractParam::isequal(int i, void* defdata)
[109]356{ // defdata->member == object->member ?
[154]357        void *backup = object;
358        switch (type(i)[0])
[109]359        {
360        case 'd':
[154]361        {
[109]362                select(defdata);
[247]363                paInt x = getInt(i);
[109]364                select(backup);
[154]365                return x == getInt(i);
366        }
[109]367        case 'f':
[154]368        {
[109]369                select(defdata);
[154]370                double x = getDouble(i);
[109]371                select(backup);
[154]372                return x == getDouble(i);
373        }
[109]374        case 's':
[154]375        {
[109]376                select(defdata);
[154]377                SString x = getString(i);
[109]378                select(backup);
[154]379                return x == getString(i);
[109]380        }
[154]381        }
382        return 1;
[109]383}
384
[720]385void SimpleAbstractParam::saveSingleLine(SString& f, void *defdata, bool addcr, bool all_names)
[154]386{ // defdata!=NULL -> does not save default values
387        const char *p;
388        int i;
389        int needlabel = 0;
390        int first = 1;
391        SString val;
392        SString t;
393        int fl;
394        // t+=SString(getName()); t+=':';
395        for (i = 0; p = id(i); i++)
[973]396                if (!((fl = flags(i)) & PARAM_DONTSAVE))
[109]397                {
[822]398                        if (defdata && isequal(i, defdata))
399                                needlabel = 1;
400                        else
401                        {
402                                if (!first) t += ", ";
[109]403#ifndef SAVE_ALL_NAMES
404#ifdef SAVE_SELECTED_NAMES
[822]405                                if (needlabel || all_names || !(fl & PARAM_CANOMITNAME))
[109]406#else
[822]407                                if (needlabel)
[109]408#endif
409#endif
410                                {
[822]411                                        t += p; t += "="; needlabel = 0;
[109]412                                }
[822]413                                if (type(i)[0] == 's')
414                                { // string - special case
415                                        SString str = getString(i);
416                                        if (strContainsOneOf(str.c_str(), ", \\\n\r\t\""))
417                                        {
418                                                t += "\"";
419                                                sstringQuote(str);
420                                                t += str;
421                                                t += "\"";
422                                        }
423                                        else
424                                                t += str;
425                                }
[154]426                                else
[822]427                                        t += get(i);
428                                first = 0;
[109]429                        }
430                }
[154]431        if (addcr)
432                t += "\n";
433        f += t;
[109]434}
435
[650]436static void closingTildeError(ParamInterface *pi, VirtFILE *file, int field_index)
437{
438        SString fileinfo;
439        const char* fname = file->VgetPath();
440        if (fname != NULL)
441                fileinfo = SString::sprintf(" while reading from '%s'", fname);
442        SString field;
443        if (field_index >= 0)
[950]444                field = pi->nameDotPropertyForMessages(field_index);
[650]445        else
446                field = SString::sprintf("unknown property of '%s'", pi->getName());
447        logPrintf("ParamInterface", "load", LOG_WARN, "Closing '~' (tilde) not found in %s%s", field.c_str(), fileinfo.c_str());
448}
449
[805]450template<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]451{
452        if (setflags & (PSET_HITMIN | PSET_HITMAX))
453        {
[805]454                ExtValue v(valuetoset);
455                pi->messageOnExceedRange(i, setflags, v);
456        }
457}
458
459void SimpleAbstractParam::messageOnExceedRange(int i, int setflags, ExtValue& valuetoset) ///< prints a warning when setflags indicates that allowed param range has been exceeded during set
460{
461        if (setflags & (PSET_HITMIN | PSET_HITMAX))
462        {
[796]463                SString svaluetoset = valuetoset.getString(); //converts any type to SString
464                SString actual = get(i);
465                bool s_type = type(i)[0] == 's';
466                bool show_length = valuetoset.getType() == TString;
467                const char* quote = (valuetoset.getType() == TString) ? "\"" : "'";
[950]468                logPrintf("Param", "set", LOG_WARN, "Setting %s = %s exceeded allowed range (too %s). %s to %s.",
469                        nameDotPropertyForMessages(i).c_str(),
[796]470                        ::sstringDelimitAndShorten(svaluetoset, 30, show_length, quote, quote).c_str(),
[973]471                        (setflags & PSET_HITMAX) ? (s_type ? "long" : "big") : "small", s_type ? "Truncated" : "Adjusted",
[796]472                        ::sstringDelimitAndShorten(actual, 30, show_length, quote, quote).c_str()
[822]473                );
[796]474        }
475}
476
[720]477int ParamInterface::load(FileFormat format, VirtFILE* f, LoadOptions *options)
[109]478{
[720]479        LoadOptions default_options;
480        if (options == NULL)
481                options = &default_options;
482        switch (format)
483        {
484        case FormatMultiLine:
485                return loadMultiLine(f, *options);
486
487        case FormatSingleLine:
488        {
489                StringFILE *sf = dynamic_cast<StringFILE*>(f);
490                SString s;
491                if (sf)
492                {
493                        s = sf->getString().c_str();
494                        options->offset += sf->Vtell();
495                }
496                else
497                {
498                        if (!loadSStringLine(f, s))
499                                return -1;
500                }
501                return loadSingleLine(s, *options);
502        }
503        }
504        return -1;
505}
506
507int ParamInterface::load(FileFormat format, const SString &s, LoadOptions *options)
508{
509        LoadOptions default_options;
510        if (options == NULL)
511                options = &default_options;
512        switch (format)
513        {
514        case FormatMultiLine:
515        {
516                string std_string(s.c_str());
517                StringFILE f(std_string);
518                return loadMultiLine(&f, *options);
519        }
520
521        case FormatSingleLine:
522                return loadSingleLine(s, *options);
523        }
524        return -1;
525}
526
527int ParamInterface::loadMultiLine(VirtFILE* f, LoadOptions &options)
528{
[154]529        SString buf;
530        int i;
531        const char *p, *p0;
532        int p_len;
533        bool loaded;
534        int fields_loaded = 0;
[413]535        int unexpected_line = 0;
[426]536        vector<bool> seen;
537        seen.resize(getPropCount());
[650]538        if ((i = findId("beforeLoad")) >= 0)
539                call(i, NULL, NULL);
[720]540        while (((!options.abortable) || (!*options.abortable)) && loadSStringLine(f, buf))
[109]541        {
[720]542                if (options.linenum) (*options.linenum)++;
[348]543                const char* t = buf.c_str();
[413]544                p0 = t; while (isblank(*p0)) p0++;
[154]545                if (!*p0) break;
[413]546                if (p0[0] == '#') { unexpected_line = 0; continue; }
547                p = strchr(p0, ':');
548                if (!p)
[650]549                {
550                        switch (unexpected_line)
[413]551                        {
[650]552                        case 0:
553                                logPrintf("ParamInterface", "load", LOG_WARN, "Ignored unexpected line %s while reading object '%s'",
[720]554                                        options.linenum ?
555                                        SString::sprintf("%d", *options.linenum).c_str()
[413]556                                        : SString::sprintf("'%s'", p0).c_str(),
[650]557                                        getName());
558                                break;
559                        case 1:
560                                logPrintf("ParamInterface", "load", LOG_WARN, "The following line(s) were also unexpected and were ignored");
561                                break;
562                        }
[413]563                        unexpected_line++;
564                        continue;
[650]565                }
[413]566                unexpected_line = 0;
[247]567                p_len = (int)(p - p0);
[154]568                loaded = false;
[268]569                if (p_len && ((i = findIdn(p0, p_len)) >= 0))
[109]570                {
[426]571                        if (seen[i])
[650]572                        {
573                                SString fileinfo;
574                                const char* fname = f->VgetPath();
575                                if (fname != NULL)
[426]576                                {
[650]577                                        fileinfo = SString::sprintf(" while reading from '%s'", fname);
[720]578                                        if (options.linenum)
579                                                fileinfo += SString::sprintf(" (line %d)", *options.linenum);
[426]580                                }
[796]581                                logPrintf("ParamInterface", "load", LOG_WARN, "Multiple '%s.%s' properties found%s", getName(), id(i), fileinfo.c_str());
[650]582                        }
[426]583                        else
[650]584                                seen[i] = true;
[973]585                        if (!(flags(i) & PARAM_DONTLOAD))
[109]586                        {
[312]587                                if (p0[p_len + 1] == '~')
588                                {
589                                        SString s;
[650]590                                        if (!readUntilTilde(f, s))
591                                                closingTildeError(this, f, i);
[333]592                                        int lfcount = 1;
[348]593                                        const char* tmp = s.c_str();
[333]594                                        while (tmp)
595                                                if ((tmp = strchr(tmp, '\n')))
596                                                {
[822]597                                                        lfcount++; tmp++;
[333]598                                                }
[312]599                                        removeCR(s);
[523]600                                        int ch; while ((ch = f->Vgetc()) != EOF) if (ch == '\n') break;
[312]601                                        unquoteTilde(s);
[973]602                                        if (options.linenum && (flags(i) & PARAM_LINECOMMENT))
[720]603                                                s = SString::sprintf("@file %s\n@line %d\n", f->VgetPath(), *options.linenum + 1) + s;
[743]604                                        setFromString(i, s.c_str(), false);
[720]605                                        if (options.linenum)
606                                                (*options.linenum) += lfcount;
[312]607                                }
608                                else
609                                {
[883]610                                        SString s = SString(p0 + p_len + 1);
611                                        unquoteTilde(s);
612                                        setFromString(i, s.c_str(), false);
[312]613                                }
614                                fields_loaded++;
615                                loaded = true;
[109]616                        }
617                }
[720]618                else if (options.warn_unknown_fields)
[312]619                {
620                        SString name(p0, p_len);
[796]621                        logPrintf("ParamInterface", "load", LOG_WARN, "Ignored unknown property '%s.%s'", getName(), name.c_str());
[312]622                }
[268]623
[154]624                if ((!loaded) && (p0[p_len + 1] == '~'))
[109]625                { // eat unrecognized multiline field
[154]626                        SString s;
[650]627                        if (!readUntilTilde(f, s))
628                                closingTildeError(this, f, -1);
[720]629                        if (options.linenum)
[333]630                        {
[348]631                                const char* tmp = s.c_str();
[333]632                                int lfcount = 1;
633                                while (tmp)
634                                        if ((tmp = strchr(tmp, '\n')))
635                                        {
[822]636                                                lfcount++; tmp++;
[333]637                                        }
[720]638                                (*options.linenum) += lfcount;
[333]639                        }
[523]640                        int ch; while ((ch = f->Vgetc()) != EOF) if (ch == '\n') break;
[109]641                }
642        }
[650]643        if ((i = findId("afterLoad")) >= 0)
644                call(i, NULL, NULL);
[154]645        return fields_loaded;
[109]646}
647
648
649/*
650SString SimpleAbstractParam::getString(int i)
651{
652char *t;
653switch (*(t=type(i)))
[312]654{
[333]655case 'd':
656{
657for (i=atol(get(i));i>=0;i--) if (t) t=strchr(t+1,'~');
658if (t)
659{
660t++;
661char *t2=strchr(t,'~');
662if (!t2) t2=t+strlen(t);
663SString str;
664strncpy(str.directWrite(t2-t),t,t2-t);
665str.endWrite(t2-t);
666return str;
[312]667}
[333]668}
669}
[109]670return get(i);
671}
672*/
673
674int ParamInterface::findId(const char* n)
675{
[154]676        int i; const char *p;
677        for (i = 0; p = id(i); i++) if (!strcmp(n, p)) return i;
678        return -1;
[109]679}
680
[154]681int ParamInterface::findIdn(const char* naz, int n)
[109]682{
[154]683        int i; const char *p;
684        for (i = 0; p = id(i); i++) if ((!strncmp(naz, p, n)) && (!p[n])) return i;
685        return -1;
[109]686}
687
[154]688void ParamInterface::get(int i, ExtValue &ret)
[109]689{
[154]690        switch (type(i)[0])
[109]691        {
692        case 'd':       ret.setInt(getInt(i)); break;
693        case 'f':       ret.setDouble(getDouble(i)); break;
694        case 's':       ret.setString(getString(i)); break;
695        case 'o':       ret.setObject(getObject(i)); break;
[154]696        case 'x':       ret = getExtValue(i); break;
[950]697        default: logPrintf("ParamInterface", "get", LOG_ERROR, "%s is not a property", nameDotPropertyForMessages(i).c_str());
[109]698        }
699}
700
[743]701int ParamInterface::setIntFromString(int i, const char* str, bool strict)
[109]702{
[326]703        paInt value;
[645]704        if (!ExtValue::parseInt(str, value, strict, true))
[109]705        {
[314]706                paInt mn, mx, def;
[743]707                if (getMinMaxInt(i, mn, mx, def) >= 3)
[393]708                        return setInt(i, def) | PSET_PARSEFAILED;
[154]709                else
[393]710                        return setInt(i, (paInt)0) | PSET_PARSEFAILED;
[154]711        }
[109]712        else
[326]713                return setInt(i, value);
[109]714}
715
[743]716int ParamInterface::setDoubleFromString(int i, const char* str)
[109]717{
[326]718        double value;
[333]719        if (!ExtValue::parseDouble(str, value, true))
[109]720        {
[314]721                double mn, mx, def;
[743]722                if (getMinMaxDouble(i, mn, mx, def) >= 3)
[393]723                        return setDouble(i, def) | PSET_PARSEFAILED;
[154]724                else
[393]725                        return setDouble(i, (double)0) | PSET_PARSEFAILED;
[154]726        }
[109]727        else
[326]728                return setDouble(i, value);
[109]729}
730
[154]731int ParamInterface::set(int i, const ExtValue &v)
[109]732{
[154]733        switch (type(i)[0])
[109]734        {
[144]735        case 'd':
[154]736                if ((v.type == TInt) || (v.type == TDouble)) return setInt(i, v.getInt());
[144]737                else
[154]738                {
739                        if (v.type == TObj)
[333]740                        {
[950]741                                logPrintf("ParamInterface", "set", LOG_ERROR, "Setting int %s from object reference (%s)", nameDotPropertyForMessages(i).c_str(), v.getString().c_str());
[333]742                                return 0;
743                        }
744                        else
[743]745                                return setIntFromString(i, v.getString().c_str(), false);
[154]746                }
[144]747        case 'f':
[154]748                if ((v.type == TInt) || (v.type == TDouble)) return setDouble(i, v.getDouble());
[144]749                else
[154]750                {
751                        if (v.type == TObj)
[333]752                        {
[950]753                                logPrintf("ParamInterface", "set", LOG_ERROR, "Setting float %s from object reference (%s)", nameDotPropertyForMessages(i).c_str(), v.getString().c_str());
[333]754                                return 0;
755                        }
756                        else
[743]757                                return setDoubleFromString(i, v.getString().c_str());
[154]758                }
759        case 's': { SString t = v.getString(); return setString(i, t); }
[478]760        case 'o':
[650]761                if ((v.type != TUnknown) && (v.type != TObj))
[950]762                        logPrintf("ParamInterface", "set", LOG_ERROR, "Setting object %s from %s", nameDotPropertyForMessages(i).c_str(), v.typeAndValue().c_str());
[478]763                else
764                        return setObject(i, v.getObject());
765                break;
[154]766        case 'x': return setExtValue(i, v);
[950]767        default: logPrintf("ParamInterface", "set", LOG_ERROR, "%s is not a property", nameDotPropertyForMessages(i).c_str());
[109]768        }
[154]769        return 0;
[109]770}
771
[743]772int ParamInterface::setFromString(int i, const char *v, bool strict)
[109]773{
[312]774        char typ = type(i)[0];
[273]775        switch (typ)
[109]776        {
[743]777        case 'd': return setIntFromString(i, v, strict);
778        case 'f': return setDoubleFromString(i, v);
[154]779        case 's': { SString t(v); return setString(i, t); }
[273]780        case 'x': case 'o':
[109]781        {
[154]782                ExtValue e;
783                const char* after;
784                if (!strncmp(v, SERIALIZATION_PREFIX, strlen(SERIALIZATION_PREFIX)))
[109]785                {
[154]786                        after = e.deserialize(v + strlen(SERIALIZATION_PREFIX));
787                        if ((after == NULL) || (*after))
[343]788                        {
[478]789                                logPrintf("ParamInterface", "set", LOG_ERROR, "serialization format mismatch in %s.%s", (getName() ? getName() : "<Unknown>"), id(i));
[334]790                                e.setEmpty();
[343]791                        }
[109]792                }
[154]793                else if ((after = e.parseNumber(v)) && (*after == 0)) //consumed the whole string
[109]794                {
[154]795                        //OK!
[109]796                }
[154]797                else
[109]798                {
[154]799                        e.setString(SString(v));
[109]800                }
[312]801                if (typ == 'x')
[273]802                        return setExtValue(i, e);
803                else
804                        return setObject(i, e.getObject());
[109]805        }
806        }
[154]807        return 0;
[109]808}
809
[704]810SString ParamInterface::getText(int i) //find the current enum text or call get(i) if not enum
[109]811{
[154]812        const char *t;
[720]813        if (((*(t = type(i))) == 'd') && (strchr(t, '~') != NULL)) //type is int and contains enum labels
[109]814        {
[314]815                paInt mn, mx, def;
[312]816                int value = getInt(i);
[743]817                if (getMinMaxIntFromTypeDef(t, mn, mx, def) >= 2)
[312]818                {
[314]819                        if (value > mx)
[704]820                                return get(i);//unexpected value of out bounds (should never happen) -> fallback
[314]821                        value -= mn;
[312]822                }
[704]823                if (value < 0) return get(i); //unexpected value of out bounds (should never happen) -> fallback
824                // now value is 0-based index of ~text
825                for (; value >= 0; value--) if (t) t = strchr(t + 1, '~'); else break;
826                if (t) // found n-th ~text in type description (else: not enough ~texts in type description)
[109]827                {
[154]828                        t++;
829                        const char *t2 = strchr(t, '~');
830                        if (!t2) t2 = t + strlen(t);
[247]831                        return SString(t, (int)(t2 - t));
[109]832                }
833        }
[704]834        return get(i); //fallback - return int value as string
[109]835}
836
837SString ParamInterface::get(int i)
838{
[154]839        switch (type(i)[0])
[109]840        {
841        case 'd': return SString::valueOf(getInt(i));
842        case 'f': return SString::valueOf(getDouble(i));
843        case 's': return getString(i);
844        }
[154]845        ExtValue v;
846        get(i, v);
847        return v.getString();
[109]848}
849
[535]850bool ParamInterface::isValidTypeDescription(const char* t)
851{
[650]852        if (t == NULL) return false;
853        if (*t == 0) return false;
854        if (strchr("dfsoxp", *t) == NULL) return false;
855        switch (*t)
[574]856        {
857        case 'd':
[754]858        {
859                paInt a, b, c;
860                int have = getMinMaxIntFromTypeDef(t, a, b, c);
861                if (have == 1) return false;
862                if ((have >= 2) && (b < a) && (a != 0) && (b != -1)) return false; // max<min meaning 'undefined' is only allowed as "d 0 -1"
863        }
[822]864        break;
[574]865        case 'f':
[754]866        {
867                double a, b, c;
868                int have = getMinMaxDoubleFromTypeDef(t, a, b, c);
869                if (have == 1) return false;
870                if ((have >= 2) && (b < a) && (a != 0) && (b != -1)) return false; // max<min meaning 'undefined' is only allowed as "f 0 -1"
871        }
[822]872        break;
[754]873        case 's':
874        {
875                int a, b; SString c;
876                int have = getMinMaxStringFromTypeDef(t, a, b, c);
877                //if (have == 1) return false; //not sure?
878                if ((have >= 1) && (!((a == 0) || (a == 1)))) return false; // 'min' for string (single/multi) can be only 0 or 1
879                if ((have >= 2) && (b < 0)) return false; // max=0 means unlimited, max<0 is not allowed
[574]880        }
[822]881        break;
[754]882        }
[650]883        return true;
[535]884}
[109]885
[743]886SString ParamInterface::friendlyTypeDescrFromTypeDef(const char* type)
[640]887{
[650]888        SString t;
889        switch (type[0])
[640]890        {
[650]891        case 'd': t += "integer";
[743]892        {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]893        break;
[650]894        case 'f': t += "float";
[743]895        {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]896        break;
[650]897        case 's': t += "string";
[743]898        {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]899        break;
[650]900        case 'x': t += "untyped value"; break;
901        case 'p': t += "function"; break;
902        case 'o': t += "object"; if (type[1]) { t += " of class "; t += type + 1; } break;
[640]903        default: return "unknown type";
904        }
[650]905        return t;
[640]906}
907
[109]908//////////////////////////////// PARAM ////////////////////////////////////
909
[483]910#ifdef _DEBUG
[230]911void SimpleAbstractParam::sanityCheck(int i)
912{
[650]913        ParamEntry *pe = entry(i);
[230]914
[650]915        const char* t = pe->type;
916        const char* err = NULL;
[230]917
[535]918        if (!isValidTypeDescription(t))
[650]919                err = "invalid type description";
920        if (*t == 'p')
[230]921        {
[650]922                if (pe->fun1 == NULL)
[822]923                {
924                        MutableParamInterface *mpi = dynamic_cast<MutableParamInterface*>(this);
925                        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.
926                                err = "no procedure defined";
927                }
[654]928                if (pe->flags & PARAM_READONLY)
929                        err = "function can't be PARAM_READONLY";
[230]930        }
[312]931        else
[230]932        {
[732]933                if ((t[0] == 'o') && (t[1] == ' '))
934                {
935                        err = "space after 'o'";
936                }
[659]937                if (!(pe->flags & (PARAM_READONLY | PARAM_DONTSAVE | PARAM_USERREADONLY | PARAM_CONST | PARAM_DONTLOAD | PARAM_LINECOMMENT | PARAM_OBJECTSET)))
[230]938                { //write access
[650]939                        if ((pe->fun2 == NULL) && (pe->offset == PARAM_ILLEGAL_OFFSET))
940                                err = "no field defined (GETONLY without PARAM_READONLY?)";
[230]941                }
942        }
[650]943        if (err != NULL)
944                logPrintf("SimpleAbstractParam", "sanityCheck", LOG_ERROR,
[973]945                        "Invalid ParamEntry for %s (%s)", nameDotPropertyForMessages(i).c_str(), err);
[650]946}
[230]947#endif
948
[109]949void *SimpleAbstractParam::getTarget(int i)
950{
[154]951        return (void*)(((char*)object) + entry(i)->offset);
952        //return &(object->*(entry(i)->fldptr));
[109]953}
954
955///////// get
956
[483]957#ifdef _DEBUG
[230]958#define SANITY_CHECK(i) sanityCheck(i)
959#else
960#define SANITY_CHECK(i)
961#endif
962
[247]963paInt SimpleAbstractParam::getInt(int i)
[109]964{
[230]965        SANITY_CHECK(i);
[154]966        ExtValue v;
967        ParamEntry *pe = entry(i);
968        if (pe->fun1)
[109]969        {
[154]970                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
971                return v.getInt();
[109]972        }
[154]973        else
[109]974        {
[154]975                void *target = getTarget(i);
[247]976                return *((paInt*)target);
[109]977        }
978}
979
980double SimpleAbstractParam::getDouble(int i)
981{
[230]982        SANITY_CHECK(i);
[154]983        ExtValue v;
984        ParamEntry *pe = entry(i);
985        if (pe->fun1)
[109]986        {
[154]987                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
988                return v.getDouble();
[109]989        }
[154]990        else
[109]991        {
[154]992                void *target = getTarget(i);
993                return *((double*)target);
[109]994        }
995}
996
997SString SimpleAbstractParam::getString(int i)
998{
[230]999        SANITY_CHECK(i);
[154]1000        ExtValue v;
1001        ParamEntry *pe = entry(i);
1002        if (pe->fun1)
[109]1003        {
[154]1004                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
1005                return v.getString();
[109]1006        }
[154]1007        else
[109]1008        {
[154]1009                void *target = getTarget(i);
1010                return *((SString*)target);
[109]1011        }
1012}
1013
1014ExtObject SimpleAbstractParam::getObject(int i)
1015{
[230]1016        SANITY_CHECK(i);
[154]1017        ExtValue v;
1018        ParamEntry *pe = entry(i);
1019        if (pe->fun1)
[109]1020        {
[154]1021                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
1022                return v.getObject();
[109]1023        }
[154]1024        else
[109]1025        {
[154]1026                void *target = getTarget(i);
1027                return *((ExtObject*)target);
[109]1028        }
1029}
1030
1031ExtValue SimpleAbstractParam::getExtValue(int i)
1032{
[230]1033        SANITY_CHECK(i);
[154]1034        ExtValue v;
1035        ParamEntry *pe = entry(i);
1036        if (pe->fun1)
[109]1037        {
[154]1038                (*(void(*)(void*, ExtValue*))pe->fun1)(object, &v);
1039                return v;
[109]1040        }
[154]1041        else
[109]1042        {
[154]1043                void *target = getTarget(i);
1044                return *((ExtValue*)target);
[109]1045        }
1046}
1047
1048
1049//////// set
1050
[247]1051int SimpleAbstractParam::setInt(int i, paInt x)
[109]1052{
[230]1053        SANITY_CHECK(i);
[154]1054        ExtValue v;
1055        ParamEntry *pe = entry(i);
[973]1056        if (pe->flags & PARAM_READONLY) return PSET_RONLY;
[247]1057        paInt xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
[574]1058        paInt mn = 0, mx = 0, de = 0;
[154]1059        int result = 0;
[743]1060        if (getMinMaxIntFromTypeDef(pe->type, mn, mx, de) >= 2)
[316]1061                if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking
[109]1062                {
[822]1063                        if (x < mn) { x = mn; result = PSET_HITMIN; }
1064                        else if (x > mx) { x = mx; result = PSET_HITMAX; }
[109]1065                }
1066
[154]1067        if (pe->fun2)
[109]1068        {
[154]1069                v.setInt(x);
1070                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[109]1071        }
[154]1072        else
[109]1073        {
[154]1074                void *target = getTarget(i);
[247]1075                if (dontcheckchanges || (*((paInt*)target) != x))
[154]1076                {
[109]1077                        result |= PSET_CHANGED;
[247]1078                        *((paInt*)target) = x;
[154]1079                }
[109]1080        }
[805]1081        ::messageOnExceedRange(this, i, result, xcopy);
[109]1082        return result;
1083}
1084
[154]1085int SimpleAbstractParam::setDouble(int i, double x)
[109]1086{
[230]1087        SANITY_CHECK(i);
[154]1088        ExtValue v;
1089        ParamEntry *pe = entry(i);
[973]1090        if (pe->flags & PARAM_READONLY) return PSET_RONLY;
[154]1091        double xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
[574]1092        double mn = 0, mx = 0, de = 0;
[154]1093        int result = 0;
[743]1094        if (getMinMaxDoubleFromTypeDef(pe->type, mn, mx, de) >= 2)
[316]1095                if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking
[109]1096                {
[822]1097                        if (x < mn) { x = mn; result = PSET_HITMIN; }
1098                        else if (x > mx) { x = mx; result = PSET_HITMAX; }
[109]1099                }
1100
[154]1101        if (pe->fun2)
[109]1102        {
[154]1103                v.setDouble(x);
1104                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[109]1105        }
[154]1106        else
[109]1107        {
[154]1108                void *target = getTarget(i);
1109                if (dontcheckchanges || (*((double*)target) != x))
[109]1110                {
[154]1111                        result |= PSET_CHANGED;
1112                        *((double*)target) = x;
[109]1113                }
1114        }
[805]1115        ::messageOnExceedRange(this, i, result, xcopy);
[109]1116        return result;
1117}
1118
[154]1119int SimpleAbstractParam::setString(int i, const SString& x)
[109]1120{
[230]1121        SANITY_CHECK(i);
[154]1122        ExtValue v;
1123        SString vs;
1124        const SString *xx = &x;
1125        ParamEntry *pe = entry(i);
[973]1126        if (pe->flags & PARAM_READONLY) return PSET_RONLY;
[154]1127        SString xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
1128        const char* t = pe->type + 1;
1129        while (*t) if (*t == ' ') break; else t++;
[314]1130        int mn = 0, mx = 0;
[154]1131        int result = 0;
[314]1132        if (sscanf(t, "%d %d", &mn, &mx) == 2) //using getMinMax would also get default value, which is not needed here
[109]1133        {
[973]1134                if ((x.length() > mx) && (mx > 0))
[109]1135                {
[314]1136                        vs = x.substr(0, mx);
[154]1137                        xx = &vs;
1138                        result |= PSET_HITMAX;
[109]1139                }
1140        }
1141
[154]1142        if (pe->fun2)
[109]1143        {
[154]1144                v.setString(*xx);
1145                result |= (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[109]1146        }
[154]1147        else
[109]1148        {
[154]1149                void *target = getTarget(i);
1150                if (dontcheckchanges || (!(*((SString*)target) == *xx)))
[109]1151                {
[154]1152                        result |= PSET_CHANGED;
[306]1153                        *((SString*)target) = *xx;
[109]1154                }
1155        }
[805]1156        ::messageOnExceedRange(this, i, result, xcopy);
[109]1157        return result;
1158}
1159
[154]1160int SimpleAbstractParam::setObject(int i, const ExtObject& x)
[109]1161{
[230]1162        SANITY_CHECK(i);
[154]1163        ExtValue v;
1164        ParamEntry *pe = entry(i);
[973]1165        if (pe->flags & PARAM_READONLY) return PSET_RONLY;
1166        if (pe->flags & PARAM_OBJECTSET)
[650]1167        {
1168                ExtObject o = getObject(i);
[478]1169                Param tmp;
[650]1170                ParamInterface* oif = o.getParamInterface(tmp);
[478]1171                int ass;
[650]1172                if (oif && ((ass = oif->findId("assign")) >= 0))
1173                {
1174                        ExtValue arg = x;
1175                        oif->call(ass, &arg, &v);
1176                }
[478]1177                else
1178                        logPrintf("SimpleAbstractParam", "setObject", LOG_ERROR,
[950]1179                                "%s is PARAM_OBJECTSET but no 'assign()' in %s", nameDotPropertyForMessages(i).c_str(), o.interfaceName());
[478]1180                return PSET_CHANGED;
[650]1181        }
[154]1182        ExtObject 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                v.setObject(x);
1186                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &v);
[805]1187                ::messageOnExceedRange(this, i, result, xcopy);
[154]1188                return result;
[109]1189        }
[154]1190        else
[109]1191        {
[154]1192                void *target = getTarget(i);
1193                *((ExtObject*)target) = x;
1194                return PSET_CHANGED;
[109]1195        }
1196}
1197
[154]1198int SimpleAbstractParam::setExtValue(int i, const ExtValue& x)
[109]1199{
[230]1200        SANITY_CHECK(i);
[154]1201        ParamEntry *pe = entry(i);
[973]1202        if (pe->flags & PARAM_READONLY) return PSET_RONLY;
[154]1203        ExtValue xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
1204        if (pe->fun2)
[109]1205        {
[154]1206                int result = (*(int(*)(void*, const ExtValue*))pe->fun2)(object, &x);
[805]1207                ::messageOnExceedRange(this, i, result, xcopy);
[154]1208                return result;
[109]1209        }
[154]1210        else
[109]1211        {
[154]1212                void *target = getTarget(i);
1213                *((ExtValue*)target) = x;
1214                return PSET_CHANGED;
[109]1215        }
1216}
1217
[154]1218void SimpleAbstractParam::call(int i, ExtValue *args, ExtValue *ret)
[109]1219{
[230]1220        SANITY_CHECK(i);
[154]1221        ParamEntry *pe = entry(i);
1222        if (!pe) return;
1223        if (pe->fun1 && (pe->type[0] == 'p'))
1224                (*(void(*)(void*, ExtValue*, ExtValue*))pe->fun1)(object, args, ret);
1225        else
[109]1226        {
[375]1227                logPrintf("SimpleAbstractParam", "call", LOG_ERROR,
[950]1228                        (*pe->type != 'p') ? "%s is not a function" : "Internal error - undefined function pointer for %s", nameDotPropertyForMessages(i).c_str());
[290]1229                ret->setInvalid();
[109]1230        }
1231}
1232
[278]1233void SimpleAbstractParam::setDefault()
[109]1234{
[154]1235        bool save = dontcheckchanges;
1236        dontcheckchanges = 1;
[278]1237        ParamInterface::setDefault();
[154]1238        dontcheckchanges = save;
[109]1239}
1240
[278]1241void SimpleAbstractParam::setDefault(int i)
[109]1242{
[154]1243        bool save = dontcheckchanges;
1244        dontcheckchanges = 1;
[278]1245        ParamInterface::setDefault(i);
[154]1246        dontcheckchanges = save;
[109]1247}
1248
[154]1249// Returns the address of the beginning of the line.
1250// len = line length (without \n).
1251// 0 may mean the line with length=0 or the end of the SString.
1252// poz is advanced to the beginning of the next line.
1253// A typical loop: for(poz=0;poz<s.d;) {line=getline(s,poz,len);...
1254static const char *getline(const SString &s, int &poz, int &len)
[109]1255{
[348]1256        const char *beg = s.c_str() + poz;
[973]1257        if (poz >= s.length()) { poz = s.length(); len = 0; return s.c_str() + s.length(); }
[154]1258        const char *lf = strchr(beg, '\n');
[973]1259        if (!lf) { lf = s.c_str() + s.length() - 1; poz = s.length(); }
1260        else { poz = (int)(lf - s.c_str()) + 1; if (poz > s.length()) poz = s.length(); }
[154]1261        while (lf >= beg) if ((*lf == '\n') || (*lf == '\r')) lf--; else break;
[247]1262        len = (int)(lf - beg) + 1;
[154]1263        return beg;
[109]1264}
1265
[720]1266int ParamInterface::loadSingleLine(const SString &s, LoadOptions &options)
[109]1267{
[154]1268        int i; // the index number of the parameter
1269        int tmpi;
1270        int len;
1271        int ret;
1272        int fields_loaded = 0;
1273        const char *t, *lin, *end;
[320]1274        const char *equals_sign, *field_end, *next_field;
[154]1275        char remember;
1276        const char *quote, *quote2;
1277        const char *value, *valstop;
1278        SString tmpvalue;
[650]1279        bool parse_failed = false;
[973]1280        if (options.offset >= s.length()) return fields_loaded;
[720]1281        t = s.c_str() + options.offset;
[109]1282
[720]1283        lin = getline(s, options.offset, len); // all fields must be encoded in a single line
[154]1284        if (!len) return fields_loaded; // empty line = end
1285        i = 0;
1286        end = lin + len;
1287        while (t < end)
1288        {
1289                // processing a single field
[320]1290                // "p:name=field_value,  field_name=field_value  , name=value..."
1291                //                     ^ ^-t (after)           ^ ^_next_field
1292                //                     \_t (before)            \_field_end
[325]1293                while (isspace(*t)) if (t < end) t++; else return fields_loaded;
[109]1294
[320]1295                field_end = strchrlimit(t, ',', end); if (!field_end) field_end = end;
[333]1296                next_field = field_end;
[822]1297                while ((field_end > t) && isblank(field_end[-1])) field_end--;
[320]1298                quote = strchrlimit(t, '\"', field_end);
[154]1299                if (quote)
[109]1300                {
[154]1301                        quote2 = skipQuoteString(quote + 1, end);
[320]1302                        if (quote2 > field_end)
[154]1303                        {
[320]1304                                field_end = strchrlimit(quote2 + 1, ',', end);
[393]1305                                if (!field_end) field_end = end;
1306                                next_field = field_end;
[154]1307                        }
1308                        equals_sign = strchrlimit(t, '=', quote);
[109]1309                }
[154]1310                else
1311                {
[320]1312                        equals_sign = strchrlimit(t, '=', field_end);
[154]1313                        quote2 = 0;
1314                }
1315                if (equals_sign == t) { t++; equals_sign = 0; }
[320]1316                if (field_end == t)     // skip empty value
[154]1317                {
[973]1318                        t++; if (i >= 0) i++;
[154]1319                        continue;
1320                }
1321                if (equals_sign) // have parameter name
1322                {
[247]1323                        tmpi = findIdn(t, (int)(equals_sign - t));
[154]1324                        i = tmpi;
1325                        if (tmpi < 0)
[312]1326                        {
1327                                SString name(t, (int)(equals_sign - t));
[796]1328                                logPrintf("Param", "loadSingleLine", LOG_WARN, "Unknown property '%s.%s' (ignored)", getName(), name.c_str());
[312]1329                        }
[154]1330                        t = equals_sign + 1; // t=value
1331                }
[109]1332#ifdef WARN_MISSING_NAME
[914]1333                else // no parameter name
[973]1334                {
[109]1335#ifdef SAVE_SELECTED_NAMES
[914]1336                        if ((i < 0) // field after unknown field
[973]1337                                || (i >= getPropCount()) // field after last field
1338                                || !(flags(i) & PARAM_CANOMITNAME)) // valid field but it can't be skipped
[109]1339#endif
[154]1340                        {
[914]1341                                if (i < getPropCount())
1342                                        logPrintf("Param", "loadSingleLine", LOG_WARN, "Missing property name in '%s'", getName());
1343                                else // 'i' can go past PropCount because of moving to subsequent properties by i++, id(i) is then NULL
[822]1344                                        logPrintf("Param", "loadSingleLine", LOG_WARN, "Value after the last property of '%s'", getName());
[154]1345                        }
[973]1346                }
[914]1347                //else skipping a skippable field
[109]1348#endif
[914]1349                if ((i >= 0) && id(i)) // shared by name and skippped name cases
[109]1350                {
[154]1351                        value = t;
1352                        if (quote)
1353                        {
[247]1354                                tmpvalue.copyFrom(quote + 1, (int)(quote2 - quote) - 1);
[154]1355                                sstringUnquote(tmpvalue);
[348]1356                                value = tmpvalue.c_str();
[154]1357                                valstop = quote2;
1358                        }
1359                        else
[320]1360                                if (field_end < end) valstop = field_end; else valstop = end;
[154]1361
1362                        remember = *valstop;
1363                        *(char*)valstop = 0;
[743]1364                        ret = setFromString(i, value, true);
[154]1365                        fields_loaded++;
[973]1366                        if (ret & PSET_PARSEFAILED)
[650]1367                                parse_failed = true;
[154]1368                        *(char*)valstop = remember;
[109]1369                }
1370
[154]1371                if (i >= 0) i++;
[109]1372#ifdef __CODEGUARD__
[732]1373                if (next_field < end - 1) t = next_field + 1; else return fields_loaded;
[109]1374#else
[320]1375                t = next_field + 1;
[109]1376#endif
[154]1377        }
[720]1378        if (parse_failed) options.parse_failed = true;
1379        return fields_loaded;
[109]1380}
1381
[154]1382int Param::grmember(int g, int a)
[109]1383{
[154]1384        if ((getGroupCount() < 2) && (!g))
1385                return (a < getPropCount()) ? a : -9999;
[109]1386
[154]1387        ParamEntry *e = entry(0);
1388        int x = 0, i = 0;
1389        for (; e->id; i++, e++)
[109]1390        {
[154]1391                if (e->group == g)
1392                        if (a == x) return i; else x++;
[109]1393        }
[154]1394        return -9999;
[109]1395}
Note: See TracBrowser for help on using the repository browser.