Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ParsedValue.cpp Source File

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 <stdlib.h>
00031 #include <string.h>
00032 #include <ctype.h>
00033 #include <errno.h>
00034 
00035 
00036 /*-------------------------------------------------------------------------*/
00037 ParsedValue::ParsedValue(const char *str, bool copy) :
00038     _value(str, copy),
00039     _float(0.0),
00040     _integer(0l)
00041 {
00042     if (_value.valueType() != VALUE_NULL)
00043     {
00044         _type = VALUE_CHARACTER;
00045         extractValue();
00046     }
00047     else
00048     {
00049         _type = VALUE_NULL;
00050     }
00051 }
00052 /*-------------------------------------------------------------------------*/
00053 uint8_t ParsedValue::valueType() const
00054 {
00055     return _type;
00056 }
00057 /*-------------------------------------------------------------------------*/
00058 long ParsedValue::integerValue() const
00059 {
00060     return _integer;
00061 }
00062 /*-------------------------------------------------------------------------*/
00063 double ParsedValue::floatValue() const
00064 {
00065     return _float;
00066 }
00067 /*-------------------------------------------------------------------------*/
00068 const char * ParsedValue::characterValue() const
00069 {
00070     if (_type != VALUE_CHARACTER)
00071         return NULL;
00072     return _value.characterValue();
00073 }
00074 /*-------------------------------------------------------------------------*/
00075 size_t ParsedValue::write(AbstractDataSink& sink) const
00076 {
00077     return _value.write(sink);
00078 }
00079 /*-------------------------------------------------------------------------*/
00080 size_t ParsedValue::length() const
00081 {
00082     return _value.length();
00083 }
00084 /*-------------------------------------------------------------------------*/
00085 Value* ParsedValue::copy() const
00086 {
00087     return new ParsedValue(_value.characterValue(), true);
00088 }
00089 /*-------------------------------------------------------------------------*/
00090 void ParsedValue::extractValue()
00091 {
00092     const char *str; char *ptr;
00093 
00094     str = _value.characterValue();
00095     if ((*str == '\0') || (isspace(*str)))
00096         return;
00097 
00098     errno = 0;
00099     if ((((_integer = strtol(str, &ptr, 10)) != 0) || (errno == 0)) &&
00100             (*ptr == '\0')) {
00101         _type = VALUE_INTEGER;
00102         return;
00103     }
00104 
00105     errno = 0;
00106     if ((((_float = strtod(str, &ptr)) != 0.0) || (errno == 0)) &&
00107             (*ptr == '\0')) {
00108         _type = VALUE_FLOAT;
00109         return;
00110     }
00111 }
00112 /*-------------------------------------------------------------------------*/