From 5e7558acbecc1ca8564a6e052badcbefa20cc95a Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 7 Nov 2015 09:16:17 +0100 Subject: [PATCH] extracted splitting of fields into own method and fixed parsing dquote --- src/lib/ebus/filereader.h | 125 ++++++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 51 deletions(-) diff --git a/src/lib/ebus/filereader.h b/src/lib/ebus/filereader.h index e9f1799a..357ee670 100644 --- a/src/lib/ebus/filereader.h +++ b/src/lib/ebus/filereader.h @@ -89,11 +89,19 @@ public: if (result!=RESULT_OK || !isValidAddress(zz)) defaultDest = ""; // invalid: not in hex or no master/slave/broadcast address else { - size_t lastDot = filename.find_first_of('.', firstDot+1); - if (lastDot>firstDot && lastDot-firstDot<=6) { // potential ident, matches "^ZZ.IDENT." - defaultCircuit = filename.substr(firstDot+1, lastDot-firstDot-1); + size_t endDot = filename.find_first_of('.', firstDot+1); + if (endDot>firstDot && endDot-firstDot<=6) { // potential ident, matches "^ZZ.IDENT." + defaultCircuit = filename.substr(firstDot+1, endDot-firstDot-1); // IDENT if (defaultCircuit.find_first_of(' ')!=string::npos) defaultCircuit = ""; // invalid: contains spaces + else { + size_t nextDot = filename.find_first_of('.', endDot+1); + if (nextDot!=string::npos && nextDot>endDot+1) { // potential index suffix, matches "^ZZ.IDENT.[0-9]*." + parseInt(filename.substr(endDot+1, nextDot-endDot-1).c_str(), 10, 1, 16, result, NULL); + if (result==RESULT_OK) + defaultCircuit = filename.substr(firstDot+1, nextDot-firstDot-1); // IDENT.[0-9]* + } + } } } } @@ -102,56 +110,10 @@ public: vector< vector > defaults; while (getline(ifs, line) != 0) { lineNo++; - trim(line); - // skip empty lines and comments - size_t length = line.length(); - if (length == 0 || line[0] == '#' || (line.length() > 1 && line[0] == '/' && line[1] == '/')) + splitFields(line, row); + if (row.empty()) continue; - row.clear(); - bool quotedText = false; - ostringstream field; - char prev = FIELD_SEPARATOR; - for (size_t pos = 0; pos < length; pos++) { - char ch = line[pos]; - switch (ch) - { - case FIELD_SEPARATOR: - if (quotedText) - field << ch; - else { - string str = field.str(); - trim(str); - row.push_back(str); - field.str(""); - } - break; - case TEXT_SEPARATOR: - if (quotedText) { - quotedText = false; - } - else if (prev == TEXT_SEPARATOR) { // double dquote - quotedText = true; - field << ch; - } - else if (prev == FIELD_SEPARATOR) { - quotedText = true; - } - else - field << ch; - break; - case '\r': - break; - default: - field << ch; - break; - } - prev = ch; - } - string str = field.str(); - trim(str); - row.push_back(str); - result_t result; vector::iterator it = row.begin(); const vector::iterator end = row.end(); @@ -252,6 +214,67 @@ public: } } + /** + * Split the line into fields. + * @param line the @a string with the line to split. + * @param row the @a vector to which to add the fields. + * @return true if the line was split, false if the line was completely empty or a comment line. + */ + static bool splitFields(string& line, vector& row) { + row.clear(); + trim(line); + // skip empty lines and comments + size_t length = line.length(); + if (length == 0 || line[0] == '#' || (line.length() > 1 && line[0] == '/' && line[1] == '/')) + return false; + + bool quotedText = false, wasQuoted = false; + ostringstream field; + char prev = FIELD_SEPARATOR; + for (size_t pos = 0; pos < length; pos++) { + char ch = line[pos]; + switch (ch) + { + case FIELD_SEPARATOR: + if (quotedText) { + field << ch; + } else { + string str = field.str(); + trim(str); + row.push_back(str); + field.str(""); + wasQuoted = false; + } + break; + case TEXT_SEPARATOR: + if (prev == TEXT_SEPARATOR && !quotedText) { // double dquote + field << ch; + quotedText = true; + } else if (quotedText) { + quotedText = false; + } else if (prev == FIELD_SEPARATOR) { + quotedText = wasQuoted = true; + } else { + field << ch; + } + break; + case '\r': + break; + default: + if (prev==TEXT_SEPARATOR && !quotedText && wasQuoted) { + field << TEXT_SEPARATOR; // single dquote in the middle of formerly quoted text + quotedText = true; + } + field << ch; + break; + } + prev = ch; + } + string str = field.str(); + trim(str); + row.push_back(str); + return true; + } private: