Ignore:
Timestamp:
01/14/18 11:24:22 (6 years ago)
Author:
Maciej Komosinski
Message:

Param::save2() renamed to saveSingleLine(); unified Param::load() so that it gets a single-line/multi-line format selector

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/param/param.cpp

    r704 r720  
    1010#include "common/log.h"
    1111#include <frams/util/sstringutils.h>
     12#include <common/virtfile/stringfile.h>
    1213
    1314//#define SAVE_ALL_NAMES
     
    356357}
    357358
    358 void SimpleAbstractParam::save2(SString& f, void *defdata, bool addcr, bool all_names)
     359void SimpleAbstractParam::saveSingleLine(SString& f, void *defdata, bool addcr, bool all_names)
    359360{ // defdata!=NULL -> does not save default values
    360361        const char *p;
     
    421422}
    422423
    423 int ParamInterface::load(VirtFILE* f, bool warn_unknown_fields, bool *abortable, int *linenum)
     424int ParamInterface::load(FileFormat format, VirtFILE* f, LoadOptions *options)
     425{
     426        LoadOptions default_options;
     427        if (options == NULL)
     428                options = &default_options;
     429        switch (format)
     430        {
     431        case FormatMultiLine:
     432                return loadMultiLine(f, *options);
     433
     434        case FormatSingleLine:
     435        {
     436                StringFILE *sf = dynamic_cast<StringFILE*>(f);
     437                SString s;
     438                if (sf)
     439                {
     440                        s = sf->getString().c_str();
     441                        options->offset += sf->Vtell();
     442                }
     443                else
     444                {
     445                        if (!loadSStringLine(f, s))
     446                                return -1;
     447                }
     448                return loadSingleLine(s, *options);
     449        }
     450        }
     451        return -1;
     452}
     453
     454int ParamInterface::load(FileFormat format, const SString &s, LoadOptions *options)
     455{
     456        LoadOptions default_options;
     457        if (options == NULL)
     458                options = &default_options;
     459        switch (format)
     460        {
     461        case FormatMultiLine:
     462        {
     463                string std_string(s.c_str());
     464                StringFILE f(std_string);
     465                return loadMultiLine(&f, *options);
     466        }
     467
     468        case FormatSingleLine:
     469                return loadSingleLine(s, *options);
     470        }
     471        return -1;
     472}
     473
     474int ParamInterface::loadMultiLine(VirtFILE* f, LoadOptions &options)
    424475{
    425476        SString buf;
     
    434485        if ((i = findId("beforeLoad")) >= 0)
    435486                call(i, NULL, NULL);
    436         while (((!abortable) || (!*abortable)) && loadSStringLine(f, buf))
    437         {
    438                 if (linenum) (*linenum)++;
     487        while (((!options.abortable) || (!*options.abortable)) && loadSStringLine(f, buf))
     488        {
     489                if (options.linenum) (*options.linenum)++;
    439490                const char* t = buf.c_str();
    440491                p0 = t; while (isblank(*p0)) p0++;
     
    448499                        case 0:
    449500                                logPrintf("ParamInterface", "load", LOG_WARN, "Ignored unexpected line %s while reading object '%s'",
    450                                         linenum ?
    451                                         SString::sprintf("%d", *linenum).c_str()
     501                                        options.linenum ?
     502                                        SString::sprintf("%d", *options.linenum).c_str()
    452503                                        : SString::sprintf("'%s'", p0).c_str(),
    453504                                        getName());
     
    472523                                {
    473524                                        fileinfo = SString::sprintf(" while reading from '%s'", fname);
    474                                         if (linenum)
    475                                                 fileinfo += SString::sprintf(" (line %d)", *linenum);
     525                                        if (options.linenum)
     526                                                fileinfo += SString::sprintf(" (line %d)", *options.linenum);
    476527                                }
    477528                                logPrintf("ParamInterface", "load", LOG_WARN, "Multiple '%s.%s' fields found%s", getName(), id(i), fileinfo.c_str());
     
    496547                                        int ch; while ((ch = f->Vgetc()) != EOF) if (ch == '\n') break;
    497548                                        unquoteTilde(s);
    498                                         if (linenum && (flags(i)&PARAM_LINECOMMENT))
    499                                                 s = SString::sprintf("@file %s\n@line %d\n", f->VgetPath(), *linenum + 1) + s;
     549                                        if (options.linenum && (flags(i)&PARAM_LINECOMMENT))
     550                                                s = SString::sprintf("@file %s\n@line %d\n", f->VgetPath(), *options.linenum + 1) + s;
    500551                                        set(i, s.c_str());
    501                                         if (linenum)
    502                                                 (*linenum) += lfcount;
     552                                        if (options.linenum)
     553                                                (*options.linenum) += lfcount;
    503554                                }
    504555                                else
     
    510561                        }
    511562                }
    512                 else if (warn_unknown_fields)
     563                else if (options.warn_unknown_fields)
    513564                {
    514565                        SString name(p0, p_len);
     
    521572                        if (!readUntilTilde(f, s))
    522573                                closingTildeError(this, f, -1);
    523                         if (linenum)
     574                        if (options.linenum)
    524575                        {
    525576                                const char* tmp = s.c_str();
     
    530581                                        lfcount++; tmp++;
    531582                                        }
    532                                 (*linenum) += lfcount;
     583                                (*options.linenum) += lfcount;
    533584                        }
    534585                        int ch; while ((ch = f->Vgetc()) != EOF) if (ch == '\n') break;
     
    705756{
    706757        const char *t;
    707         if (((*(t = type(i))) == 'd') && (strchr(t, '~')!=NULL)) //type is int and contains enum labels
     758        if (((*(t = type(i))) == 'd') && (strchr(t, '~') != NULL)) //type is int and contains enum labels
    708759        {
    709760                paInt mn, mx, def;
     
    11311182}
    11321183
    1133 int ParamInterface::load2(const SString &s, int &poz)
     1184int ParamInterface::loadSingleLine(const SString &s, LoadOptions &options)
    11341185{
    11351186        int i; // the index number of the parameter
     
    11451196        SString tmpvalue;
    11461197        bool parse_failed = false;
    1147         if (poz >= s.len()) return fields_loaded;
    1148         t = s.c_str() + poz;
    1149 
    1150         lin = getline(s, poz, len); // all fields must be encoded in a single line
     1198        if (options.offset >= s.len()) return fields_loaded;
     1199        t = s.c_str() + options.offset;
     1200
     1201        lin = getline(s, options.offset, len); // all fields must be encoded in a single line
    11511202        if (!len) return fields_loaded; // empty line = end
    11521203        i = 0;
     
    12411292#endif
    12421293        }
    1243         return fields_loaded | (parse_failed ? LOAD2_PARSE_FAILED : 0);
     1294        if (parse_failed) options.parse_failed = true;
     1295        return fields_loaded;
    12441296}
    12451297
Note: See TracChangeset for help on using the changeset viewer.