trim the line and each field

This commit is contained in:
john30
2015-09-19 10:25:05 +02:00
parent f06e3291fe
commit fad2276cf8
+23 -2
View File
@@ -82,6 +82,7 @@ public:
vector< vector<string> > defaults; vector< vector<string> > defaults;
while (getline(ifs, line) != 0) { while (getline(ifs, line) != 0) {
lineNo++; lineNo++;
trim(line);
// skip empty lines and comments // skip empty lines and comments
size_t length = line.length(); size_t length = line.length();
if (length == 0 || line[0] == '#' || (line.length() > 1 && line[0] == '/' && line[1] == '/')) if (length == 0 || line[0] == '#' || (line.length() > 1 && line[0] == '/' && line[1] == '/'))
@@ -99,7 +100,9 @@ public:
if (quotedText) if (quotedText)
field << ch; field << ch;
else { else {
row.push_back(field.str()); string str = field.str();
trim(str);
row.push_back(str);
field.str(""); field.str("");
} }
break; break;
@@ -125,7 +128,9 @@ public:
} }
prev = ch; prev = ch;
} }
row.push_back(field.str()); string str = field.str();
trim(str);
row.push_back(str);
result_t result; result_t result;
vector<string>::iterator it = row.begin(); vector<string>::iterator it = row.begin();
@@ -176,6 +181,22 @@ public:
*/ */
virtual result_t addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end, T arg, vector< vector<string> >* defaults, const string& filename, unsigned int lineNo) = 0; virtual result_t addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end, T arg, vector< vector<string> >* 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: private:
/** whether this instance supports rows with defaults (starting with a star). */ /** whether this instance supports rows with defaults (starting with a star). */