-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-csv.H
More file actions
53 lines (47 loc) · 1.04 KB
/
parse-csv.H
File metadata and controls
53 lines (47 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# ifndef PARSE_CSV_H
# define PARSE_CSV_H
# include <sstream>
# include <istream>
# include <string>
# include <tpl_array.H>
using namespace std;
inline Array<string> csv_read_row(istream & in, char delimiter)
{
stringstream ss;
bool inquotes = false;
Array<string> row;//relying on RVO
while (in.good())
{
char c = in.get();
if (not inquotes and c == '"') //beginquotechar
inquotes = true;
else if (inquotes and c == '"') //quotechar
{
if (in.peek() == '"') //2 consecutive quotes resolve to 1
ss << (char)in.get();
else //endquotechar
inquotes=false;
}
else if (not inquotes and c == delimiter) //end of field
{
row.append(ss.str());
ss.str("");
}
else if (not inquotes and (c == '\r' or c == '\n') )
{
if (in.peek() == '\n')
in.get();
row.append(ss.str());
return row;
}
else
ss << c;
}
return row;
}
inline Array<string> csv_read_row(string & line, char delimiter)
{
stringstream ss(line);
return csv_read_row(ss, delimiter);
}
# endif // PARSE_CSV_H