diff -r 8839fba10a350df17d9b59c1dc2de06cd42b6e80 -r a3536cf22d96d19e6746e2606d31ffed8f81f8ff include/csvpp.h --- a/include/csvpp.h Sat Mar 15 00:19:13 2014 -0500 +++ b/include/csvpp.h Tue Mar 25 22:16:53 2014 -0500 @@ -8,7 +8,7 @@ #include -#define VERSION "2.1" +#define VERSION "2.2" namespace csvpp { @@ -18,9 +18,13 @@ private: std::vector header; bool skipheader; + char delimiter_char; public: const char * newline; - RowReader(bool skipheader=false,const char * newline="\n") : skipheader(skipheader), newline(newline) { } + // Adding support for custom delimiter character + // Based on the patch by Hanifa + // https://code.google.com/p/csvpp/issues/detail?id=2 + RowReader(char delimiter_char = ',', bool skipheader=false,const char * newline="\n") : skipheader(skipheader), newline(newline) { } void clear() { header.clear(); } friend std::istream & operator>>(std::istream & os, RowReader & r); friend std::ostream & operator<<(std::ostream & os, const RowWriter & r); @@ -32,7 +36,6 @@ friend std::ostream & operator<<(std::ostream & os, const RowWriter & r); }; - //typedef std::vector rows; typedef RowReader::const_iterator rowiterator; } diff -r 8839fba10a350df17d9b59c1dc2de06cd42b6e80 -r a3536cf22d96d19e6746e2606d31ffed8f81f8ff src/csvpp.cpp --- a/src/csvpp.cpp Sat Mar 15 00:19:13 2014 -0500 +++ b/src/csvpp.cpp Tue Mar 25 22:16:53 2014 -0500 @@ -22,7 +22,7 @@ { for(unsigned int i = 0; i < r[0].header.size() - 1; i++) { - os << r[0].header[i] << ","; + os << r[0].header[i] << r.delimiter_char; } os << r[0].header[r[0].header.size() - 1] << r[0].newline; } @@ -31,7 +31,7 @@ for(it = r[i].begin(); it != r[i].end(); it++) { if (distance(r[i].begin(), it) != (int)(r[i].size() - 1)) - os << it->second << ","; + os << it->second << r.delimiter_char; else os << it->second; } @@ -60,7 +60,7 @@ if(r.header.size() == 0 && !r.skipheader) { - vector sections = split(buffer, ","); + vector sections = split(buffer, r.delimiter_char); for(unsigned int i = 0; i < sections.size(); i++) r.header.push_back(sections[i]); } else { @@ -73,7 +73,7 @@ If we aren't inside of a quote - store the value using the current header 'pointer' and keep scanning */ - if (c == ',') + if (c == r.delimiter_char) { if (startquote) { @@ -114,7 +114,7 @@ { for(unsigned int x = i; x < buffer.length(); x++) { - if (buffer[x] == ',' || x == buffer.length()) + if (buffer[x] == r.delimiter_char || x == buffer.length()) { i = x-1; break; diff -r 8839fba10a350df17d9b59c1dc2de06cd42b6e80 -r a3536cf22d96d19e6746e2606d31ffed8f81f8ff tests.cpp --- a/tests.cpp Sat Mar 15 00:19:13 2014 -0500 +++ b/tests.cpp Tue Mar 25 22:16:53 2014 -0500 @@ -5,6 +5,9 @@ using namespace csvpp; int main() { + int test = 1; + // Test 1 + { RowReader tmp; stringstream ss; ss << "field1,field2,field3\r\n123,234,345\r\n999,000,111\r\n"; @@ -20,6 +23,7 @@ { cout << tmp["field1"] << endl; cerr << "Failed at row " << row << endl; + cerr << "Failed at test " << test << endl; return 1; } break; @@ -27,12 +31,88 @@ if (tmp["field1"] != "999") { cerr << "Failed at row " << row << endl; + cerr << "Failed at test " << test << endl; return 1; } break; } row++; } + } + + test++; + // Test 2 + { + RowReader tmp; + stringstream ss; + ss << "field1,field2,field3\n123,234,345\n999,000,111\n"; + ss >> tmp; + rowiterator it; + int row = 0; + while(ss >> tmp) + { + switch (row) + { + case 0: + if (tmp["field1"] != "123") + { + cout << tmp["field1"] << endl; + cerr << "Failed at row " << row << endl; + cerr << "Failed at test " << test << endl; + return 1; + } + break; + case 1: + if (tmp["field1"] != "999") + { + cerr << "Failed at row " << row << endl; + cerr << "Failed at test " << test << endl; + return 1; + } + break; + } + row++; + } + } + + test++; + // Test 3 + // Testing patch provided by Hanifa + // https://code.google.com/p/csvpp/issues/detail?id=2 + { + RowReader tmp('|'); + stringstream ss; + ss << "field1|field2|field3\r\n123|234|345\r\n999|000|111\r\n"; + ss >> tmp; + rowiterator it; + int row = 0; + + while(ss >> tmp) + { + switch (row) + { + case 0: + if (tmp["field1"] != "123") + { + cout << tmp["field1"] << endl; + cerr << "Failed at row " << row << endl; + cerr << "Failed at test " << test << endl; + return 1; + } + break; + case 1: + if (tmp["field1"] != "999") + { + cerr << "Failed at row " << row << endl; + cerr << "Failed at test " << test << endl; + return 1; + } + break; + } + row++; + } + + } cout << "All tests ran successfully" << endl; return 0;