-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvparse.cpp
68 lines (63 loc) · 1.12 KB
/
csvparse.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
#include <iostream>
#include "csvparse.h"
int CsvParse(const std::string& input, std::vector<std::string>& values)
{
std::string::size_type pos = 0;
int cnt = 0;
std::string val;
for(;;)
{
if (pos == input.length())
{
if (val != "")
{
values.push_back(val);
cnt++;
}
return cnt;
}
char c = input[pos++];
switch(c)
{
case ',':
values.push_back(val);
val = "";
cnt++;
break;
#if 0
case '"':
// case '\'':
{
if (val != "")
{
std::cerr << "Expected quotes to start at the beginning of the string" << std::endl;
std::cerr << "pos = " << pos << " input = " << input << std::endl;
return -1;
}
char c2;
do
{
c2 = input[pos++];
if (c != c2)
{
val += c2;
if (pos == input.length())
{
std::cerr << "Expected " << c << " before end of line" << std::endl;
return -1;
}
}
} while (c2 != c);
}
break;
#endif
case ' ':
case '\t':
// Ignore spaces.
break;
default:
val += c;
break;
}
}
}