Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
ParsedRecord.cpp@20:505d29d5bdfc, 2015-04-13 (annotated)
- Committer:
- xinlei
- Date:
- Mon Apr 13 14:24:44 2015 +0000
- Revision:
- 20:505d29d5bdfc
- Parent:
- 11:e1bee9a77652
v2.1rc1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 0:099f76422485 | 1 | /* |
Cumulocity | 0:099f76422485 | 2 | * ParsedRecord.cpp |
Cumulocity | 0:099f76422485 | 3 | * |
Cumulocity | 0:099f76422485 | 4 | * Created on: Nov 1, 2013 |
Cumulocity | 0:099f76422485 | 5 | * * Authors: Vincent Wochnik <v.wochnik@gmail.com> |
Cumulocity | 0:099f76422485 | 6 | * |
Cumulocity | 0:099f76422485 | 7 | * Copyright (c) 2013 Cumulocity GmbH |
Cumulocity | 0:099f76422485 | 8 | * |
Cumulocity | 0:099f76422485 | 9 | * Permission is hereby granted, free of charge, to any person obtaining |
Cumulocity | 0:099f76422485 | 10 | * a copy of this software and associated documentation files (the |
Cumulocity | 0:099f76422485 | 11 | * "Software"), to deal in the Software without restriction, including |
Cumulocity | 0:099f76422485 | 12 | * without limitation the rights to use, copy, modify, merge, publish, |
Cumulocity | 0:099f76422485 | 13 | * distribute, sublicense, and/or sell copies of the Software, and to |
Cumulocity | 0:099f76422485 | 14 | * permit persons to whom the Software is furnished to do so, subject to |
Cumulocity | 0:099f76422485 | 15 | * the following conditions: |
Cumulocity | 0:099f76422485 | 16 | * |
Cumulocity | 0:099f76422485 | 17 | * The above copyright notice and this permission notice shall be |
Cumulocity | 0:099f76422485 | 18 | * included in all copies or substantial portions of the Software. |
Cumulocity | 0:099f76422485 | 19 | * |
Cumulocity | 0:099f76422485 | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
Cumulocity | 0:099f76422485 | 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
Cumulocity | 0:099f76422485 | 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Cumulocity | 0:099f76422485 | 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
Cumulocity | 0:099f76422485 | 24 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
Cumulocity | 0:099f76422485 | 25 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
Cumulocity | 0:099f76422485 | 26 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Cumulocity | 0:099f76422485 | 27 | */ |
xinlei | 20:505d29d5bdfc | 28 | #include <stdlib.h> |
xinlei | 20:505d29d5bdfc | 29 | #include <string.h> |
xinlei | 20:505d29d5bdfc | 30 | #include <stdio.h> |
Cumulocity | 0:099f76422485 | 31 | |
Cumulocity | 0:099f76422485 | 32 | #include "ParsedRecord.h" |
Cumulocity | 0:099f76422485 | 33 | #include "ComposedRecord.h" |
Cumulocity | 0:099f76422485 | 34 | #include "ParsedValue.h" |
Cumulocity | 0:099f76422485 | 35 | |
Cumulocity | 11:e1bee9a77652 | 36 | |
Cumulocity | 11:e1bee9a77652 | 37 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 4:059b8b90e0d9 | 38 | ParsedRecord::ParsedRecord(bool copy) : |
Cumulocity | 11:e1bee9a77652 | 39 | _copy(copy) |
Cumulocity | 0:099f76422485 | 40 | { |
Cumulocity | 11:e1bee9a77652 | 41 | _count = 0; |
Cumulocity | 0:099f76422485 | 42 | #ifdef SMARTREST_PARSED_DYNAMIC_ALLOC |
Cumulocity | 11:e1bee9a77652 | 43 | _values = NULL; |
Cumulocity | 0:099f76422485 | 44 | #endif |
Cumulocity | 11:e1bee9a77652 | 45 | _buffer = NULL; |
Cumulocity | 0:099f76422485 | 46 | } |
Cumulocity | 11:e1bee9a77652 | 47 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 48 | ParsedRecord::~ParsedRecord() |
Cumulocity | 0:099f76422485 | 49 | { |
Cumulocity | 11:e1bee9a77652 | 50 | clear(); |
Cumulocity | 0:099f76422485 | 51 | |
Cumulocity | 0:099f76422485 | 52 | #ifdef SMARTREST_PARSED_DYNAMIC_ALLOC |
xinlei | 20:505d29d5bdfc | 53 | if (_values) |
Cumulocity | 11:e1bee9a77652 | 54 | free(_values); |
Cumulocity | 0:099f76422485 | 55 | #endif |
Cumulocity | 0:099f76422485 | 56 | } |
Cumulocity | 11:e1bee9a77652 | 57 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 58 | size_t ParsedRecord::values() const |
Cumulocity | 0:099f76422485 | 59 | { |
Cumulocity | 11:e1bee9a77652 | 60 | return _count; |
Cumulocity | 0:099f76422485 | 61 | } |
Cumulocity | 11:e1bee9a77652 | 62 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 63 | const Value& ParsedRecord::value(size_t index) const |
Cumulocity | 0:099f76422485 | 64 | { |
Cumulocity | 11:e1bee9a77652 | 65 | return *_values[index]; |
Cumulocity | 0:099f76422485 | 66 | } |
Cumulocity | 11:e1bee9a77652 | 67 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 4:059b8b90e0d9 | 68 | ParsedRecord* ParsedRecord::copy() const |
Cumulocity | 0:099f76422485 | 69 | { |
Cumulocity | 11:e1bee9a77652 | 70 | ParsedRecord *copy = new ParsedRecord(true); |
Cumulocity | 11:e1bee9a77652 | 71 | copy->set(_buffer, _count); |
Cumulocity | 11:e1bee9a77652 | 72 | return copy; |
Cumulocity | 0:099f76422485 | 73 | } |
Cumulocity | 11:e1bee9a77652 | 74 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 75 | const char * ParsedRecord::rawValue(size_t index) const |
Cumulocity | 0:099f76422485 | 76 | { |
Cumulocity | 11:e1bee9a77652 | 77 | if (index >= _count) |
Cumulocity | 11:e1bee9a77652 | 78 | return NULL; |
Cumulocity | 0:099f76422485 | 79 | |
Cumulocity | 11:e1bee9a77652 | 80 | const char *ptr = _buffer; |
xinlei | 20:505d29d5bdfc | 81 | for (size_t n = 0; n < index; n++) { |
Cumulocity | 11:e1bee9a77652 | 82 | ptr += strlen(ptr) + 1; |
Cumulocity | 11:e1bee9a77652 | 83 | } |
Cumulocity | 11:e1bee9a77652 | 84 | return ptr; |
Cumulocity | 0:099f76422485 | 85 | } |
Cumulocity | 11:e1bee9a77652 | 86 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 4:059b8b90e0d9 | 87 | bool ParsedRecord::set(const char *buffer, size_t count) |
Cumulocity | 0:099f76422485 | 88 | { |
Cumulocity | 11:e1bee9a77652 | 89 | clear(); |
Cumulocity | 4:059b8b90e0d9 | 90 | |
Cumulocity | 0:099f76422485 | 91 | #ifdef SMARTREST_PARSED_DYNAMIC_ALLOC |
Cumulocity | 11:e1bee9a77652 | 92 | const Value **values = (const Value**)realloc(_values, count*sizeof(Value*)); |
Cumulocity | 11:e1bee9a77652 | 93 | if (values == NULL) |
Cumulocity | 11:e1bee9a77652 | 94 | return false; |
Cumulocity | 11:e1bee9a77652 | 95 | _values = values; |
Cumulocity | 0:099f76422485 | 96 | #else |
Cumulocity | 11:e1bee9a77652 | 97 | if (count > SMARTREST_PARSED_FIXED_SIZE) |
Cumulocity | 11:e1bee9a77652 | 98 | count = SMARTREST_PARSED_FIXED_SIZE; |
Cumulocity | 0:099f76422485 | 99 | #endif |
Cumulocity | 0:099f76422485 | 100 | |
xinlei | 20:505d29d5bdfc | 101 | if (_copy) { |
Cumulocity | 11:e1bee9a77652 | 102 | const char *ptr = buffer; |
Cumulocity | 11:e1bee9a77652 | 103 | for (size_t n = 0; n < count; n++) |
Cumulocity | 11:e1bee9a77652 | 104 | ptr += strlen(ptr) + 1; |
Cumulocity | 11:e1bee9a77652 | 105 | _buffer = (const char*)malloc(ptr-buffer); |
Cumulocity | 11:e1bee9a77652 | 106 | if (_buffer == NULL) |
Cumulocity | 11:e1bee9a77652 | 107 | return false; |
Cumulocity | 11:e1bee9a77652 | 108 | memcpy((char*)_buffer, buffer, ptr-buffer); |
xinlei | 20:505d29d5bdfc | 109 | } else { |
Cumulocity | 11:e1bee9a77652 | 110 | _buffer = buffer; |
Cumulocity | 11:e1bee9a77652 | 111 | } |
Cumulocity | 11:e1bee9a77652 | 112 | _count = count; |
Cumulocity | 4:059b8b90e0d9 | 113 | |
Cumulocity | 11:e1bee9a77652 | 114 | const char *ptr = _buffer; |
xinlei | 20:505d29d5bdfc | 115 | for (size_t n = 0; n < count; n++) { |
Cumulocity | 11:e1bee9a77652 | 116 | _values[n] = new ParsedValue(ptr, false); |
Cumulocity | 11:e1bee9a77652 | 117 | ptr += strlen(ptr) + 1; |
Cumulocity | 11:e1bee9a77652 | 118 | } |
Cumulocity | 11:e1bee9a77652 | 119 | return true; |
Cumulocity | 0:099f76422485 | 120 | } |
Cumulocity | 11:e1bee9a77652 | 121 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 122 | void ParsedRecord::clear() |
Cumulocity | 0:099f76422485 | 123 | { |
Cumulocity | 11:e1bee9a77652 | 124 | for (size_t n = 0; n < _count; n++) |
Cumulocity | 11:e1bee9a77652 | 125 | delete _values[n]; |
Cumulocity | 11:e1bee9a77652 | 126 | if ((_copy) && (_buffer != NULL)) |
Cumulocity | 11:e1bee9a77652 | 127 | free((char*)_buffer); |
Cumulocity | 11:e1bee9a77652 | 128 | _buffer = NULL; |
Cumulocity | 11:e1bee9a77652 | 129 | _count = 0; |
Cumulocity | 0:099f76422485 | 130 | } |
Cumulocity | 11:e1bee9a77652 | 131 | /*-------------------------------------------------------------------------*/ |