-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvn.h
More file actions
446 lines (314 loc) · 8.44 KB
/
csvn.h
File metadata and controls
446 lines (314 loc) · 8.44 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
Copyright 2023 Mahir Salihbasic
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef CSVN_H
#define CSVN_H
#include <stddef.h>
#define CSVN_NEWLINE '\n'
/*
While this is a CSV parser, it technically could support other
delimeters like tabulators.
Changing the char to another character should be enough,
however there are no guarantees that the parser will work fine afterwards.
*/
#define CSVN_DELIM ','
#ifdef __cplusplus
extern "C" {
#endif
/*
NOT_ENOUGH_MEM - not enough tokens were allocated
INVALID_CHARACTER - an invalid character was encountered
(check parser position for further analysis)
*/
enum csv_err {
NOT_ENOUGH_MEM = -1,
INVALID_CHARACTER = -2
} csv_err;
/*
UNASSIGN - no field has been assigned to this token
DQUOTE - this token holds a quoted field
TEXT - this token holds an unquoted field
EMPTY - this token is an empty field
*/
enum csv_tok {
UNASSIGN,
DQUOTE,
TEXT,
EMPTY
} csv_tok;
/*
pos - current parser position in the text
toknext - index of the next token to be allocated
line - current line of text in which the parser operates
*/
struct csv_p {
int pos;
int toknext;
int line;
} csv_p;
/*
start - starting (inclusive) position of the field
end - ending (inclusive) position of the field
line - line in which the field was located
size - exact length of the field (end - start)
token - field type
*/
struct csv_t {
int start;
int end;
int line;
int size;
enum csv_tok token;
} csv_t;
/*
Allocates the next token from the provided token pool and initialises it
to default starting values.
Returns a pointer to the allocated token if the allocation was
successful and NULL otherwise.
*/
static struct csv_t *csvn_allocate_token(struct csv_p *csv_p,
struct csv_t *tokens,
const size_t num_tok);
/*
Fills the provided (non-NULL) token with the provided data.
Refer to csv_t documentation for a brief explanation of parameters.
*/
static void csvn_fill_token(struct csv_t *csv_t,
int start,
int end,
int line,
enum csv_tok token);
static int csvn_parse_quotes(const char *text,
const size_t textlen,
struct csv_p *csv_p,
struct csv_t *tokpool,
const size_t num_tok);
static int csvn_parse_normal(const char *text,
const size_t textlen,
struct csv_p *csv_p,
struct csv_t *tokpool,
const size_t num_tok);
/*
Initialises the provided (non-NULL) parser to default starting values.
This function should also be used to reset the parser in case of
repeated use.
*/
void csvn_init(struct csv_p *csv_p);
/*
Parses the provided text (without exceeding textlen) and stores
the parsed fields in tokens array pointed to by tokpool which contains
at least num_tok tokens.
If tokpool is NULL, it will parse the text, but will not store any
tokens in the tokpool. This is useful when the number of tokens is not
initially known.
Returns the number of parsed fields (always greater than or equal to 0)
or a negative value indicating an error (refer to csv_err).
*/
int csvn_parse(const char *text,
const size_t textlen,
struct csv_p *csv_p,
struct csv_t *tokpool,
const size_t num_tok);
void
csvn_init(struct csv_p *csv_p)
{
csv_p->pos = 0;
csv_p->toknext = 0;
csv_p->line = 1;
}
static struct csv_t *
csvn_allocate_token(struct csv_p *csv_p,
struct csv_t *tokens,
const size_t num_tok)
{
if (tokens == NULL) {
return NULL;
}
if (csv_p->toknext > num_tok) {
return NULL;
}
struct csv_t *token = &tokens[csv_p->toknext++];
token->start = token->end = token->line = token->size = 0;
token->token = UNASSIGN;
return token;
}
static void
csvn_fill_token(struct csv_t *csv_t,
int start,
int end,
int line,
enum csv_tok token)
{
csv_t->start = start;
csv_t->end = end;
csv_t->line = line;
csv_t->size = (end - start);
csv_t->token = token;
}
static int
csvn_parse_quotes(const char *text,
const size_t textlen,
struct csv_p *csv_p,
struct csv_t *tokpool,
const size_t num_tok)
{
struct csv_t *token;
int start, end;
start = csv_p->pos;
int line = csv_p->line;
while (text[csv_p->pos] != '\0' && csv_p->pos < textlen) {
if (text[csv_p->pos] == '\"' && text[csv_p->pos + 1] == '\"') {
csv_p->pos += 2;
continue;
}
if (text[csv_p->pos] == '\"' && text[csv_p->pos + 1] != '\"') {
break;
}
if (text[csv_p->pos] == CSVN_NEWLINE) {
#ifdef CSVN_CONSIDER_NL
line++
#endif
csv_p->line++;
}
csv_p->pos++;
}
end = csv_p->pos - 1; /* curent pos is at the closing quote */
if (tokpool != NULL) {
token = csvn_allocate_token(csv_p, tokpool, num_tok);
if (token == NULL) {
return NOT_ENOUGH_MEM;
}
csvn_fill_token(token, start, end, line, DQUOTE);
}
return 0;
}
static int
csvn_parse_normal(const char *text,
const size_t textlen,
struct csv_p *csv_p,
struct csv_t *tokpool,
const size_t num_tok)
{
struct csv_t *token;
int start, end;
start = csv_p->pos;
int line = csv_p->line;
for (; text[csv_p->pos] != '\0' &&
csv_p->pos < textlen &&
text[csv_p->pos] != CSVN_NEWLINE &&
text[csv_p->pos] != CSVN_DELIM; csv_p->pos++) {
#ifdef CSVN_STRICT
if (text[csv_p->pos] == '\"') {
return INVALID_CHARACTER;
}
#endif
continue;
}
/* let the main loop handle newlines and delimeters */
if (text[csv_p->pos] == CSVN_NEWLINE || text[csv_p->pos] == CSVN_DELIM) {
csv_p->pos--;
}
end = csv_p->pos;
if (tokpool != NULL) {
token = csvn_allocate_token(csv_p, tokpool, num_tok);
if (token == NULL) {
return NOT_ENOUGH_MEM;
}
csvn_fill_token(token, start, end, line, DQUOTE);
}
return 0;
}
int
csvn_parse(const char *text,
const size_t textlen,
struct csv_p *csv_p,
struct csv_t *tokpool,
const size_t num_tok)
{
int parsed = 0;
int res = 0;
while(text[csv_p->pos] != '\0' && csv_p->pos < textlen) {
switch (text[csv_p->pos]) {
case CSVN_NEWLINE:
csv_p->line++;
csv_p->pos++;
break;
case CSVN_DELIM:
csv_p->pos++;
#ifdef CSVN_SKIP_WHITESPACE
if (text[csv_p->pos] == ' ') {
do {
csv_p->pos++;
} while (text[csv_p->pos] == ' ');
}
#endif
if (text[csv_p->pos] == CSVN_DELIM) {
#ifdef CSVN_NO_EMPTY_FIELD
return INVALID_CHARACTER;
#elif CSVN_IGNORE_EMPTY_FIELD
break;
#else
if (tokpool != NULL) {
struct csv_t *tok;
tok = csvn_allocate_token(csv_p, tokpool, num_tok);
if (tok == NULL) {
return NOT_ENOUGH_MEM;
}
csvn_fill_token(tok, csv_p->pos - 1, csv_p->pos, csv_p->line, EMPTY);
}
parsed++;
break;
#endif
}
break;
case '\"':
csv_p->pos++; /* skip opening quote */
res = csvn_parse_quotes(text, textlen, csv_p, tokpool, num_tok);
if (res != 0) {
return res;
}
csv_p->pos++;
#ifdef CSVN_STRICT
/* closing quote has to be followed by a delimiter, newline or end of file */
if (text[csv_p->pos] == CSVN_DELIM || text[csv_p->pos] == CSVN_NEWLINE) {
parsed++;
break;
} else {
if (text[csv_p->pos] != '\0' && csv_p->pos < textlen) {
return INVALID_CHARACTER;
}
}
#endif
parsed++;
break;
default:
res = csvn_parse_normal(text, textlen, csv_p, tokpool, num_tok);
if (res != 0) {
return res;
}
parsed++;
csv_p->pos++;
break;
}
}
return parsed;
}
#ifdef __cplusplus
}
#endif
#endif /* ifndef CSVN_H */