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

Last change on this file since 1155 was 1155, checked in by Maciej Komosinski, 3 years ago

Added ParamInterface::findGroupId(const char* name)

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