-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsvparser.cpp
More file actions
209 lines (184 loc) · 7.07 KB
/
csvparser.cpp
File metadata and controls
209 lines (184 loc) · 7.07 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "csvparser.h"
#include <assert.h>
#include <string.h>
#include "csvbase.h"
#include <boost/variant.hpp>
//#define DEBUG
#ifdef DEBUG
#include <stdio.h>
#include <typeinfo>
#endif
namespace csvFSM {
// States
struct Start {};
struct ReadSkipPre {};
struct ReadQuoted {};
struct ReadQuotedCheckEscape {};
struct ReadQuotedSkipPost {};
struct ReadUnquoted {};
struct ReadUnquotedWhitespace {
ReadUnquotedWhitespace(unsigned char value) : value(1,value) {}
std::string value;
};
struct ReadError {
ReadError(const char *type) : type(type) {} // i.e. for static strings
const char *type;
};
typedef boost::variant<Start,
ReadSkipPre,
ReadQuoted,
ReadQuotedCheckEscape,
ReadQuotedSkipPost,
ReadUnquoted,
ReadUnquotedWhitespace,
ReadError> States;
// Events
struct Echar { // and fallback
Echar(unsigned char value) : value(value) {}
unsigned char value;
};
struct Ewhitespace : Echar {
Ewhitespace(unsigned char value) : Echar(value) {}
};
struct Eqchar : Echar {
Eqchar(unsigned char value) : Echar(value) {}
};
struct Esep : Echar {
Esep(unsigned char value) : Echar(value) {}
};
struct Enewline : Echar {
Enewline(unsigned char value) : Echar(value) {}
};
#define TTS(S,E,Snew,code) bool on(States &self,S &s,const E &e) { code; self=Snew; return true; }
struct Trans {
Trans(csv_builder &out) : out(out) {}
/* Start eps { begin_row } ReadSkipPre */
TTS(Start, Eqchar, ReadQuoted(), { out.begin_row(); });
TTS(Start, Esep, ReadSkipPre(), { out.begin_row(); next_cell(false); });
TTS(Start, Enewline, self, { out.begin_row(); out.end_row(); });
TTS(Start, Ewhitespace, ReadSkipPre(), { out.begin_row(); });
TTS(Start, Echar, ReadUnquoted(), { out.begin_row(); add(e); });
/*
template <typename State>
TTS(State, Enewline, Start(), { ("Esep")(); out.end_row(); }); // on(self[_copy],s,Esep(e.value));
*/
TTS(ReadSkipPre, Eqchar, ReadQuoted(), {});
TTS(ReadSkipPre, Esep, self, { next_cell(false); });
TTS(ReadSkipPre, Enewline, Start(), { next_cell(false); out.end_row(); });
TTS(ReadSkipPre, Ewhitespace, self, {});
TTS(ReadSkipPre, Echar, ReadUnquoted(), { add(e); });
TTS(ReadQuoted, Eqchar, ReadQuotedCheckEscape(), {});
TTS(ReadQuoted, Esep, self, { add(e); });
// TTS(ReadQuoted, Enewline, self, { add(e); });
// TTS(ReadQuoted, Ewhitespace, self, { add(e); });
TTS(ReadQuoted, Echar, self, { add(e); });
TTS(ReadQuotedCheckEscape, Eqchar, ReadQuoted(), { add(e); });
TTS(ReadQuotedCheckEscape, Esep, ReadSkipPre(), { next_cell(); });
TTS(ReadQuotedCheckEscape, Enewline, Start(), { next_cell(); out.end_row(); });
TTS(ReadQuotedCheckEscape, Ewhitespace, ReadQuotedSkipPost(), {});
TTS(ReadQuotedCheckEscape, Echar, ReadError("char after possible endquote"), { return false; });
// TTS(ReadQuotedSkipPost, Eqchar, ReadError("quote after endquote"), { return false; });
TTS(ReadQuotedSkipPost, Esep, ReadSkipPre(), { next_cell(); });
TTS(ReadQuotedSkipPost, Enewline, Start(), { next_cell(); out.end_row(); });
TTS(ReadQuotedSkipPost, Ewhitespace, self, {});
TTS(ReadQuotedSkipPost, Echar, ReadError("char after endquote"), { return false; });
TTS(ReadUnquoted, Eqchar, ReadError("unexpected quote in unquoted string"), { return false; });
TTS(ReadUnquoted, Esep, ReadSkipPre(), { next_cell(); });
TTS(ReadUnquoted, Enewline, Start(), { next_cell(); out.end_row(); });
TTS(ReadUnquoted, Ewhitespace, ReadUnquotedWhitespace(e.value), {});
TTS(ReadUnquoted, Echar, self, { add(e); });
TTS(ReadUnquotedWhitespace, Eqchar, ReadError("unexpected quote after unquoted string"), { return false; });
TTS(ReadUnquotedWhitespace, Esep, ReadSkipPre(), { cell.append(s.value); next_cell(); });
TTS(ReadUnquotedWhitespace, Enewline, Start(), { cell.append(s.value); next_cell(); out.end_row(); });
TTS(ReadUnquotedWhitespace, Ewhitespace, self, { s.value.push_back(e.value); });
TTS(ReadUnquotedWhitespace, Echar, ReadUnquoted(), { cell.append(s.value); add(e); });
TTS(ReadError, Echar, self, { return false; });
private:
inline void add(const Echar &e) { cell.push_back(e.value); }
inline void next_cell(bool has_content=true) {
if (has_content) {
out.cell(cell.c_str(),cell.size());
} else {
assert(cell.empty());
out.cell(NULL,0);
}
cell.clear();
}
private:
csv_builder &out;
std::string cell;
};
namespace detail {
template <class StateVariant,class Event,class Transitions>
struct NextState : boost::static_visitor<bool> {
NextState(StateVariant &v,const Event &e,Transitions &t) : v(v),e(e),t(t) {}
template <class State>
bool operator()(State &s) const {
#ifdef DEBUG
printf("%s %c\n",typeid(State).name(),e.value);
#endif
return t.on(v,s,e);
}
// bool operator()(...) const { return false; }
private:
StateVariant &v;
const Event &e;
Transitions &t;
};
} // namespace detail
#ifdef CPP11
template <class Transitions=Trans,class StateVariant,class Event>
bool next(StateVariant &state,Event &&event,Transitions &&trans=Transitions()) {
return boost::apply_visitor(detail::NextState<StateVariant,Event,Transitions>(state,std::forward<Event>(event),std::forward<Transitions>(trans)),state);
}
#else
template <class Transitions,class StateVariant,class Event>
bool next(StateVariant &state,const Event &event,const Transitions &trans=Transitions()) {
return boost::apply_visitor(detail::NextState<StateVariant,Event,Transitions>(state,event,trans),state);
}
template <class Transitions,class StateVariant,class Event>
bool next(StateVariant &state,const Event &event,Transitions &trans) {
return boost::apply_visitor(detail::NextState<StateVariant,Event,Transitions>(state,event,trans),state);
}
#endif
} // namespace csvFSM
// TODO?
bool csvparser::operator()(const std::string &line) // {{{
{
const char *buf=line.c_str();
return (operator())(buf,line.size());
}
// }}}
bool csvparser::operator()(const char *&buf,int len) // {{{
{
csvFSM::States state;
csvFSM::Trans trans(out);
while (len>0) {
bool run=true;
if (*buf==qchar) {
run=csvFSM::next(state,csvFSM::Eqchar(*buf),trans);
} else if (*buf==sep) {
run=csvFSM::next(state,csvFSM::Esep(*buf),trans);
} else if (*buf==' ') { // TODO? more (but DO NOT collide with sep=='\t')
run=csvFSM::next(state,csvFSM::Ewhitespace(*buf),trans);
} else if (*buf=='\n') {
run=csvFSM::next(state,csvFSM::Enewline(*buf),trans);
} else {
run=csvFSM::next(state,csvFSM::Echar(*buf),trans);
}
if (!run) {
csvFSM::ReadError *err=boost::get<csvFSM::ReadError>(&state);
if (err) {
#ifdef DEBUG
fprintf(stderr,"csv parse error: %s\n",err->type);
#endif
errmsg=err->type;
}
return true;
}
buf++;
len--;
}
return false;
}
// }}}