Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
Diff: ParsedValue.cpp
- Revision:
- 7:8159a2d12e4e
- Parent:
- 0:099f76422485
- Child:
- 11:e1bee9a77652
diff -r cd7ba1ddb664 -r 8159a2d12e4e ParsedValue.cpp --- a/ParsedValue.cpp Mon Jul 28 12:11:02 2014 +0200 +++ b/ParsedValue.cpp Wed Oct 22 16:17:22 2014 +0200 @@ -31,13 +31,11 @@ #include <string.h> #include <ctype.h> #include <errno.h> -#include <math.h> -#include "NullValue.h" -#include "IntegerValue.h" -#include "FloatValue.h" -#include "CharValue.h" -ParsedValue::ParsedValue(const char *str, bool copy) : _value(str, copy), _float(0.0), _digits(0), _zflag(false), _integer(0l) +ParsedValue::ParsedValue(const char *str, bool copy) : + _value(str, copy), + _float(0.0), + _integer(0l) { if (_value.valueType() != VALUE_NULL) { _type = VALUE_CHARACTER; @@ -81,80 +79,28 @@ Value* ParsedValue::copy() const { - if (_type == VALUE_NULL) - return new NullValue(); - if (_type == VALUE_INTEGER) - return new IntegerValue(_integer); - if (_type == VALUE_FLOAT) - return new FloatValue(_float, _digits, _zflag); - return new CharValue(_value.characterValue(), true); + return new ParsedValue(_value.characterValue(), true); } void ParsedValue::extractValue() { - const char *str; char *ptr, c; - bool floating = false, zflag = false, negative = false; - uint8_t digits = 0; + const char *str; char *ptr; str = _value.characterValue(); - - if (isspace(*str)) + if ((*str == '\0') || (isspace(*str))) return; - // easy check for nan - if (strcmp(str, "nan") == 0) { - _float = NAN; - _type = VALUE_FLOAT; - return; - } - - if (*str == '-') { - negative = true; - str++; - } - - if (strcmp(str, "inf") == 0) { - _float = (negative) ? -INFINITY : INFINITY; - _type = VALUE_FLOAT; + errno = 0; + if ((((_integer = strtol(str, &ptr, 10)) != 0) || (errno == 0)) && + (*ptr == '\0')) { + _type = VALUE_INTEGER; return; } - for (ptr = (char*)str; (c = *ptr) != 0; ptr++) { - if (floating) { - if (!isdigit(c)) - return; - digits++; - } else { - if (c == '.') { - if (*(ptr+1) == '\0') - return; - floating = true; - } else if (isdigit(c)) { - _zflag = true; - } else { - return; - } - } - } - - if (floating) { - errno = 0; - if ((((_float = strtod(str, &ptr)) != 0.0) || (errno == 0)) && - (*ptr == '\0')) { - if (negative) - _float = -_float; - _zflag = zflag; - _digits = digits; - _type = VALUE_FLOAT; - } - } else { - errno = 0; - if ((((_integer = strtol(str, &ptr, 10)) != 0) || (errno == 0)) && - (*ptr == '\0')) { - if (negative) - _integer = -_integer; - _type = VALUE_INTEGER; - return; - } + errno = 0; + if ((((_float = strtod(str, &ptr)) != 0.0) || (errno == 0)) && + (*ptr == '\0')) { + _type = VALUE_FLOAT; + return; } }