Generic SmartRest library
Dependents: SmartRestUnitTest MbedSmartRest MbedSmartRestStreaming
ParsedValue.cpp
00001 /* 00002 * ParsedValue.cpp 00003 * 00004 * Created on: Nov 1, 2013 00005 * * Authors: Vincent Wochnik <v.wochnik@gmail.com> 00006 * 00007 * Copyright (c) 2013 Cumulocity GmbH 00008 * 00009 * Permission is hereby granted, free of charge, to any person obtaining 00010 * a copy of this software and associated documentation files (the 00011 * "Software"), to deal in the Software without restriction, including 00012 * without limitation the rights to use, copy, modify, merge, publish, 00013 * distribute, sublicense, and/or sell copies of the Software, and to 00014 * permit persons to whom the Software is furnished to do so, subject to 00015 * the following conditions: 00016 * 00017 * The above copyright notice and this permission notice shall be 00018 * included in all copies or substantial portions of the Software. 00019 * 00020 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00021 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00022 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00023 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00024 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00025 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00026 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00027 */ 00028 00029 #include "ParsedValue.h" 00030 #include "NullValue.h" 00031 #include "IntegerValue.h" 00032 #include "FloatValue.h" 00033 #include "CharValue.h" 00034 #include <stdio.h> 00035 #include <string.h> 00036 #include <ctype.h> 00037 00038 ParsedValue::ParsedValue(const char *str, bool copy) : _value(str, copy) 00039 { 00040 if (_value.valueType() != VALUE_NULL) { 00041 _type = VALUE_CHARACTER; 00042 extractValue(); 00043 } else { 00044 _type = VALUE_NULL; 00045 } 00046 } 00047 00048 uint8_t ParsedValue::valueType() const 00049 { 00050 return _type; 00051 } 00052 00053 long ParsedValue::integerValue() const 00054 { 00055 if (_type != VALUE_INTEGER) 00056 return 0; 00057 return _integer; 00058 } 00059 00060 double ParsedValue::floatValue() const 00061 { 00062 if (_type != VALUE_FLOAT) 00063 return 0.0; 00064 return _float; 00065 } 00066 00067 const char * ParsedValue::characterValue() const 00068 { 00069 if (_type != VALUE_CHARACTER) 00070 return NULL; 00071 return _value.characterValue(); 00072 } 00073 00074 size_t ParsedValue::write(AbstractDataSink& sink) const 00075 { 00076 return _value.write(sink); 00077 } 00078 00079 size_t ParsedValue::length() const 00080 { 00081 return _value.length(); 00082 } 00083 00084 Value* ParsedValue::copy() const 00085 { 00086 if (_type == VALUE_NULL) 00087 return new NullValue(); 00088 if (_type == VALUE_INTEGER) 00089 return new IntegerValue(_integer); 00090 if (_type == VALUE_FLOAT) 00091 return new FloatValue(_float, _digits, _zflag); 00092 return new CharValue(_value.characterValue(), true); 00093 } 00094 00095 void ParsedValue::extractValue() 00096 { 00097 int len; 00098 const char *str = _value.characterValue(); 00099 00100 sscanf(str, "%li%n", &_integer, &len); 00101 if ((size_t)len == strlen(str)) { 00102 _type = VALUE_INTEGER; 00103 return; 00104 } 00105 00106 sscanf(str, "%lf%n", &_float, &len); 00107 if ((size_t)len == strlen(str)) { 00108 bool floating = false; 00109 _digits = 0; 00110 _zflag = false; 00111 for (char c, *ptr = (char*)str; (c = *ptr) > 0; ptr++) { 00112 if (floating) { 00113 if (c == '.') 00114 return; 00115 _digits++; 00116 } else { 00117 if (c == '.') 00118 floating = true; 00119 else if (isdigit(c)) 00120 _zflag = true; 00121 } 00122 } 00123 if (!((floating) && (!_digits))) { 00124 _type = VALUE_FLOAT; 00125 return; 00126 } 00127 } 00128 }
Generated on Wed Jul 13 2022 09:53:06 by 1.7.2