-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv_writer.c
More file actions
389 lines (305 loc) · 12.6 KB
/
csv_writer.c
File metadata and controls
389 lines (305 loc) · 12.6 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
#include "csv_writer.h"
#include "csv_utils.h"
#include <string.h>
static const unsigned char UTF8_BOM[] = {0xEF, 0xBB, 0xBF};
static const unsigned char UTF16LE_BOM[] = {0xFF, 0xFE};
static const unsigned char UTF16BE_BOM[] = {0xFE, 0xFF};
static const unsigned char UTF32LE_BOM[] = {0xFF, 0xFE, 0x00, 0x00};
static const unsigned char UTF32BE_BOM[] = {0x00, 0x00, 0xFE, 0xFF};
const char* csv_writer_error_string(CSVWriterResult result) {
switch (result) {
case CSV_WRITER_OK: return "Success";
case CSV_WRITER_ERROR_NULL_POINTER: return "Null pointer error";
case CSV_WRITER_ERROR_MEMORY_ALLOCATION: return "Memory allocation failed";
case CSV_WRITER_ERROR_FILE_OPEN: return "Failed to open file";
case CSV_WRITER_ERROR_FILE_WRITE: return "Failed to write to file";
case CSV_WRITER_ERROR_INVALID_FIELD_COUNT: return "Invalid field count";
case CSV_WRITER_ERROR_FIELD_NOT_FOUND: return "Field not found";
case CSV_WRITER_ERROR_BUFFER_OVERFLOW: return "Buffer overflow";
case CSV_WRITER_ERROR_ENCODING: return "Encoding error";
default: return "Unknown error";
}
}
static CSVWriterResult write_bom(FILE *file, CSVEncoding encoding) {
const unsigned char *bom = NULL;
size_t bom_size = 0;
switch (encoding) {
case CSV_ENCODING_UTF8:
bom = UTF8_BOM;
bom_size = sizeof(UTF8_BOM);
break;
case CSV_ENCODING_UTF16LE:
bom = UTF16LE_BOM;
bom_size = sizeof(UTF16LE_BOM);
break;
case CSV_ENCODING_UTF16BE:
bom = UTF16BE_BOM;
bom_size = sizeof(UTF16BE_BOM);
break;
case CSV_ENCODING_UTF32LE:
bom = UTF32LE_BOM;
bom_size = sizeof(UTF32LE_BOM);
break;
case CSV_ENCODING_UTF32BE:
bom = UTF32BE_BOM;
bom_size = sizeof(UTF32BE_BOM);
break;
case CSV_ENCODING_ASCII:
case CSV_ENCODING_LATIN1:
return CSV_WRITER_OK;
default:
return CSV_WRITER_OK;
}
if (bom && fwrite(bom, 1, bom_size, file) != bom_size) {
return CSV_WRITER_ERROR_FILE_WRITE;
}
return CSV_WRITER_OK;
}
static CSVWriterResult validate_writer_params(CSVWriter **writer, CSVConfig *config, Arena *arena) {
if (!writer) return CSV_WRITER_ERROR_NULL_POINTER;
if (!config) return CSV_WRITER_ERROR_NULL_POINTER;
if (!arena) return CSV_WRITER_ERROR_NULL_POINTER;
const char *path = csv_config_get_path(config);
if (!path || path[0] == '\0') return CSV_WRITER_ERROR_NULL_POINTER;
return CSV_WRITER_OK;
}
static CSVWriterResult allocate_writer(CSVWriter **writer, Arena *arena) {
void *ptr;
ArenaResult result = arena_alloc(arena, sizeof(CSVWriter), &ptr);
if (result != ARENA_OK) return CSV_WRITER_ERROR_MEMORY_ALLOCATION;
*writer = (CSVWriter*)ptr;
memset(*writer, 0, sizeof(CSVWriter));
(*writer)->arena = arena;
return CSV_WRITER_OK;
}
static CSVWriterResult copy_headers_to_arena(CSVWriter *writer, char **headers, int header_count) {
if (header_count <= 0) {
writer->headers = NULL;
writer->header_count = 0;
return CSV_WRITER_OK;
}
void *ptr;
ArenaResult result = arena_alloc(writer->arena, header_count * sizeof(char*), &ptr);
if (result != ARENA_OK) return CSV_WRITER_ERROR_MEMORY_ALLOCATION;
writer->headers = (char**)ptr;
for (int i = 0; i < header_count; i++) {
if (!headers[i]) {
writer->headers[i] = arena_strdup(writer->arena, "");
} else {
writer->headers[i] = arena_strdup(writer->arena, headers[i]);
}
if (!writer->headers[i]) return CSV_WRITER_ERROR_MEMORY_ALLOCATION;
}
writer->header_count = header_count;
return CSV_WRITER_OK;
}
CSVWriterResult csv_writer_init(CSVWriter **writer, CSVConfig *config, char **headers, int header_count, Arena *arena) {
CSVWriterResult result = validate_writer_params(writer, config, arena);
if (result != CSV_WRITER_OK) return result;
result = allocate_writer(writer, arena);
if (result != CSV_WRITER_OK) return result;
const char *path = csv_config_get_path(config);
(*writer)->file = fopen(path, "wb");
if (!(*writer)->file) return CSV_WRITER_ERROR_FILE_OPEN;
(*writer)->owns_file = true;
(*writer)->config = csv_config_copy(arena, config);
if (!(*writer)->config) {
fclose((*writer)->file);
return CSV_WRITER_ERROR_MEMORY_ALLOCATION;
}
(*writer)->owns_config = true;
(*writer)->delimiter = csv_config_get_delimiter((*writer)->config);
(*writer)->enclosure = csv_config_get_enclosure((*writer)->config);
(*writer)->escape = csv_config_get_escape((*writer)->config);
if (csv_config_get_write_bom((*writer)->config)) {
result = write_bom((*writer)->file, csv_config_get_encoding((*writer)->config));
if (result != CSV_WRITER_OK) {
if ((*writer)->owns_config) csv_config_free((*writer)->config);
fclose((*writer)->file);
return result;
}
}
result = copy_headers_to_arena(*writer, headers, header_count);
if (result != CSV_WRITER_OK) {
if ((*writer)->owns_config) csv_config_free((*writer)->config);
fclose((*writer)->file);
return result;
}
if (header_count > 0) {
result = write_headers(*writer, headers, header_count);
if (result != CSV_WRITER_OK) {
if ((*writer)->owns_config) csv_config_free((*writer)->config);
fclose((*writer)->file);
return result;
}
}
return CSV_WRITER_OK;
}
CSVWriterResult csv_writer_init_with_file(CSVWriter **writer, FILE *file, CSVConfig *config, char **headers, int header_count, Arena *arena) {
if (!writer || !file || !config || !arena) return CSV_WRITER_ERROR_NULL_POINTER;
CSVWriterResult result = allocate_writer(writer, arena);
if (result != CSV_WRITER_OK) return result;
(*writer)->file = file;
(*writer)->owns_file = false;
(*writer)->config = config;
(*writer)->owns_config = false;
(*writer)->delimiter = csv_config_get_delimiter((*writer)->config);
(*writer)->enclosure = csv_config_get_enclosure((*writer)->config);
(*writer)->escape = csv_config_get_escape((*writer)->config);
if (csv_config_get_write_bom((*writer)->config)) {
result = write_bom((*writer)->file, csv_config_get_encoding((*writer)->config));
if (result != CSV_WRITER_OK) return result;
}
result = copy_headers_to_arena(*writer, headers, header_count);
if (result != CSV_WRITER_OK) return result;
if (header_count > 0) {
result = write_headers(*writer, headers, header_count);
if (result != CSV_WRITER_OK) return result;
}
return CSV_WRITER_OK;
}
bool is_numeric_field(const char *field) {
if (!field || strlen(field) == 0) return false;
const char *p = field;
while (*p == ' ' || *p == '\t') p++;
if (*p == '+' || *p == '-') p++;
bool has_digits = false;
while (*p >= '0' && *p <= '9') {
has_digits = true;
p++;
}
if (*p == '.') {
p++;
while (*p >= '0' && *p <= '9') {
has_digits = true;
p++;
}
}
while (*p == ' ' || *p == '\t') p++;
return has_digits && *p == '\0';
}
bool field_needs_quoting(const char *field, char delimiter, char enclosure, bool strictMode) {
if (!field) return false;
for (const char *p = field; *p; p++) {
if (*p == delimiter || *p == enclosure || *p == '\n' || *p == '\r') {
return true;
}
}
if (strictMode) {
for (const char *p = field; *p; p++) {
if (*p == ' ') {
return true;
}
}
}
return false;
}
CSVWriterResult write_field(FILE *file, const FieldWriteOptions *options) {
if (!file || !options) return CSV_WRITER_ERROR_NULL_POINTER;
const char *field = options->field ? options->field : "";
bool needs_quoting = field_needs_quoting(field, options->delimiter, options->enclosure, options->strictMode);
if (needs_quoting || options->needs_quoting) {
if (fputc(options->enclosure, file) == EOF) return CSV_WRITER_ERROR_FILE_WRITE;
for (const char *p = field; *p; p++) {
if (*p == options->enclosure) {
if (fputc(options->enclosure, file) == EOF) return CSV_WRITER_ERROR_FILE_WRITE;
if (fputc(options->enclosure, file) == EOF) return CSV_WRITER_ERROR_FILE_WRITE;
} else {
if (fputc(*p, file) == EOF) return CSV_WRITER_ERROR_FILE_WRITE;
}
}
if (fputc(options->enclosure, file) == EOF) return CSV_WRITER_ERROR_FILE_WRITE;
} else {
if (fputs(field, file) == EOF) return CSV_WRITER_ERROR_FILE_WRITE;
}
return CSV_WRITER_OK;
}
CSVWriterResult write_headers(CSVWriter *writer, char **headers, int header_count) {
if (!writer || !headers || header_count <= 0) {
return CSV_WRITER_ERROR_NULL_POINTER;
}
FieldWriteOptions options = {
.delimiter = writer->config->delimiter,
.enclosure = writer->config->enclosure,
.escape = writer->config->escape,
.strictMode = csv_config_get_strict_mode(writer->config)
};
for (int i = 0; i < header_count; i++) {
if (i > 0) {
if (fputc(writer->delimiter, writer->file) == EOF) {
return CSV_WRITER_ERROR_FILE_WRITE;
}
}
options.field = headers[i];
options.needs_quoting = false;
CSVWriterResult result = write_field(writer->file, &options);
if (result != CSV_WRITER_OK) return result;
}
if (fprintf(writer->file, "\n") < 0) return CSV_WRITER_ERROR_FILE_WRITE;
if (csv_config_get_auto_flush(writer->config)) {
if (fflush(writer->file) != 0) return CSV_WRITER_ERROR_FILE_WRITE;
}
return CSV_WRITER_OK;
}
CSVWriterResult csv_writer_write_record(CSVWriter *writer, char **fields, int field_count) {
if (!writer || !fields || field_count <= 0) {
return CSV_WRITER_ERROR_NULL_POINTER;
}
FieldWriteOptions options = {
.delimiter = writer->config->delimiter,
.enclosure = writer->config->enclosure,
.escape = writer->config->escape,
.strictMode = csv_config_get_strict_mode(writer->config)
};
for (int i = 0; i < field_count; i++) {
if (i > 0) {
if (fputc(writer->delimiter, writer->file) == EOF) {
return CSV_WRITER_ERROR_FILE_WRITE;
}
}
options.field = fields[i];
options.needs_quoting = false;
CSVWriterResult result = write_field(writer->file, &options);
if (result != CSV_WRITER_OK) return result;
}
if (fprintf(writer->file, "\n") < 0) return CSV_WRITER_ERROR_FILE_WRITE;
if (csv_config_get_auto_flush(writer->config)) {
if (fflush(writer->file) != 0) return CSV_WRITER_ERROR_FILE_WRITE;
}
return CSV_WRITER_OK;
}
CSVWriterResult csv_writer_write_record_map(CSVWriter *writer, char **field_names, char **field_values, int field_count) {
if (!writer || !writer->file) return CSV_WRITER_ERROR_NULL_POINTER;
if (!field_names || !field_values) return CSV_WRITER_ERROR_NULL_POINTER;
if (writer->header_count <= 0) return CSV_WRITER_ERROR_INVALID_FIELD_COUNT;
if (writer->header_count > MAX_FIELDS) return CSV_WRITER_ERROR_BUFFER_OVERFLOW;
char *ordered_fields[MAX_FIELDS];
for (int i = 0; i < writer->header_count; i++) {
ordered_fields[i] = NULL;
}
for (int i = 0; i < field_count; i++) {
if (!field_names[i]) continue;
for (int j = 0; j < writer->header_count; j++) {
if (writer->headers[j] && strcmp(field_names[i], writer->headers[j]) == 0) {
ordered_fields[j] = field_values[i];
break;
}
}
}
return csv_writer_write_record(writer, ordered_fields, writer->header_count);
}
CSVWriterResult csv_writer_flush(CSVWriter *writer) {
if (!writer || !writer->file) return CSV_WRITER_ERROR_NULL_POINTER;
if (fflush(writer->file) != 0) return CSV_WRITER_ERROR_FILE_WRITE;
return CSV_WRITER_OK;
}
void csv_writer_free(CSVWriter *writer) {
if (!writer) return;
if (writer->file && writer->owns_file) {
fflush(writer->file);
fclose(writer->file);
}
if (writer->config && writer->owns_config) {
csv_config_free(writer->config);
}
}