-
Notifications
You must be signed in to change notification settings - Fork 7
/
csv.cpp
138 lines (119 loc) · 3.02 KB
/
csv.cpp
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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <vector>
using namespace std;
class Csv
{ // read and parse comma-separated values
// sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625
public:
Csv(istream &fin = cin, string sep = ",") : fin(fin), fieldsep(sep) {}
int getline(string &);
string getfield(int n);
int getnfield() const { return nfield; }
private:
istream &fin; // input file pointer
string line; // input line
vector<string> field; // field strings
int nfield; // number of fields
string fieldsep; // separator characters
int split();
int endofline(char);
int advplain(const string &line, string &fld, int);
int advquoted(const string &line, string &fld, int);
};
// getline: get one line, grow as needed
int Csv::getline(string &str)
{
char c;
for (line = ""; fin.get(c) && !endofline(c);)
line += c;
split();
str = line;
return !fin.eof() || line.length() != 0;
}
// endofline: check for and consume \r, \n, \r\n, or EOF
int Csv::endofline(char c)
{
int eol;
eol = (c == '\r' || c == '\n');
if (c == '\r')
{
fin.get(c);
if (!fin.eof() && c != '\n')
fin.putback(c); // read too far
}
return eol;
}
// split: split line into fields
int Csv::split()
{
string fld;
int i, j;
nfield = 0;
if (line.length() == 0)
return 0;
i = 0;
do
{
if (i < line.length() && line[i] == '"')
j = advquoted(line, fld, ++i); // skip quote
else
j = advplain(line, fld, i);
if (nfield >= field.size())
field.push_back(fld);
else
field[nfield] = fld;
nfield++;
i = j + 1;
} while (j < line.length());
return nfield;
}
// advquoted: quoted field: return index of next separator
int Csv::advquoted(const string &s, string &fld, int i)
{
int j;
fld = "";
for (j = i; j < s.length(); j++)
{
if (s[j] == '"' && s[++j] != '"')
{
int k = s.find_first_of(fieldsep, j);
if (k > s.length()) // no separator found
k = s.length();
for (k -= j; k-- > 0;)
fld += s[j++];
break;
}
fld += s[j];
}
return j;
}
// advplain: unquoted field; return index of next separator
int Csv::advplain(const string &s, string &fld, int i)
{
int j;
j = s.find_first_of(fieldsep, i); // look for separator
if (j > s.length()) // none found
j = s.length();
fld = string(s, i, j - i);
return j;
}
// getfield: return n-th field
string Csv::getfield(int n)
{
if (n < 0 || n >= nfield)
return "";
else
return field[n];
}
int main(int argc, char const *argv[])
{
string line;
Csv csv;
while (csv.getline(line) != 0)
{
cout << "line = '" << line << "'\n";
for (int i = 0; i < csv.getnfield(); i++)
cout << "field[" << i << "] = '" << csv.getfield(i) << "'\n";
}
return 0;
}