mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.
Dependents: MbedSmartRestMain MbedSmartRestMain
Revision 4:059b8b90e0d9, committed 2014-07-09
- Comitter:
- Cumulocity
- Date:
- Wed Jul 09 15:37:19 2014 +0200
- Parent:
- 3:aab51c468511
- Child:
- 5:2b74510900da
- Commit message:
- Updated from revision b9c81426d0e6
Changed in this revision
--- a/ComposedRecord.cpp Mon Jul 07 17:12:59 2014 +0200 +++ b/ComposedRecord.cpp Wed Jul 09 15:37:19 2014 +0200 @@ -114,7 +114,7 @@ return *_values[index]; } -DataGenerator* ComposedRecord::copy() const +ComposedRecord* ComposedRecord::copy() const { ComposedRecord *copy = new ComposedRecord(true); for (size_t n = 0; n < _count; n++)
--- a/ComposedRecord.h Mon Jul 07 17:12:59 2014 +0200 +++ b/ComposedRecord.h Wed Jul 09 15:37:19 2014 +0200 @@ -78,7 +78,7 @@ size_t values() const; const Value& value(size_t index) const; - DataGenerator* copy() const; + ComposedRecord* copy() const; private: #ifndef SMARTREST_COMPOSED_FIXED_SIZE
--- a/ParsedRecord.cpp Mon Jul 07 17:12:59 2014 +0200 +++ b/ParsedRecord.cpp Wed Jul 09 15:37:19 2014 +0200 @@ -33,12 +33,14 @@ #include <stdlib.h> #include <string.h> -ParsedRecord::ParsedRecord() +ParsedRecord::ParsedRecord(bool copy) : + _copy(copy) { _count = 0; #ifdef SMARTREST_PARSED_DYNAMIC_ALLOC _values = NULL; #endif + _buffer = NULL; } ParsedRecord::~ParsedRecord() @@ -63,11 +65,10 @@ return *_values[index]; } -DataGenerator* ParsedRecord::copy() const +ParsedRecord* ParsedRecord::copy() const { - ComposedRecord *copy = new ComposedRecord(true); - for (size_t n = 0; n < _count; n++) - copy->add(*_values[n]); + ParsedRecord *copy = new ParsedRecord(true); + copy->set(_buffer, _count); return copy; } @@ -84,32 +85,48 @@ return ptr; } -void ParsedRecord::set(const char *buffer, size_t count) +bool ParsedRecord::set(const char *buffer, size_t count) { clear(); - + #ifdef SMARTREST_PARSED_DYNAMIC_ALLOC const Value **values = (const Value**)realloc(_values, count*sizeof(Value*)); if (values == NULL) - return; + return false; _values = values; #else if (count > SMARTREST_PARSED_FIXED_SIZE) count = SMARTREST_PARSED_FIXED_SIZE; #endif - const char *ptr = buffer; + if (_copy) { + const char *ptr = buffer; + for (size_t n = 0; n < count; n++) + ptr += strlen(ptr) + 1; + _buffer = (const char*)malloc(ptr-buffer); + if (_buffer == NULL) + return false; + memcpy((char*)_buffer, buffer, ptr-buffer); + } else { + _buffer = buffer; + } + _count = count; + + const char *ptr = _buffer; for (size_t n = 0; n < count; n++) { _values[n] = new ParsedValue(ptr, false); ptr += strlen(ptr) + 1; } - _count = count; - _buffer = buffer; + return true; } void ParsedRecord::clear() { for (size_t n = 0; n < _count; n++) delete _values[n]; + if ((_copy) && (_buffer != NULL)) + free((char*)_buffer); + _buffer = NULL; + _count = 0; }
--- a/ParsedRecord.h Mon Jul 07 17:12:59 2014 +0200 +++ b/ParsedRecord.h Wed Jul 09 15:37:19 2014 +0200 @@ -75,13 +75,13 @@ class ParsedRecord : public Record { public: - ParsedRecord(); + ParsedRecord(bool=false); ~ParsedRecord(); size_t values() const; const Value& value(size_t index) const; - DataGenerator* copy() const; + ParsedRecord* copy() const; /** * Returns the raw value string by index @@ -91,7 +91,7 @@ const char * rawValue(size_t index) const; protected: - void set(const char*, size_t); + bool set(const char*, size_t); void clear(); private: @@ -102,6 +102,7 @@ #endif const char *_buffer; size_t _count; + bool _copy; friend class Parser; };
--- a/Parser.cpp Mon Jul 07 17:12:59 2014 +0200 +++ b/Parser.cpp Wed Jul 09 15:37:19 2014 +0200 @@ -46,15 +46,17 @@ uint8_t status; char read; reset(); - record.set(NULL, 0); + record.clear(); while ((_state < STATE_COMPLETE) && (((read = source.read()) > 0 ) || ((status = source.status()) == DS_STATUS_OK))) parse(read); - if (_state == STATE_COMPLETE) - record.set(_buffer, _count); + if (_state == STATE_COMPLETE) { + if (!record.set(_buffer, _count)) + return PARSER_INTERNAL_ERROR; + } if (_state == STATE_COMPLETE) return PARSER_SUCCESS;
--- a/Parser.h Mon Jul 07 17:12:59 2014 +0200 +++ b/Parser.h Wed Jul 09 15:37:19 2014 +0200 @@ -54,6 +54,8 @@ #define PARSER_END_OF_RESPONSE 2 /** Return value indicating a parse error. */ #define PARSER_PARSE_ERROR 3 +/** Return value indicating an internal error. */ +#define PARSER_INTERNAL_ERROR 4 /* * A record parser reading from a data source.
--- a/Record.h Mon Jul 07 17:12:59 2014 +0200 +++ b/Record.h Wed Jul 09 15:37:19 2014 +0200 @@ -57,6 +57,11 @@ size_t writeTo(AbstractDataSink&) const; size_t writtenLength() const; + virtual Record* copy() const = 0; + + /** + * Test if record contains any values. + */ operator bool() const; };
--- a/SmartRest.cpp Mon Jul 07 17:12:59 2014 +0200 +++ b/SmartRest.cpp Wed Jul 09 15:37:19 2014 +0200 @@ -80,7 +80,7 @@ return SMARTREST_INTERNAL_ERROR; } -uint8_t SmartRest::bootstrap(DataGenerator& generator) +uint8_t SmartRest::bootstrap(const DataGenerator& generator) { ParsedRecord record; int8_t ret;
--- a/SmartRest.h Mon Jul 07 17:12:59 2014 +0200 +++ b/SmartRest.h Wed Jul 09 15:37:19 2014 +0200 @@ -133,7 +133,7 @@ * sent as a template. * @return a non-zero value if and only if an error occured */ - uint8_t bootstrap(DataGenerator&); + uint8_t bootstrap(const DataGenerator&); /* * Closes the connection.