mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.
Dependents: MbedSmartRestMain MbedSmartRestMain
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 #include <ctype.h> 00034 00035 00036 /*-------------------------------------------------------------------------*/ 00037 CharValue::CharValue(const char *str, bool copy) 00038 { 00039 if ((str == NULL) || (*str == '\0')) 00040 { 00041 _alloc = false; 00042 _str = NULL; 00043 } 00044 else 00045 { 00046 _alloc = copy; 00047 if (copy) 00048 { 00049 _str = (const char*)malloc(strlen(str)*sizeof(char)); 00050 if (_str == NULL) 00051 return; 00052 strcpy((char*)_str, str); 00053 _escape = escapeCheck(); 00054 } 00055 else 00056 { 00057 _str = str; 00058 _escape = escapeCheck(); 00059 } 00060 } 00061 } 00062 /*-------------------------------------------------------------------------*/ 00063 CharValue::~CharValue() 00064 { 00065 if (_alloc) 00066 free((void*)_str); 00067 } 00068 /*-------------------------------------------------------------------------*/ 00069 uint8_t CharValue::valueType() const 00070 { 00071 if (_str == NULL) 00072 return VALUE_NULL; 00073 return VALUE_CHARACTER; 00074 } 00075 /*-------------------------------------------------------------------------*/ 00076 long CharValue::integerValue() const 00077 { 00078 return 0; 00079 } 00080 /*-------------------------------------------------------------------------*/ 00081 double CharValue::floatValue() const 00082 { 00083 return 0.0; 00084 } 00085 /*-------------------------------------------------------------------------*/ 00086 const char * CharValue::characterValue() const 00087 { 00088 return _str; 00089 } 00090 /*-------------------------------------------------------------------------*/ 00091 size_t CharValue::write(AbstractDataSink& sink) const 00092 { 00093 size_t n; 00094 const char *ptr; char c; 00095 00096 if (_str == NULL) 00097 return 0; 00098 00099 n = 0; 00100 if (_escape) 00101 n += sink.write('"'); 00102 00103 for (ptr = _str; (c = *ptr) != '\0'; ptr++) 00104 { 00105 if ((_escape) && (c == '"')) 00106 n += sink.write('"'); 00107 n += sink.write(c); 00108 } 00109 00110 if (_escape) 00111 n += sink.write('"'); 00112 return n; 00113 } 00114 /*-------------------------------------------------------------------------*/ 00115 size_t CharValue::length() const 00116 { 00117 size_t n; 00118 const char *ptr; char c; 00119 00120 if (_str == NULL) 00121 return 0; 00122 00123 n = 0; 00124 if (_escape) 00125 n += 2; 00126 00127 for (ptr = _str; (c = *ptr) != '\0'; ptr++) 00128 { 00129 if ((_escape) && (c == '"')) 00130 n++; 00131 n++; 00132 } 00133 00134 return n; 00135 } 00136 /*-------------------------------------------------------------------------*/ 00137 Value* CharValue::copy() const 00138 { 00139 if (_str == NULL) 00140 return new NullValue(); 00141 return new CharValue(_str, true); 00142 } 00143 /*-------------------------------------------------------------------------*/ 00144 bool CharValue::escapeCheck() 00145 { 00146 const char *ptr; char c; 00147 00148 ptr = _str; 00149 while (*ptr != '\0') 00150 { 00151 c = *ptr; 00152 if ((isspace(c)) && ((ptr == _str) || (c != ' '))) 00153 return true; 00154 else if ((c == '"') || (c == ',')) 00155 return true; 00156 ptr++; 00157 } 00158 00159 if (isspace(c)) 00160 return true; 00161 00162 return false; 00163 } 00164 /*-------------------------------------------------------------------------*/
Generated on Tue Jul 12 2022 15:21:50 by 1.7.2