From fb3574de8c6a985efc6599797a9362e59d61ff05 Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 30 Nov 2014 10:22:36 +0100 Subject: [PATCH] added defaults, added printErrorPos --- src/lib/ebus/data.cpp | 28 +++++++++++++++++++++++++++- src/lib/ebus/data.h | 33 ++++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index f619a757..2af7a240 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -95,6 +95,32 @@ unsigned int parseInt(const char* str, int base, const unsigned int minValue, co return ret; } +void printErrorPos(vector::iterator begin, const vector::iterator end, vector::iterator pos, char separator) +{ + cout << "Erroneous item is here:" << endl; + bool first = true; + int cnt = 0; + if (pos > begin) + pos--; + while (begin != end) { + if (first == true) + first = false; + else { + cout << separator; + if (begin <= pos) { + cnt++; + } + } + if (begin < pos) { + cnt += (*begin).length(); + } + cout << (*begin++); + } + cout << endl; + cout << setw(cnt) << " " << setw(0) << "^" << endl; +} + + result_t DataField::create(vector::iterator& it, const vector::iterator end, DataFieldTemplates* templates, @@ -1060,7 +1086,7 @@ result_t DataFieldTemplates::add(DataField* field, bool replace) return RESULT_OK; } -result_t DataFieldTemplates::addFromFile(vector& row, void* arg) +result_t DataFieldTemplates::addFromFile(vector& row, void* arg, vector< vector >* defaults) { DataField* field = NULL; vector::iterator it = row.begin(); diff --git a/src/lib/ebus/data.h b/src/lib/ebus/data.h index 8117a9da..a62de4e3 100644 --- a/src/lib/ebus/data.h +++ b/src/lib/ebus/data.h @@ -83,6 +83,15 @@ typedef struct { */ unsigned int parseInt(const char* str, int base, const unsigned int minValue, const unsigned int maxValue, result_t& result, unsigned int* length=NULL); +/** + * @brief Print the error position of the iterator to stdout. + * @param begin the iterator to the beginning of the items. + * @param end the iterator to the end of the items. + * @param pos the iterator with the erroneous position. + * @param separator the character to place between items. + */ +void printErrorPos(vector::iterator begin, const vector::iterator end, vector::iterator pos, char separator=';'); + class DataFieldTemplates; class SingleDataField; @@ -152,7 +161,9 @@ public: /** * @brief Reads the value from the master or slave @a SymbolString. * @param masterData the unescaped master data @a SymbolString for reading binary data. + * @param masterOffset the additional offset to add for reading the master data. * @param slaveData the unescaped slave data @a SymbolString for reading binary data. + * @param slaveOffset the additional offset to add for reading the slave data. * @param output the @a ostringstream to append the formatted value to. * @param verbose whether to prepend the name, append the unit (if present), and append * the comment in square brackets (if present). @@ -570,7 +581,8 @@ public: /** * @brief Constructs a new instance. */ - FileReader() {} + FileReader(bool supportsDefaults) + : m_supportsDefaults(supportsDefaults) {} /** * @brief Destructor. */ @@ -591,6 +603,7 @@ public: unsigned int lineNo = 0; vector row; string token; + vector< vector > defaults; while (getline(ifs, line) != 0) { lineNo++; // skip empty lines and comments @@ -601,7 +614,12 @@ public: while (getline(isstr, token, FIELD_SEPARATOR) != 0) row.push_back(token); - result_t result = addFromFile(row, arg); + if (m_supportsDefaults == true && line.substr(0, 1) == "*") { + row[0] = row[0].substr(1); + defaults.push_back(row); + continue; + } + result_t result = addFromFile(row, arg, m_supportsDefaults == true ? &defaults : NULL); if (result != RESULT_OK) { cerr << "error reading \"" << filename << "\" line " << static_cast(lineNo) << ": " << getResultCode(result) << endl; ifs.close(); @@ -615,9 +633,14 @@ public: /** * @brief Adds a definition that was read from a file. * @param row the definition row read from the file. + * @param defaults all previously read default rows (initial star char removed), or NULL if not supported. * @return @a RESULT_OK on success, or an error code. */ - virtual result_t addFromFile(vector& row, T arg) = 0; + virtual result_t addFromFile(vector& row, T arg, vector< vector >* defaults) = 0; + +private: + /** whether this instance supports rows with defaults (starting with a star). */ + bool m_supportsDefaults; }; @@ -632,7 +655,7 @@ public: /** * @brief Constructs a new instance. */ - DataFieldTemplates() {} + DataFieldTemplates() : FileReader(false) {} /** * @brief Destructor. */ @@ -650,7 +673,7 @@ public: */ result_t add(DataField* message, bool replace=false); // @copydoc - virtual result_t addFromFile(vector& row, void* arg); + virtual result_t addFromFile(vector& row, void* arg, vector< vector >* defaults); /** * @brief Gets the template @a DataField instance with the specified name. * @return the template @a DataField instance, or NULL.