Changeset 312 for cpp/frams


Ignore:
Timestamp:
01/26/15 09:18:39 (9 years ago)
Author:
Maciej Komosinski
Message:

Formatted source

File:
1 edited

Legend:

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

    r310 r312  
    5252                        && (*type(i) != 'p'))
    5353                {
    54                         j = src->findId(id(i));
    55                         if (j < 0) continue;
    56                         src->get(j, v);
    57                         set(i, v);
     54                j = src->findId(id(i));
     55                if (j < 0) continue;
     56                src->get(j, v);
     57                set(i, v);
    5858                }
    5959}
     
    6767                        && (*type(i) != 'p'))
    6868                {
    69                         src->get(i, v);
    70                         set(i, v);
     69                src->get(i, v);
     70                set(i, v);
    7171                }
    7272}
     
    9090        const char* t = type(prop) + 1;
    9191        while (*t) if (*t == ' ') break; else t++;
    92         int ret=sscanf(t, "%d %d", &minumum, &maximum);
    93         def=SString::empty();
    94         if (ret==2)
    95                 {
    96                 while (*t==' ') t++;
    97                 for(int skip_fields=2;skip_fields>0;skip_fields--)
    98                         {
     92        int ret = sscanf(t, "%d %d", &minumum, &maximum);
     93        def = SString::empty();
     94        if (ret == 2)
     95        {
     96                while (*t == ' ') t++;
     97                for (int skip_fields = 2; skip_fields > 0; skip_fields--)
     98                {
    9999                        while (*t) if (*t == ' ') break; else t++;
    100                         while (*t==' ') t++;
    101                         }
     100                        while (*t == ' ') t++;
     101                }
    102102                if (*t)
    103                         {
    104                         const char* end=strchr(t,'~');
     103                {
     104                        const char* end = strchr(t, '~');
    105105                        if (!end)
    106                                 end=t+strlen(t);
    107                         while ((end>t)&&(end[-1]==' ')) end--;
    108                         def=SString(t,end-t);
    109                         }
     106                                end = t + strlen(t);
     107                        while ((end > t) && (end[-1] == ' ')) end--;
     108                        def = SString(t, end - t);
     109                }
    110110                return 3;
    111                 }
     111        }
    112112        else
    113113                return ret;
     
    151151        }
    152152                break;
    153         case 's': case 'x':
    154         {
    155                 int a,b; SString c;
    156                 getMinMax(i,a,b,c);
    157                 if (*t=='s')
    158                         setString(i,c);
     153        case 's': case 'x':
     154        {
     155                int a, b; SString c;
     156                getMinMax(i, a, b, c);
     157                if (*t == 's')
     158                        setString(i, c);
    159159                else
    160                         { if (c.len()>0) setExtValue(i,ExtValue(c)); else setExtValue(i,ExtValue::empty()); }
    161         }
     160                {
     161                        if (c.len() > 0) setExtValue(i, ExtValue(c)); else setExtValue(i, ExtValue::empty());
     162                }
     163        }
    162164                break;
    163165        case 'o':
    164                 setObject(i,ExtObject::empty());
     166                setObject(i, ExtObject::empty());
    165167                break;
    166168        }
     
    214216
    215217SString ParamInterface::getStringById(const char*prop)
    216 {int i=findId(prop); if (i>=0) return getString(i); else return SString();}
     218{
     219        int i = findId(prop); if (i >= 0) return getString(i); else return SString();
     220}
    217221paInt ParamInterface::getIntById(const char*prop)
    218 {int i=findId(prop); if (i>=0) return getInt(i); else return 0;}
     222{
     223        int i = findId(prop); if (i >= 0) return getInt(i); else return 0;
     224}
    219225double ParamInterface::getDoubleById(const char*prop)
    220 {int i=findId(prop); if (i>=0) return getDouble(i); else return 0;}
     226{
     227        int i = findId(prop); if (i >= 0) return getDouble(i); else return 0;
     228}
    221229ExtObject ParamInterface::getObjectById(const char*prop)
    222 {int i=findId(prop); if (i>=0) return getObject(i); else return ExtObject();}
     230{
     231        int i = findId(prop); if (i >= 0) return getObject(i); else return ExtObject();
     232}
    223233ExtValue ParamInterface::getExtValueById(const char*prop)
    224 {int i=findId(prop); if (i>=0) return getExtValue(i); else return ExtValue();}
    225 
    226 int ParamInterface::setIntById(const char* prop,paInt v)
    227 {int i=findId(prop); if (i>=0) return setInt(i,v); else return PSET_NOPROPERTY;}
    228 int ParamInterface::setDoubleById(const char* prop,double v)
    229 {int i=findId(prop); if (i>=0) return setDouble(i,v); else return PSET_NOPROPERTY;}
    230 int ParamInterface::setStringById(const char* prop,const SString &v)
    231 {int i=findId(prop); if (i>=0) return setString(i,v); else return PSET_NOPROPERTY;}
    232 int ParamInterface::setObjectById(const char* prop,const ExtObject &v)
    233 {int i=findId(prop); if (i>=0) return setObject(i,v); else return PSET_NOPROPERTY;}
    234 int ParamInterface::setExtValueById(const char* prop,const ExtValue &v)
    235 {int i=findId(prop); if (i>=0) return setExtValue(i,v); else return PSET_NOPROPERTY;}
    236 int ParamInterface::setById(const char* prop,const ExtValue &v)
    237 {int i=findId(prop); if (i>=0) return set(i,v); else return PSET_NOPROPERTY;}
     234{
     235        int i = findId(prop); if (i >= 0) return getExtValue(i); else return ExtValue();
     236}
     237
     238int ParamInterface::setIntById(const char* prop, paInt v)
     239{
     240        int i = findId(prop); if (i >= 0) return setInt(i, v); else return PSET_NOPROPERTY;
     241}
     242int ParamInterface::setDoubleById(const char* prop, double v)
     243{
     244        int i = findId(prop); if (i >= 0) return setDouble(i, v); else return PSET_NOPROPERTY;
     245}
     246int ParamInterface::setStringById(const char* prop, const SString &v)
     247{
     248        int i = findId(prop); if (i >= 0) return setString(i, v); else return PSET_NOPROPERTY;
     249}
     250int ParamInterface::setObjectById(const char* prop, const ExtObject &v)
     251{
     252        int i = findId(prop); if (i >= 0) return setObject(i, v); else return PSET_NOPROPERTY;
     253}
     254int ParamInterface::setExtValueById(const char* prop, const ExtValue &v)
     255{
     256        int i = findId(prop); if (i >= 0) return setExtValue(i, v); else return PSET_NOPROPERTY;
     257}
     258int ParamInterface::setById(const char* prop, const ExtValue &v)
     259{
     260        int i = findId(prop); if (i >= 0) return set(i, v); else return PSET_NOPROPERTY;
     261}
    238262
    239263int ParamInterface::save(VirtFILE* f, const char* altname, bool force)
     
    270294        err |= (fputs(p, f) == EOF); fputc(':', f);
    271295        cr = 0;
    272         if ((*typ == 'x')||(*typ == 'o'))
     296        if ((*typ == 'x') || (*typ == 'o'))
    273297        {
    274298                ExtValue ex;
     
    332356                if (!((fl = flags(i))&PARAM_DONTSAVE))
    333357                {
    334                         if (defdata && isequal(i, defdata))
    335                                 needlabel = 1;
    336                         else
    337                         {
    338                                 if (!first) t += ", ";
     358                if (defdata && isequal(i, defdata))
     359                        needlabel = 1;
     360                else
     361                {
     362                        if (!first) t += ", ";
    339363#ifndef SAVE_ALL_NAMES
    340364#ifdef SAVE_SELECTED_NAMES
    341                                 if (needlabel || all_names || !(fl & PARAM_CANOMITNAME))
     365                        if (needlabel || all_names || !(fl & PARAM_CANOMITNAME))
    342366#else
    343                                 if (needlabel)
     367                        if (needlabel)
    344368#endif
    345369#endif
     370                        {
     371                                t += p; t += "="; needlabel = 0;
     372                        }
     373                        if (type(i)[0] == 's')
     374                        { // string - special case
     375                                SString str = getString(i);
     376                                if (strContainsOneOf(str, ", \\\n\r\t\""))
    346377                                {
    347                                         t += p; t += "="; needlabel = 0;
    348                                 }
    349                                 if (type(i)[0] == 's')
    350                                 { // string - special case
    351                                         SString str = getString(i);
    352                                         if (strContainsOneOf(str, ", \\\n\r\t\""))
    353                                         {
    354                                                 t += "\"";
    355                                                 sstringQuote(str);
    356                                                 t += str;
    357                                                 t += "\"";
    358                                         }
    359                                         else
    360                                                 t += str;
     378                                        t += "\"";
     379                                        sstringQuote(str);
     380                                        t += str;
     381                                        t += "\"";
    361382                                }
    362383                                else
    363                                         t += get(i);
    364                                 first = 0;
     384                                        t += str;
    365385                        }
     386                        else
     387                                t += get(i);
     388                        first = 0;
     389                }
    366390                }
    367391        if (addcr)
     
    370394}
    371395
    372 int ParamInterface::load(VirtFILE* f,bool warn_unknown_fields,bool *abortable)
     396int ParamInterface::load(VirtFILE* f, bool warn_unknown_fields, bool *abortable)
    373397{
    374398        SString buf;
     
    378402        bool loaded;
    379403        int fields_loaded = 0;
    380         while ( ((!abortable)||(!*abortable)) && loadSStringLine(f, buf) )
     404        while (((!abortable) || (!*abortable)) && loadSStringLine(f, buf))
    381405        {
    382406                const char* t = (const char*)buf;
    383407                p0 = t; while ((*p0 == ' ') || (*p0 == '\t')) p0++;
    384408                if (!*p0) break;
    385                 if (p0[0]=='#') continue;
     409                if (p0[0] == '#') continue;
    386410                p = strchr(p0, ':'); if (!p) continue;
    387411                p_len = (int)(p - p0);
     
    389413                if (p_len && ((i = findIdn(p0, p_len)) >= 0))
    390414                {
    391                 if (!(flags(i)&PARAM_DONTLOAD))
    392                    {
    393                         if (p0[p_len + 1] == '~')
     415                        if (!(flags(i)&PARAM_DONTLOAD))
    394416                        {
    395                                 SString s;
    396                                 czytdotyldy(f, s);
    397                                 removeCR(s);
    398                                 int ch; while ((ch = fgetc(f)) != EOF) if (ch == '\n') break;
    399                                 unquoteTilde(s);
    400                                 set(i, (const char*)s);
     417                                if (p0[p_len + 1] == '~')
     418                                {
     419                                        SString s;
     420                                        czytdotyldy(f, s);
     421                                        removeCR(s);
     422                                        int ch; while ((ch = fgetc(f)) != EOF) if (ch == '\n') break;
     423                                        unquoteTilde(s);
     424                                        set(i, (const char*)s);
     425                                }
     426                                else
     427                                {
     428                                        set(i, p0 + p_len + 1);
     429                                }
     430                                fields_loaded++;
     431                                loaded = true;
    401432                        }
    402                         else
    403                         {
    404                                 set(i, p0 + p_len + 1);
    405                         }
    406                         fields_loaded++;
    407                         loaded = true;
    408                    }
    409433                }
    410434                else if (warn_unknown_fields)
    411                         {
    412                         SString name(p0,p_len);
    413                         FMprintf("ParamInterface","load",FMLV_WARN,"Unknown property '%s' while reading object '%s' (ignored)",(const char*)name,getName());
    414                         }
     435                {
     436                        SString name(p0, p_len);
     437                        FMprintf("ParamInterface", "load", FMLV_WARN, "Unknown property '%s' while reading object '%s' (ignored)", (const char*)name, getName());
     438                }
    415439
    416440                if ((!loaded) && (p0[p_len + 1] == '~'))
     
    430454char *t;
    431455switch (*(t=type(i)))
    432         {
     456{
    433457        case 'd':
    434458        {
    435         for (i=atol(get(i));i>=0;i--) if (t) t=strchr(t+1,'~');
    436         if (t)
    437                 {
    438                 t++;
    439                 char *t2=strchr(t,'~');
    440                 if (!t2) t2=t+strlen(t);
    441                 SString str;
    442                 strncpy(str.directWrite(t2-t),t,t2-t);
    443                 str.endWrite(t2-t);
    444                 return str;
    445                 }
    446         }
    447         }
     459                for (i=atol(get(i));i>=0;i--) if (t) t=strchr(t+1,'~');
     460                if (t)
     461                {
     462                        t++;
     463                        char *t2=strchr(t,'~');
     464                        if (!t2) t2=t+strlen(t);
     465                        SString str;
     466                        strncpy(str.directWrite(t2-t),t,t2-t);
     467                        str.endWrite(t2-t);
     468                        return str;
     469                }
     470        }
     471}
    448472return get(i);
    449473}
     
    543567int ParamInterface::set(int i, const char *v)
    544568{
    545         char typ=type(i)[0];
     569        char typ = type(i)[0];
    546570        switch (typ)
    547571        {
     
    567591                        e.setString(SString(v));
    568592                }
    569                 if (typ=='x')
     593                if (typ == 'x')
    570594                        return setExtValue(i, e);
    571595                else
     
    581605        if ((*(t = type(i))) == 'd')
    582606        {
    583                 paInt a,b,c;
    584                 int value=getInt(i);
    585                 if (getMinMax(i,a,b,c)>=2)
    586                         {
    587                         if (value>b)
     607                paInt a, b, c;
     608                int value = getInt(i);
     609                if (getMinMax(i, a, b, c) >= 2)
     610                {
     611                        if (value > b)
    588612                                return get(i);
    589                         value-=a;
    590                         }
    591                 if (value<0) return get(i);
     613                        value -= a;
     614                }
     615                if (value < 0) return get(i);
    592616                for (; value >= 0; value--) if (t) t = strchr(t + 1, '~');
    593617                if (t)
     
    621645void SimpleAbstractParam::sanityCheck(int i)
    622646{
    623 ParamEntry *pe=entry(i);
    624 
    625 const char* t=pe->type;
    626 const char* err=NULL;
    627 
    628 if (*t=='p')
    629         {
    630         if (pe->fun1==NULL)
    631                 err="no procedure defined";
    632         }
    633 else
    634         {
    635         if (!(pe->flags & PARAM_READONLY))
     647        ParamEntry *pe=entry(i);
     648
     649        const char* t=pe->type;
     650        const char* err=NULL;
     651
     652        if (*t=='p')
     653        {
     654                if (pe->fun1==NULL)
     655                        err="no procedure defined";
     656        }
     657        else
     658        {
     659                if (!(pe->flags & PARAM_READONLY))
    636660                { //write access
    637                 if ((pe->fun2==NULL)&&(pe->offset==PARAM_ILLEGAL_OFFSET))
    638                         err="no field defined (GETONLY without PARAM_READONLY?)";
    639                 }
    640         }
    641 if (err!=NULL)
    642         FMprintf("SimpleAbstractParam","sanityCheck", FMLV_ERROR,
    643                  "Invalid ParamEntry for %s.%s (%s)", getName(), pe->id, err);
     661                        if ((pe->fun2==NULL)&&(pe->offset==PARAM_ILLEGAL_OFFSET))
     662                                err="no field defined (GETONLY without PARAM_READONLY?)";
     663                }
     664        }
     665        if (err!=NULL)
     666                FMprintf("SimpleAbstractParam","sanityCheck", FMLV_ERROR,
     667                "Invalid ParamEntry for %s.%s (%s)", getName(), pe->id, err);
    644668}       
    645669#endif
     
    761785                if (a <= b) // if max<min then the min/max constraint check is not supported
    762786                {
    763                         if (x<a) { x = a; result = PSET_HITMIN; }
    764                         else if (x>b) { x = b; result = PSET_HITMAX; }
     787                if (x < a) { x = a; result = PSET_HITMIN; }
     788                else if (x > b) { x = b; result = PSET_HITMAX; }
    765789                }
    766790
     
    797821                if (a <= b) // if max<min then the min/max constraint check is not supported
    798822                {
    799                         if (x<a) { x = a; result = PSET_HITMIN; }
    800                         else if (x>b) { x = b; result = PSET_HITMAX; }
     823                if (x < a) { x = a; result = PSET_HITMIN; }
     824                else if (x > b) { x = b; result = PSET_HITMAX; }
    801825                }
    802826
     
    973997        {
    974998                // processing a single field
    975                 while (strchr(" \n\r\t", *t)) if (t<end) t++; else return fields_loaded;
     999                while (strchr(" \n\r\t", *t)) if (t < end) t++; else return fields_loaded;
    9761000
    9771001                comma_sign = strchrlimit(t, ',', end); if (!comma_sign) comma_sign = end;
     
    9801004                {
    9811005                        quote2 = skipQuoteString(quote + 1, end);
    982                         if (quote2>comma_sign)
     1006                        if (quote2 > comma_sign)
    9831007                        {
    9841008                                comma_sign = strchrlimit(quote2 + 1, ',', end);
     
    10031027                        i = tmpi;
    10041028                        if (tmpi < 0)
    1005                                 {
    1006                                 SString name(t,(int)(equals_sign - t));
    1007                                 FMprintf("Param", "load2", FMLV_WARN, "Unknown property '%s' while reading object '%s' (ignored)",(const char*)name,getName());
    1008                                 }
     1029                        {
     1030                                SString name(t, (int)(equals_sign - t));
     1031                                FMprintf("Param", "load2", FMLV_WARN, "Unknown property '%s' while reading object '%s' (ignored)", (const char*)name, getName());
     1032                        }
    10091033                        t = equals_sign + 1; // t=value
    10101034                }
     
    10151039#endif
    10161040                        {
    1017                                 FMprintf("Param", "load2", FMLV_WARN, "Missing property name in '%s' (assuming '%s')",
    1018                                         getName(), id(i) ? id(i) : "unknown property?");
     1041                        FMprintf("Param", "load2", FMLV_WARN, "Missing property name in '%s' (assuming '%s')",
     1042                                getName(), id(i) ? id(i) : "unknown property?");
    10191043                        }
    10201044#endif
Note: See TracChangeset for help on using the changeset viewer.