From fad2276cf8fdd8c464bbb78067d9649d1d20954e Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 19 Sep 2015 10:25:05 +0200 Subject: [PATCH] trim the line and each field --- src/lib/ebus/filereader.h | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/lib/ebus/filereader.h b/src/lib/ebus/filereader.h index e366b39f..0b873305 100644 --- a/src/lib/ebus/filereader.h +++ b/src/lib/ebus/filereader.h @@ -82,6 +82,7 @@ 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] == '/')) @@ -99,7 +100,9 @@ public: if (quotedText) field << ch; else { - row.push_back(field.str()); + string str = field.str(); + trim(str); + row.push_back(str); field.str(""); } break; @@ -125,7 +128,9 @@ public: } prev = ch; } - row.push_back(field.str()); + string str = field.str(); + trim(str); + row.push_back(str); result_t result; vector::iterator it = row.begin(); @@ -145,7 +150,7 @@ public: if (!verbose) { ifs.close(); ostringstream error; - error << filename <<":" << static_cast(lineNo); + error << filename << ":" << static_cast(lineNo); m_lastError = error.str(); return result; } @@ -176,6 +181,22 @@ public: */ virtual result_t addFromFile(vector::iterator& begin, const vector::iterator end, T arg, vector< vector >* defaults, const string& filename, unsigned int lineNo) = 0; + /** + * Left and right trim the string. + * @param str the @a string to trim. + */ + virtual void trim(string& str) { + size_t pos = str.find_first_not_of(" \t"); + if (pos!=string::npos) { + str.erase(0, pos); + } + pos = str.find_last_not_of(" \t"); + if (pos!=string::npos) { + str.erase(pos+1); + } + } + + private: /** whether this instance supports rows with defaults (starting with a star). */