- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsvparser.cpp
More file actions
Latest commit
209 lines (184 loc) · 7.07 KB
/
Copy pathcsvparser.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
namespacecsvFSM {
// States
structStart {};
structReadSkipPre {};
structReadQuoted {};
structReadQuotedCheckEscape {};
structReadQuotedSkipPost {};
structReadUnquoted {};
structReadUnquotedWhitespace {
ReadUnquotedWhitespace(unsignedchar value) : value(1,value) {}
std::string value;
};
structReadError {
ReadError(constchar *type) : type(type) {} // i.e. for static strings
constchar *type;
};
typedef boost::variant<Start,
ReadSkipPre,
ReadQuoted,
ReadQuotedCheckEscape,
ReadQuotedSkipPost,
ReadUnquoted,
ReadUnquotedWhitespace,
ReadError> States;
// Events
structEchar { // and fallback
Echar(unsignedchar value) : value(value) {}
unsignedchar value;
};
structEwhitespace : Echar {
Ewhitespace(unsignedchar value) : Echar(value) {}
};
structEqchar : Echar {
Eqchar(unsignedchar value) : Echar(value) {}
};
structEsep : Echar {
Esep(unsignedchar value) : Echar(value) {}
};
structEnewline : Echar {
Enewline(unsignedchar value) : Echar(value) {}
};
#defineTTS(S,E,Snew,code) boolon(States &self,S &s,const E &e) { code; self=Snew; returntrue; }
structTrans {
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"), { returnfalse; });
// 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"), { returnfalse; });
TTS(ReadUnquoted, Eqchar, ReadError("unexpected quote in unquoted string"), { returnfalse; });
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"), { returnfalse; });
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, { returnfalse; });
private:
inlinevoidadd(const Echar &e) { cell.push_back(e.value); }
inlinevoidnext_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;
};
namespacedetail {
template <classStateVariant,classEvent,classTransitions>
structNextState : boost::static_visitor<bool> {
NextState(StateVariant &v,const Event &e,Transitions &t) : v(v),e(e),t(t) {}
template <classState>
booloperator()(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 <classTransitions=Trans,classStateVariant,classEvent>
boolnext(StateVariant &state,Event &&event,Transitions &&trans=Transitions()) {
returnboost::apply_visitor(detail::NextState<StateVariant,Event,Transitions>(state,std::forward<Event>(event),std::forward<Transitions>(trans)),state);
}
#else
template <classTransitions,classStateVariant,classEvent>
boolnext(StateVariant &state,const Event &event,const Transitions &trans=Transitions()) {
returnboost::apply_visitor(detail::NextState<StateVariant,Event,Transitions>(state,event,trans),state);
}
template <classTransitions,classStateVariant,classEvent>
boolnext(StateVariant &state,const Event &event,Transitions &trans) {
returnboost::apply_visitor(detail::NextState<StateVariant,Event,Transitions>(state,event,trans),state);
}
#endif
} // namespace csvFSM
// TODO?
boolcsvparser::operator()(const std::string &line) // {{{
{
constchar *buf=line.c_str();
return (operator())(buf,line.size());
}
// }}}
boolcsvparser::operator()(constchar *&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);
} elseif (*buf==sep) {
run=csvFSM::next(state,csvFSM::Esep(*buf),trans);
} elseif (*buf=='') { // TODO? more (but DO NOT collide with sep=='\t')
run=csvFSM::next(state,csvFSM::Ewhitespace(*buf),trans);
} elseif (*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;
}
returntrue;
}
buf++;
len--;
}
returnfalse;
}
// }}}