-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathcsv_parser.c
362 lines (315 loc) · 10.2 KB
/
csv_parser.c
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
Copyright (C) CFEngine AS
This file is part of CFEngine 3 - written and maintained by CFEngine AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <csv_parser.h>
#include <alloc.h>
#include <writer.h>
typedef enum
{
CSV_ST_NEW_LINE,
CSV_ST_PRE_START_SPACE,
CSV_ST_NO_QUOTE_MODE,
CSV_ST_SEPARATOR,
CSV_ST_LEADING_QUOTE,
CSV_ST_INTERNAL_QUOTE,
CSV_ST_WITH_QUOTE_MODE,
CSV_ST_SPACE_AFTER_QUOTE,
CSV_ST_ERROR,
CSV_ST_CLOSED
} csv_state;
#define CSVCL_BLANK(x) (((x)==' ')||((x)=='\t')||((x)=='\n')||((x)=='\r'))
#define CSVCL_QUOTE(x) (((x)=='"'))
#define CSVCL_SEP(x) (((x)==','))
#define CSVCL_EOL(x) (((x)=='\0'))
#define CSVCL_ANY1(x) ((!CSVCL_BLANK(x))&&(!CSVCL_QUOTE(x))&&(!CSVCL_SEP(x)))
#define CSVCL_ANY2(x) ((!CSVCL_BLANK(x))&&(!CSVCL_QUOTE(x))&&(!CSVCL_SEP(x)))
#define CSVCL_ANY3(x) ((!CSVCL_QUOTE(x))&&(!CSVCL_SEP(x)))
#define CSVCL_ANY4(x) ((!CSVCL_BLANK(x))&&(!CSVCL_QUOTE(x))&&(!CSVCL_SEP(x)))
#define CSVCL_ANY5(x) ((!CSVCL_QUOTE(x)))
#define CSVCL_ANY6(x) ((!CSVCL_BLANK(x))&&(!CSVCL_QUOTE(x))&&(!CSVCL_SEP(x)))
#define CSVCL_ANY7(x) ((!CSVCL_QUOTE(x)))
#define CSVCL_ANY8(x) ((!CSVCL_BLANK(x))&&(!CSVCL_SEP(x)))
typedef enum {
CSV_ERR_OK,
CVS_ERR_MALFORMED,
CSV_ERR_UNKNOWN_STATE,
CSV_ERR_UNEXPECTED_END,
CSV_ERR_INVALID_INPUT
} csv_parser_error;
/**
@brief parse CSV-formatted line and put its content in a list
@param[in] str: is the CSV string to parse
@param[out] newlist: list of elements found
@retval 0: successful, <>0: failed
*/
static csv_parser_error LaunchCsvAutomata(const char *str, Seq **newlist)
{
assert(str);
if (str == NULL)
{
return CSV_ERR_INVALID_INPUT;
}
char *snatched = xmalloc(strlen(str) + 1);
snatched[0] = '\0';
char *sn = snatched;
csv_parser_error ret;
csv_state current_state = CSV_ST_NEW_LINE; /* initial state */
const char *s;
for (s = str; *s != '\0'; s++)
{
switch(current_state) {
case CSV_ST_ERROR:
ret = CVS_ERR_MALFORMED;
goto clean;
case CSV_ST_NEW_LINE:
if (CSVCL_SEP(*s))
{
*sn = '\0'; sn = NULL;
SeqAppend(*newlist, xstrdup(""));
current_state = CSV_ST_SEPARATOR;
}
else if (CSVCL_BLANK(*s))
{
*sn = *s; sn++;
current_state = CSV_ST_PRE_START_SPACE;
}
else if (CSVCL_QUOTE(*s))
{
snatched[0] = '\0'; sn = NULL;
current_state = CSV_ST_LEADING_QUOTE;
}
else if (CSVCL_ANY1(*s))
{
*sn = *s; sn++;
current_state = CSV_ST_NO_QUOTE_MODE;
}
break;
case CSV_ST_PRE_START_SPACE:
if (CSVCL_SEP(*s))
{
*sn = '\0'; sn = NULL;
SeqAppend(*newlist, xstrdup(snatched));
snatched[0] = '\0';
current_state = CSV_ST_SEPARATOR;
}
else if (CSVCL_BLANK(*s))
{
*sn = *s; sn++;
current_state = CSV_ST_PRE_START_SPACE;
}
else if (CSVCL_QUOTE(*s))
{
snatched[0] = '\0'; sn = NULL;
current_state = CSV_ST_LEADING_QUOTE;
}
else if (CSVCL_ANY2(*s))
{
*sn = *s; sn++;
current_state = CSV_ST_NO_QUOTE_MODE;
}
break;
case CSV_ST_NO_QUOTE_MODE:
if (CSVCL_SEP(*s))
{
*sn = '\0'; sn = NULL;
SeqAppend(*newlist, xstrdup(snatched));
snatched[0] = '\0';
current_state = CSV_ST_SEPARATOR;
}
else if (CSVCL_QUOTE(*s))
{
snatched[0] = '\0'; sn = NULL;
current_state = CSV_ST_ERROR;
}
else if (CSVCL_ANY3(*s))
{
*sn = *s; sn++;
current_state = CSV_ST_NO_QUOTE_MODE;
}
break;
case CSV_ST_SEPARATOR:
if (CSVCL_SEP(*s))
{
snatched[0] = '\0'; sn = NULL;
SeqAppend(*newlist, xstrdup(snatched));
current_state = CSV_ST_SEPARATOR;
}
else if (CSVCL_BLANK(*s))
{
sn = snatched; *sn = *s; sn++;
current_state = CSV_ST_PRE_START_SPACE;
}
else if (CSVCL_QUOTE(*s))
{
snatched[0] = '\0'; sn = NULL;
current_state = CSV_ST_LEADING_QUOTE;
}
else if (CSVCL_ANY4(*s))
{
sn = snatched; *sn = *s; sn++;
current_state = CSV_ST_NO_QUOTE_MODE;
}
break;
case CSV_ST_LEADING_QUOTE:
if (CSVCL_QUOTE(*s))
{
sn = snatched;
current_state = CSV_ST_INTERNAL_QUOTE;
}
else if (CSVCL_ANY5(*s))
{
sn = snatched;
*sn = *s; sn++;
current_state = CSV_ST_WITH_QUOTE_MODE;
}
break;
case CSV_ST_INTERNAL_QUOTE:
if (CSVCL_SEP(*s))
{
*sn = '\0'; sn = NULL;
SeqAppend(*newlist, xstrdup(snatched));
current_state = CSV_ST_SEPARATOR;
}
else if (CSVCL_BLANK(*s))
{
current_state = CSV_ST_SPACE_AFTER_QUOTE;
}
else if (CSVCL_QUOTE(*s))
{
*sn = *s; sn++;
current_state = CSV_ST_WITH_QUOTE_MODE;
}
else if (CSVCL_ANY6(*s))
{
snatched[0] = '\0'; sn++;
current_state = CSV_ST_ERROR;
}
break;
case CSV_ST_WITH_QUOTE_MODE:
if (CSVCL_QUOTE(*s))
{
current_state = CSV_ST_INTERNAL_QUOTE;
}
else if (CSVCL_ANY7(*s))
{
*sn = *s; sn++;
current_state = CSV_ST_WITH_QUOTE_MODE;
}
break;
case CSV_ST_SPACE_AFTER_QUOTE:
if (CSVCL_SEP(*s))
{
sn = NULL;
SeqAppend(*newlist, xstrdup(snatched));
current_state = CSV_ST_SEPARATOR;
}
else if (CSVCL_BLANK(*s))
{
if (sn != NULL)
{
*sn = '\0';
}
sn = NULL;
current_state = CSV_ST_SPACE_AFTER_QUOTE;
}
else if (CSVCL_ANY8(*s))
{
snatched[0] = '\0'; sn = NULL;
current_state = CSV_ST_ERROR;
}
break;
default:
ret = CSV_ERR_UNKNOWN_STATE;
goto clean;
}
}
assert(*s == '\0');
if (current_state != CSV_ST_LEADING_QUOTE &&
current_state != CSV_ST_WITH_QUOTE_MODE )
{
if (sn != NULL)
{
*sn = *s; /* write the trailing '\0' */
sn = NULL;
}
/* Trim trailing CRLF. */
if(current_state == CSV_ST_NO_QUOTE_MODE ||
current_state == CSV_ST_PRE_START_SPACE)
{
int len = strlen(snatched);
if (len > 1 && snatched[len - 2] == '\r' && snatched[len - 1] == '\n')
{
snatched[len - 2] = '\0';
}
}
SeqAppend(*newlist, xstrdup(snatched));
snatched[0] = '\0';
}
else /* LEADING_QUOTE or WITH_QUOTE */
{
ret = CSV_ERR_UNEXPECTED_END;
goto clean;
}
free(snatched);
return CSV_ERR_OK;
clean:
if (newlist)
{
SeqDestroy(*newlist);
}
free(snatched);
return ret;
}
Seq *SeqParseCsvString(const char *string)
{
Seq *newlist = SeqNew(16, free);
if (LaunchCsvAutomata(string, &newlist) != CSV_ERR_OK)
{
return NULL;
}
return newlist;
}
char *GetCsvLineNext(FILE *fp)
{
if (!fp)
{
return NULL;
}
Writer *buffer = StringWriter();
char prev = 0;
for (;;)
{
int current = fgetc(fp);
if (current == EOF || feof(fp))
{
break;
}
WriterWriteChar(buffer, current);
if ((current == '\n') && (prev == '\r'))
{
break;
}
prev = current;
}
if (StringWriterLength(buffer) <= 0)
{
WriterClose(buffer);
return NULL;
}
return StringWriterClose(buffer);
}