Changeset 574


Ignore:
Timestamp:
08/09/16 01:08:59 (8 years ago)
Author:
Maciej Komosinski
Message:

More strict validation of numeric type definitions: min without max is now invalid. More code is now reused.

Location:
cpp/frams/param
Files:
2 edited

Legend:

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

    r535 r574  
    7272int ParamInterface::getMinMax(int prop, paInt& minumum, paInt& maximum, paInt &def)
    7373{
    74         const char* t = type(prop) + 1;
     74        return getMinMax(type(prop),minumum,maximum,def);
     75}
     76
     77int ParamInterface::getMinMax(int prop, double& minumum, double& maximum, double& def)
     78{
     79        return getMinMax(type(prop),minumum,maximum,def);
     80}
     81
     82int ParamInterface::getMinMax(int prop, int& minumum, int& maximum, SString& def)
     83{
     84        return getMinMax(type(prop),minumum,maximum,def);
     85}
     86
     87int ParamInterface::getMinMax(const char* t, paInt& minumum, paInt& maximum, paInt &def)
     88{
    7589        while (*t) if (*t == ' ') break; else t++;
    7690        return sscanf(t, PA_INT_SCANF " " PA_INT_SCANF " " PA_INT_SCANF, &minumum, &maximum, &def);
    7791}
    7892
    79 int ParamInterface::getMinMax(int prop, double& minumum, double& maximum, double& def)
    80 {
    81         const char* t = type(prop) + 1;
     93int ParamInterface::getMinMax(const char* t, double& minumum, double& maximum, double& def)
     94{
    8295        while (*t) if (*t == ' ') break; else t++;
    8396        return sscanf(t, "%lg %lg %lg", &minumum, &maximum, &def);
    8497}
    8598
    86 int ParamInterface::getMinMax(int prop, int& minumum, int& maximum, SString& def)
    87 {
    88         const char* t = type(prop) + 1;
     99int ParamInterface::getMinMax(const char* t, int& minumum, int& maximum, SString& def)
     100{
    89101        while (*t) if (*t == ' ') break; else t++;
    90102        int ret = sscanf(t, "%d %d", &minumum, &maximum);
     
    138150        {
    139151                double mn = 0, mx = 0, def = 0;
    140                 if (getMinMax(i, mn, mx, def) < 3) def = mn;
     152                if (getMinMax(t, mn, mx, def) < 3) def = mn;
    141153                setDouble(i, def);
    142154        }
     
    145157        {
    146158                paInt mn = 0, mx = 0, def = 0;
    147                 if (getMinMax(i, mn, mx, def) < 3) def = mn;
     159                if (getMinMax(t, mn, mx, def) < 3) def = mn;
    148160                setInt(i, def);
    149161        }
     
    152164        {
    153165                int mn, mx; SString def;
    154                 getMinMax(i, mn, mx, def);
     166                getMinMax(t, mn, mx, def);
    155167                if (*t == 's')
    156168                        setString(i, def);
     
    175187        {
    176188                double mn = 0, mx = 0, def = 0;
    177                 getMinMax(i, mn, mx, def);
     189                getMinMax(t, mn, mx, def);
    178190                setDouble(i, mn);
    179191        }
     
    182194        {
    183195                paInt mn = 0, mx = 0, def = 0;
    184                 getMinMax(i, mn, mx, def);
     196                getMinMax(t, mn, mx, def);
    185197                setInt(i, mn);
    186198        }
     
    198210        {
    199211                double mn = 0, mx = 0, def = 0;
    200                 getMinMax(i, mn, mx, def);
     212                getMinMax(t, mn, mx, def);
    201213                setDouble(i, mx);
    202214        }
     
    205217        {
    206218                paInt mn = 0, mx = 0, def = 0;
    207                 getMinMax(i, mn, mx, def);
     219                getMinMax(t, mn, mx, def);
    208220                setInt(i, mx);
    209221        }
     
    678690                paInt mn, mx, def;
    679691                int value = getInt(i);
    680                 if (getMinMax(i, mn, mx, def) >= 2)
     692                if (getMinMax(t, mn, mx, def) >= 2)
    681693                {
    682694                        if (value > mx)
     
    715727if (*t==0) return false;
    716728if (strchr("dfsoxp", *t)==NULL) return false;
     729switch(*t)
     730        {
     731        case 'd':
     732                {paInt a,b,c; if (getMinMax(t,a,b,c)==1) return false;}
     733                break;
     734        case 'f':
     735                {double a,b,c; if (getMinMax(t,a,b,c)==1) return false;}
     736                break;
     737        }
    717738return true;
    718739}
     
    858879        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
    859880        paInt xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
    860         paInt mn = 0, mx = 0;
     881        paInt mn = 0, mx = 0, de = 0;
    861882        int result = 0;
    862         const char* t = pe->type + 1;
    863         while (*t) if (*t == ' ') break; else t++;
    864         if (sscanf(t, PA_INT_SCANF " " PA_INT_SCANF, &mn, &mx) == 2)
     883        if (getMinMax(pe->type,mn,mx,de)>=2)
    865884                if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking
    866885                {
     
    894913        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
    895914        double xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
    896         double mn = 0, mx = 0;
     915        double mn = 0, mx = 0, de = 0;
    897916        int result = 0;
    898         const char* t = pe->type + 1;
    899         while (*t) if (*t == ' ') break; else t++;
    900         if (sscanf(t, "%lg %lg", &mn, &mx) == 2)
     917        if (getMinMax(pe->type,mn,mx,de)>=2)
    901918                if (mn <= mx) // else if mn>mx then the min/max constraint makes no sense and there is no checking
    902919                {
  • cpp/frams/param/param.h

    r535 r574  
    144144        int getMinMax(int prop, double& minumum, double& maximum, double& def);
    145145        int getMinMax(int prop, int& minumum, int& maximum, SString& def);
     146        static int getMinMax(const char* type, paInt& minumum, paInt& maximum, paInt& def);
     147        static int getMinMax(const char* type, double& minumum, double& maximum, double& def);
     148        static int getMinMax(const char* type, int& minumum, int& maximum, SString& def);
    146149
    147150        virtual void setDefault();
Note: See TracChangeset for help on using the changeset viewer.