Generic SmartRest library

Dependents:   SmartRestUnitTest MbedSmartRest MbedSmartRestStreaming

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CharValue.cpp Source File

CharValue.cpp

00001 /*
00002  * CharValue.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 "CharValue.h"
00030 #include "NullValue.h"
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 inline int8_t __charvalue_need_escape(const char *str);
00035 
00036 CharValue::CharValue(const char *str, bool copy)
00037 {
00038     if ((str == NULL) || (strlen(str) == 0)) {
00039         _alloc = false;
00040         _str = NULL;
00041     } else {
00042         _alloc = copy;
00043         if (copy) {
00044             _str = (const char*)malloc(strlen(str)*sizeof(char));
00045             strcpy((char*)_str, str);
00046         } else {
00047             _str = str;
00048         }
00049     }
00050 }
00051 
00052 CharValue::~CharValue()
00053 {
00054     if (_alloc)
00055         free((void*)_str);
00056 }
00057 
00058 uint8_t CharValue::valueType() const
00059 {
00060     if (_str == NULL)
00061         return VALUE_NULL;
00062     return VALUE_CHARACTER;
00063 }
00064 
00065 long CharValue::integerValue() const
00066 {
00067     return 0;
00068 }
00069 
00070 double CharValue::floatValue() const
00071 {
00072     return 0.0;
00073 }
00074 
00075 const char * CharValue::characterValue() const
00076 {
00077     return _str;
00078 }
00079 
00080 size_t CharValue::write(AbstractDataSink& sink) const
00081 {
00082     if (_str == NULL)
00083         return 0;
00084     size_t n = 0;
00085     int8_t esc = __charvalue_need_escape(_str) ? 1 : 0;
00086     if (esc) n += sink.write('"');
00087     for (char *q = (char*)_str, c; (c = *q) > 0; q++) {
00088         if ((esc) && (c == '"'))
00089             n += sink.write('"');
00090         n += sink.write(c);
00091     }
00092     if (esc) n += sink.write('"');
00093     return n;
00094 }
00095 
00096 size_t CharValue::length() const
00097 {
00098     if (_str == NULL)
00099         return 0;
00100     size_t n = 0;
00101     int8_t esc = __charvalue_need_escape(_str) ? 1 : 0;
00102     if (esc)
00103         n += 2;
00104     for (char *q = (char*)_str, c; (c = *q) > 0; q++) {
00105         if ((esc) && (c == '"'))
00106             n++;
00107         n++;
00108     }
00109     return n;
00110 }
00111 
00112 int8_t __charvalue_need_escape(const char *str)
00113 {
00114     int8_t w = 0;
00115     for (char c; (c = *str) > 0; str++) {
00116         if ((c == ' ') || (c == '\t')) {
00117             if (w == 0)
00118                 return 1;
00119             w = 2;
00120         } else {
00121             w = 1;
00122             if ((c == '"') || (c == ',') || (c == '\r') || (c == '\n'))
00123                 return 1;
00124         }
00125     }
00126     if (w == 2)
00127         return 1;
00128     return 0;
00129 }
00130 
00131 Value* CharValue::copy() const
00132 {
00133     if (_str == NULL)
00134         return new NullValue();
00135     return new CharValue(_str, true);
00136 }
00137