csvpp

csvpp Commit Details


Date:2014-03-25 22:16:00 (10 years 8 months ago)
Author:Natalie Adams
Branch:default
Commit:a3536cf22d96
Parents: 8839fba10a35
Message:updating tests

merging patch from Hanifa for custom delimiters
Changes:
Minclude/csvpp.h (3 diffs)
Msrc/csvpp.cpp (5 diffs)
Mtests.cpp (3 diffs)

File differences

include/csvpp.h
88
99
1010
11
11
1212
1313
1414
......
1818
1919
2020
21
2122
2223
23
24
25
26
27
2428
2529
2630
......
3236
3337
3438
35
3639
3740
3841
#include <istream>
#define VERSION "2.1"
#define VERSION "2.2"
namespace csvpp {
private:
std::vector<std::string> 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);
friend std::ostream & operator<<(std::ostream & os, const RowWriter & r);
};
//typedef std::vector<row> rows;
typedef RowReader::const_iterator rowiterator;
}
src/csvpp.cpp
2222
2323
2424
25
25
2626
2727
2828
......
3131
3232
3333
34
34
3535
3636
3737
......
6060
6161
6262
63
63
6464
6565
6666
......
7373
7474
7575
76
76
7777
7878
7979
......
114114
115115
116116
117
117
118118
119119
120120
{
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;
}
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;
}
if(r.header.size() == 0 && !r.skipheader)
{
vector<string> sections = split(buffer, ",");
vector<string> sections = split(buffer, r.delimiter_char);
for(unsigned int i = 0; i < sections.size(); i++)
r.header.push_back(sections[i]);
} else {
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)
{
{
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;
tests.cpp
55
66
77
8
9
10
811
912
1013
......
2023
2124
2225
26
2327
2428
2529
......
2731
2832
2933
34
3035
3136
3237
3338
3439
3540
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
36116
37117
38118
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";
{
cout << tmp["field1"] << endl;
cerr << "Failed at row " << row << endl;
cerr << "Failed at test " << test << endl;
return 1;
}
break;
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;

Archive Download the corresponding diff file

Branches

Tags

Page rendered in 0.42387s using 14 queries.