-
Notifications
You must be signed in to change notification settings - Fork 84
/
csv.cpp
175 lines (146 loc) · 2.97 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "csv.hpp"
#include "text.hpp"
#include "errors.hpp"
std::vector<std::string> csv_split(const char *s) {
std::vector<std::string> ret;
while (*s && *s != '\n' && *s != '\r') {
const char *start = s;
int within = 0;
for (; *s && *s != '\n' && *s != '\r'; s++) {
if (*s == '"') {
within = !within;
}
if (*s == ',' && !within) {
break;
}
}
std::string v = std::string(start, s - start);
ret.push_back(v);
if (*s == ',') {
s++;
while (*s && isspace(*s)) {
s++;
}
if (*s == '\0' || *s == '\r' || *s == '\n') {
ret.push_back(std::string(""));
break;
}
}
}
return ret;
}
std::string csv_dequote(std::string s) {
std::string out;
for (size_t i = 0; i < s.size(); i++) {
if (s[i] == '"') {
if (i + 1 < s.size() && s[i + 1] == '"') {
out.push_back('"');
}
} else {
out.push_back(s[i]);
}
}
return out;
}
std::string csv_getline(FILE *f) {
std::string out;
int c;
while ((c = getc(f)) != EOF) {
out.push_back(c);
if (c == '\n') {
break;
}
}
return out;
}
void readcsv(const char *fn, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping) {
FILE *f = fopen(fn, "r");
if (f == NULL) {
perror(fn);
exit(EXIT_OPEN);
}
std::string s;
if ((s = csv_getline(f)).size() > 0) {
std::string err = check_utf8(s);
if (err != "") {
fprintf(stderr, "%s: %s\n", fn, err.c_str());
exit(EXIT_UTF8);
}
header = csv_split(s.c_str());
for (size_t i = 0; i < header.size(); i++) {
header[i] = csv_dequote(header[i]);
}
}
while ((s = csv_getline(f)).size() > 0) {
std::string err = check_utf8(s);
if (err != "") {
fprintf(stderr, "%s: %s\n", fn, err.c_str());
exit(EXIT_UTF8);
}
std::vector<std::string> line = csv_split(s.c_str());
if (line.size() > 0) {
line[0] = csv_dequote(line[0]);
}
for (size_t i = 0; i < line.size() && i < header.size(); i++) {
// printf("putting %s\n", line[0].c_str());
mapping.insert(std::pair<std::string, std::vector<std::string>>(line[0], line));
}
}
if (fclose(f) != 0) {
perror("fclose");
exit(EXIT_CLOSE);
}
}
// Follow JSON rules for what looks like a number
bool is_number(std::string const &s) {
const char *cp = s.c_str();
char c = *(cp++);
if (c == '-' || (c >= '0' && c <= '9')) {
if (c == '-') {
c = *(cp++);
}
if (c == '0') {
;
} else if (c >= '1' && c <= '9') {
c = *cp;
while (c >= '0' && c <= '9') {
cp++;
c = *cp;
}
}
if (*cp == '.') {
cp++;
c = *cp;
if (c < '0' || c > '9') {
return false;
}
while (c >= '0' && c <= '9') {
cp++;
c = *cp;
}
}
c = *cp;
if (c == 'e' || c == 'E') {
cp++;
c = *cp;
if (c == '+' || c == '-') {
cp++;
}
c = *cp;
if (c < '0' || c > '9') {
return false;
}
while (c >= '0' && c <= '9') {
cp++;
c = *cp;
}
}
if (*cp == '\0') {
return true;
} else {
// Something non-numeric at the end
return false;
}
}
return false;
}