mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.
Dependents: MbedSmartRestMain MbedSmartRestMain
ParsedRecord.cpp
00001 /* 00002 * ParsedRecord.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 "ParsedRecord.h" 00030 #include "ComposedRecord.h" 00031 #include "ParsedValue.h" 00032 #include <stdlib.h> 00033 #include <string.h> 00034 00035 00036 /*-------------------------------------------------------------------------*/ 00037 ParsedRecord::ParsedRecord(bool copy) : 00038 _copy(copy) 00039 { 00040 _count = 0; 00041 #ifdef SMARTREST_PARSED_DYNAMIC_ALLOC 00042 _values = NULL; 00043 #endif 00044 _buffer = NULL; 00045 } 00046 /*-------------------------------------------------------------------------*/ 00047 ParsedRecord::~ParsedRecord() 00048 { 00049 clear(); 00050 00051 #ifdef SMARTREST_PARSED_DYNAMIC_ALLOC 00052 if (_values != NULL) 00053 free(_values); 00054 #endif 00055 } 00056 /*-------------------------------------------------------------------------*/ 00057 size_t ParsedRecord::values() const 00058 { 00059 return _count; 00060 } 00061 /*-------------------------------------------------------------------------*/ 00062 const Value& ParsedRecord::value(size_t index) const 00063 { 00064 return *_values[index]; 00065 } 00066 /*-------------------------------------------------------------------------*/ 00067 ParsedRecord* ParsedRecord::copy() const 00068 { 00069 ParsedRecord *copy = new ParsedRecord(true); 00070 copy->set(_buffer, _count); 00071 return copy; 00072 } 00073 /*-------------------------------------------------------------------------*/ 00074 const char * ParsedRecord::rawValue(size_t index) const 00075 { 00076 if (index >= _count) 00077 return NULL; 00078 00079 const char *ptr = _buffer; 00080 for (size_t n = 0; n < index; n++) 00081 { 00082 ptr += strlen(ptr) + 1; 00083 } 00084 00085 return ptr; 00086 } 00087 /*-------------------------------------------------------------------------*/ 00088 bool ParsedRecord::set(const char *buffer, size_t count) 00089 { 00090 clear(); 00091 00092 #ifdef SMARTREST_PARSED_DYNAMIC_ALLOC 00093 const Value **values = (const Value**)realloc(_values, count*sizeof(Value*)); 00094 if (values == NULL) 00095 return false; 00096 _values = values; 00097 #else 00098 if (count > SMARTREST_PARSED_FIXED_SIZE) 00099 count = SMARTREST_PARSED_FIXED_SIZE; 00100 #endif 00101 00102 if (_copy) 00103 { 00104 const char *ptr = buffer; 00105 for (size_t n = 0; n < count; n++) 00106 ptr += strlen(ptr) + 1; 00107 _buffer = (const char*)malloc(ptr-buffer); 00108 if (_buffer == NULL) 00109 return false; 00110 memcpy((char*)_buffer, buffer, ptr-buffer); 00111 } 00112 else 00113 { 00114 _buffer = buffer; 00115 } 00116 _count = count; 00117 00118 const char *ptr = _buffer; 00119 for (size_t n = 0; n < count; n++) 00120 { 00121 _values[n] = new ParsedValue(ptr, false); 00122 ptr += strlen(ptr) + 1; 00123 } 00124 00125 return true; 00126 } 00127 /*-------------------------------------------------------------------------*/ 00128 void ParsedRecord::clear() 00129 { 00130 for (size_t n = 0; n < _count; n++) 00131 delete _values[n]; 00132 if ((_copy) && (_buffer != NULL)) 00133 free((char*)_buffer); 00134 _buffer = NULL; 00135 _count = 0; 00136 } 00137 /*-------------------------------------------------------------------------*/
Generated on Tue Jul 12 2022 15:21:50 by 1.7.2