-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcsv.c
427 lines (361 loc) · 8.44 KB
/
csv.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
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
/*
* A set of functions for the manipulation of RFC 4180 style
* Comma Separated Values (CSV) files.
*
* See csv.h for more info
*
* This is free and unencumbered software released into the public domain.
* http://unlicense.org/
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include "csv.h"
#include "utils.h"
#ifdef _MSC_VER
/* For Visual C++ 6.0
* (I don't know about newer versions yet)
*/
# define vsnprintf _vsnprintf
#endif
/* The size of the internal buffer used by csv_setx() */
#define CSV_SETX_BUFFER_SIZE 512
/*
* Trim whitespace at thestart and end of fields?
* This does not apply to quoted fields
*/
#define TRIM_SPACES 0
/* Various error codes */
#define ER_OK 1
#define ER_MEM_FAIL -1
#define ER_IOW_FAIL -2
#define ER_IOR_FAIL -3
#define ER_INV_PARAM -4
#define ER_EXPECTED_EOS -5
#define ER_BAD_QUOTEEND -6
static int csv_set_int(csv_file *csv, int row, int col, char *value);
const char *csv_errstr(int err)
{
switch(err)
{
case ER_OK: return "Success";
case ER_MEM_FAIL: return "Out of memory";
case ER_IOW_FAIL: return "Unable to open file for writing";
case ER_IOR_FAIL: return "Unable to read file";
case ER_INV_PARAM: return "Invalid parameter";
case ER_EXPECTED_EOS : return "Unterminated string";
case ER_BAD_QUOTEEND : return "Expected a field or record separator after the \"";
}
return "Unknown";
}
/* Allocates memory for a CSV file structure */
csv_file *csv_create(int def_rows, int def_cols)
{
csv_file *csv;
int i, j;
if(def_rows <= 0)
def_rows = CSV_DEFAULT_ROWS;
if(def_cols <= 0)
def_cols = CSV_DEFAULT_COLS;
csv = malloc(sizeof *csv);
if(!csv) return NULL;
csv->rows = calloc(def_rows, sizeof *csv->rows);
if(!csv->rows)
{
free(csv);
return NULL;
}
csv->arows = def_rows;
csv->nrows = 0;
csv->def_cols = def_cols;
for(i = 0; i < csv->arows; i++)
{
csv->rows[i].cols = calloc(def_cols, sizeof *csv->rows[i].cols);
if(!csv->rows[i].cols)
{
for(j = 0; j < i; j++) free(csv->rows[j].cols);
free(csv->rows);
free(csv);
return NULL;
}
csv->rows[i].acols = def_cols;
csv->rows[i].ncols = 0;
}
return csv;
}
/* Deallocs memory allocated by csv_create() */
void csv_free(csv_file *csv)
{
int r,c;
for(r = 0; r < csv->arows; r++)
{
for(c = 0; c < csv->rows[r].acols; c++)
if(csv->rows[r].cols[c])
free(csv->rows[r].cols[c]);
free(csv->rows[r].cols);
}
free(csv->rows);
free(csv);
}
#define RETERR(code) do{if(err)*err = code;return NULL;}while(0)
#define ERREND(code) do{if(err)*err = code;goto error;}while(0)
/* loads a CSV file from disk */
csv_file *csv_load(const char *filename, int *err, int *line)
{
csv_file *csv;
char *buffer, *p;
int r = 0, c = 0;
if(err) *err = ER_OK;
if(line) *line = 1;
if(!filename)
RETERR(ER_INV_PARAM);
csv = csv_create(0,0);
if(!csv)
RETERR(ER_MEM_FAIL);
/* My approach is to read the entire file into memory and then
* parse it from there.
* I Acknowledge that this is not the most efficient way of doing it,
* but it does simplify the parsing somewhat.
*/
buffer = my_readfile(filename);
if(!buffer)
ERREND(ER_IOR_FAIL);
for(p = buffer; *p;)
{
if(strchr(" \t",p[0]))
{
/* This is to prevent space between the start of a field
and a " from confusing the parser */
char *q;
for(q = p + 1; strchr(" \t",q[0]); q++);
if(q[0] == '\"')
p = q;
}
if(!strncmp(p,"\r\n", 2))
{
/* official line endings */
r++;
c = 0;
p+=2;
if(line) (*line)++;
}
else if(strchr("\r\n",p[0]))
{
/* alternative line endings */
r++;
c = 0;
p++;
if(line) (*line)++;
}
else if(*p == ',')
{
/* A new or empty field */
c++;
p++;
}
else if(*p == '\"')
{
/* A quoted field */
char *q = p, *s, *t;
do {
q++;
if(*q == '\0')
ERREND(ER_EXPECTED_EOS);
else if(q[0] == '\"' && q[1] == '\"')
q+=2;
} while(*q != '\"');
s = malloc(q - p);
if(!s)
ERREND(ER_MEM_FAIL);
t = s;
p++;
while(p < q)
{
if(*p == '\"' && *(p+1) == '\"')
{
*(t++) = *(p++);
p++;
}
else
*(t++) = *(p++);
}
*t = '\0';
/* Skip any whitespace after the closing quote */
for(p++; p[0] && !strchr(",\r\n",p[0]);p++)
if(!strchr(" \t",p[0]))
ERREND(ER_BAD_QUOTEEND);
csv_set_int(csv, r, c, s);
}
else
{
/* A normal field */
char *q, *s, *t;
#if TRIM_SPACES
/* Trim leading whitespace */
while(p[0] && strchr(" \t",p[0])) p++;
#endif
for(q = p;q[0] && !strchr(",\r\n",q[0]); q++);
s = malloc(q - p + 1);
if(!s)
ERREND(ER_MEM_FAIL);
t = s;
while(p < q)
{
*(t++) = *(p++);
}
*t = '\0';
#if TRIM_SPACES
/* Trim trailing whitespace */
while(t > s && strchr(" \t",t[-1]))
*(--t) = '\0';
#endif
csv_set_int(csv, r, c, s);
}
}
free(buffer);
return csv;
error:
csv_free(csv);
return NULL;
}
/* Saves a CSV file to disk */
int csv_save(csv_file *csv, const char *filename)
{
int r, c;
FILE *f = stdout;
if(!csv || !filename) return ER_INV_PARAM;
f = fopen(filename, "w");
if(!f) return ER_IOW_FAIL;
for(r = 0; r < csv->nrows; r++)
{
for(c = 0; c < csv->rows[r].ncols; c++)
{
const char *cell = csv->rows[r].cols[c];
if(cell)
{
if(strpbrk(cell, ",\"\r\n"))
{
const char *p;
fputc('\"',f);
for(p = cell; *p; p++)
{
if(*p == '\"')
fputc('\"', f);
fputc(*p, f);
}
fputc('\"',f);
}
else
fputs(cell, f);
}
if(c < csv->rows[r].ncols - 1)
fputc(',',f);
}
fputs(CSV_LINE_TERMINATOR,f);
}
return 1;
}
/* Returns the number of rows in a CSV file */
int csv_rowcount(csv_file *csv)
{
if(!csv) return 0;
return csv->nrows;
}
/* Returns the number of columns in a CSV file */
int csv_colcount(csv_file *csv, int row)
{
if(!csv || row >= csv->nrows) return 0;
return csv->rows[row].ncols;
}
/* Retrieves the value of a cell */
const char *csv_get(csv_file *csv, int row, int col)
{
if(!csv || row >= csv->nrows || col >= csv->rows[row].ncols || row < 0 || col < 0 || !csv->rows[row].cols[col])
return "";
return csv->rows[row].cols[col];
}
/* Sets the value at cell [row,col] in the csv_file (internal function).
* This function is used internally because it assumes this module is already
* the owner of 'value' - you'll notice that csv_set() just calls this function
* with a strdup()ed value.
*/
static int csv_set_int(csv_file *csv, int row, int col, char *value)
{
csv_row *rp;
if(!csv || row < 0 || col < 0) return ER_INV_PARAM;
if(row >= csv->nrows)
{
if(row >= csv->arows)
{
/* Need to allocate more rows */
int i, ns = MY_MAX(row+1,csv->arows + (csv->arows >> 1));
void *op = csv->rows;
if(ns <= 2) ns = 3;
csv->rows = realloc(csv->rows, ns * sizeof *csv->rows);
if(!csv->rows)
{
csv->rows = op;
return ER_MEM_FAIL;
}
/* Initialise the new rows */
for(i = csv->arows; i < ns; i++)
{
csv->rows[i].cols = calloc(csv->def_cols, sizeof *csv->rows[i].cols);
if(!csv->rows[i].cols)
return ER_MEM_FAIL;
csv->rows[i].acols = csv->def_cols;
csv->rows[i].ncols = 0;
}
csv->arows = ns;
}
csv->nrows = row + 1;
assert(csv->nrows <= csv->arows);
}
assert(row < csv->nrows);
rp = &csv->rows[row];
if(col >= rp->ncols)
{
if(col >= rp->acols)
{
/* Need to reallocate the columns */
int i, ns = MY_MAX(col+1, rp->acols + (rp->acols >> 1));
void *op = rp->cols;
if(ns <= 2) ns = 3;
rp->cols = realloc(rp->cols, ns * sizeof *rp->cols);
if(!rp->cols)
{
rp->cols = op;
return ER_MEM_FAIL;
}
for(i = rp->acols; i < ns; i++)
rp->cols[i] = NULL;
rp->acols = ns;
}
rp->ncols = col + 1;
assert(rp->ncols <= rp->acols);
}
assert(col < rp->ncols);
if(rp->cols[col])
free(rp->cols[col]);
rp->cols[col] = value;
if(rp->cols[col])
return ER_OK;
return ER_MEM_FAIL;
}
/* Sets the value at cell [row,col] in the csv_file */
int csv_set(csv_file *csv, int row, int col, const char *value)
{
return csv_set_int(csv, row, col, my_strdup(value));
}
/* Sets a specific cell in the CSV file to a value with a printf()-style format */
int csv_setx(csv_file *csv, int row, int col, const char *fmt, ...)
{
char buffer[CSV_SETX_BUFFER_SIZE];
va_list arg;
va_start (arg, fmt);
vsnprintf (buffer, CSV_SETX_BUFFER_SIZE, fmt, arg);
va_end (arg);
return csv_set(csv, row, col, buffer);
}