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

Last change on this file since 1185 was 1184, checked in by Maciej Komosinski, 2 years ago

Introduced a class to quickly copy field values between two compatible Param's, and added a constructor that checks if the paramtab structure is correct

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