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