Changeset 326 for cpp/frams/util


Ignore:
Timestamp:
02/06/15 00:15:08 (9 years ago)
Author:
Maciej Komosinski
Message:

Unified parsing of ints and floats; hex notation

Location:
cpp/frams/util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/util/extvalue.cpp

    r325 r326  
    610610}
    611611
    612 bool ExtValue::parseInt(const char* s, paInt &result, bool strict)
     612bool ExtValue::parseInt(const char* s, paInt &result, bool strict, bool error)
    613613{
    614614        ExtValue tmp;
    615615        const char* after = tmp.parseNumber(s, strict ? TInt : TUnknown);
    616         if (after == NULL) return false;
    617         if (after[0] != 0) return false;
     616        if ((after == NULL) || (after[0] != 0))
     617                {
     618                if (error)
     619                        FMprintf("ExtValue", "parseInt", FMLV_ERROR, "Could not parse '%s'%s", s, strict?" (strict)":"");
     620                return false;
     621                }
    618622        result = tmp.getInt();
    619623        return true;
    620624}
    621625
    622 bool ExtValue::parseDouble(const char* s, double &result)
     626bool ExtValue::parseDouble(const char* s, double &result, bool error)
    623627{
    624628        ExtValue tmp;
    625629        const char* after = tmp.parseNumber(s, TDouble);
    626         if (after == NULL) return false;
    627         if (after[0] != 0) return false;
     630        if ((after == NULL) || (after[0] != 0))
     631                {
     632                if (error)
     633                        FMprintf("ExtValue", "parseDouble", FMLV_ERROR, "Could not parse '%s'", s);
     634                return false;
     635                }
    628636        result = tmp.getDouble();
    629637        return true;
     
    633641{
    634642        paInt result;
    635         if (parseInt(s, result, strict))
     643        if (parseInt(s, result, strict, true))
    636644                return result;
    637         FMprintf("ExtValue", "getInt", FMLV_ERROR, "Could not parse '%s'%s", s, strict?" (strict)":"");
    638645        return 0;
    639646}
     
    642649{
    643650        double result;
    644         if (parseDouble(s, result))
     651        if (parseDouble(s, result, true))
    645652                return result;
    646         FMprintf("ExtValue", "getDouble", FMLV_ERROR, "Could not parse '%s'", s);
    647653        return 0;
    648654}
     
    729735        if (in[0] == 0) return NULL;
    730736        while (isspace(*in)) in++;
    731         bool minus = false;
    732         if (((in[0] == '0') && (in[1] == 'x'))
    733                 || (((minus = (in[0] == '-')) && (in[1] == '0') && (in[2] == 'x'))))
    734         {
    735                 in += minus ? 3 : 2;
     737        bool minus = (in[0] == '-');
     738        bool plus = (in[0] == '+');
     739        if (((in[0] == '0') && ((in[1] == 'x') || (in[1]=='X')))
     740             || (((minus || plus) && (in[1] == '0') && ((in[2] == 'x') || (in[2] == 'X')))))
     741        {
     742                in += (minus || plus) ? 3 : 2;
    736743                if (isspace(*in)) return NULL;
    737744                errno = 0;
  • cpp/frams/util/extvalue.h

    r325 r326  
    160160        void setString(const SString &v) { if (type != TString) setrs(v); else sdata() = v; }
    161161        void setObject(const ExtObject &src) { if (type != TObj) setro(src); else odata() = src; }
    162         static bool parseInt(const char* s, paInt &result, bool strict = false);
    163         static bool parseDouble(const char* s, double &result);
     162        static bool parseInt(const char* s, paInt &result, bool strict, bool error);
     163        static bool parseDouble(const char* s, double &result, bool error);
    164164        static paInt getInt(const char* s, bool strict = false);//< @param strict=true will fail on floating point
    165165        static double getDouble(const char* s);
Note: See TracChangeset for help on using the changeset viewer.