]> begriffs open source - sa-parse/blob - src/csv_parser.c
Prefixed, reentrant CSV parser
[sa-parse] / src / csv_parser.c
1 #include "csv_parser.h"
2 #include "csv.tab.h"
3 #include <stdlib.h>
4 #include <string.h>
5
6 /* External declarations from Bison/Flex - updated for reentrant API */
7 typedef void *yyscan_t;
8 extern int csvlex_init(yyscan_t *scanner);
9 extern int csvlex_destroy(yyscan_t scanner);
10 extern void csvset_in(FILE *file, yyscan_t scanner);
11 extern int csvparse(void *parser_state, yyscan_t scanner);
12
13 /* Global parser state - used to communicate with Bison parser */
14 static csv_document_t *g_current_document = NULL;
15 static csv_parser_t *g_current_parser = NULL;
16 static bool g_parse_error = false;
17 static char g_error_message[256] = "";
18
19 /* CSV parser structure */
20 struct csv_parser {
21     char error_message[256];
22     bool has_error;
23 };
24
25 /* Helper functions */
26 static csv_field_t *convert_field_list(field_node_t *field_nodes);
27 static csv_record_t *create_record_from_fields(field_node_t *field_nodes);
28 static void free_document_internal(csv_document_t *doc);
29
30 /* Functions called by the Bison parser */
31 void csv_parser_add_record(field_node_t *fields);
32 void csv_parser_set_error(const char *error);
33
34 /* Implementation */
35
36 csv_parser_t *csv_parser_create(void)
37 {
38     csv_parser_t *parser = malloc(sizeof(csv_parser_t));
39     if (!parser) {
40         return NULL;
41     }
42     
43     parser->error_message[0] = '\0';
44     parser->has_error = false;
45     
46     return parser;
47 }
48
49 void csv_parser_destroy(csv_parser_t *parser)
50 {
51     if (parser) {
52         free(parser);
53     }
54 }
55
56 csv_error_t csv_parser_parse_string(csv_parser_t *parser, const char *input, csv_document_t **document)
57 {
58     if (!parser || !input || !document) {
59         return CSV_ERROR_INVALID_PARAM;
60     }
61     
62     *document = NULL;
63     
64     /* Create a temporary file from the string input */
65     FILE *temp_file = tmpfile();
66     if (!temp_file) {
67         strncpy(parser->error_message, "Failed to create temporary file", sizeof(parser->error_message) - 1);
68         parser->has_error = true;
69         return CSV_ERROR_IO;
70     }
71     
72     /* Write input to temporary file */
73     if (fputs(input, temp_file) == EOF) {
74         fclose(temp_file);
75         strncpy(parser->error_message, "Failed to write to temporary file", sizeof(parser->error_message) - 1);
76         parser->has_error = true;
77         return CSV_ERROR_IO;
78     }
79     
80     /* Reset file position to beginning */
81     rewind(temp_file);
82     
83     /* Parse from the temporary file */
84     csv_error_t result = csv_parser_parse_file(parser, temp_file, document);
85     fclose(temp_file);
86     
87     return result;
88 }
89
90 csv_error_t csv_parser_parse_file(csv_parser_t *parser, FILE *file, csv_document_t **document)
91 {
92     if (!parser || !file || !document) {
93         return CSV_ERROR_INVALID_PARAM;
94     }
95     
96     *document = NULL;
97     parser->has_error = false;
98     parser->error_message[0] = '\0';
99     
100     /* Initialize global state */
101     g_current_document = malloc(sizeof(csv_document_t));
102     if (!g_current_document) {
103         strncpy(parser->error_message, "Memory allocation failed", sizeof(parser->error_message) - 1);
104         parser->has_error = true;
105         return CSV_ERROR_MEMORY;
106     }
107     
108     g_current_document->records = NULL;
109     g_current_document->record_count = 0;
110     g_current_document->header = NULL;
111     g_current_parser = parser;
112     g_parse_error = false;
113     g_error_message[0] = '\0';
114     
115     /* Initialize the reentrant scanner */
116     yyscan_t scanner;
117     int scanner_init_result = csvlex_init(&scanner);
118     if (scanner_init_result != 0) {
119         free(g_current_document);
120         g_current_document = NULL;
121         return CSV_ERROR_MEMORY;
122     }
123     
124     /* Set up the lexer input */
125     csvset_in(file, scanner);
126     
127     /* Parse the input */
128     int parse_result = csvparse(parser, scanner);
129     
130     /* Clean up lexer state */
131     csvlex_destroy(scanner);
132     
133     /* Check for errors */
134     if (parse_result != 0 || g_parse_error) {
135         if (g_error_message[0] != '\0') {
136             strncpy(parser->error_message, g_error_message, sizeof(parser->error_message) - 1);
137         } else {
138             strncpy(parser->error_message, "Parse error", sizeof(parser->error_message) - 1);
139         }
140         parser->has_error = true;
141         
142         free_document_internal(g_current_document);
143         g_current_document = NULL;
144         g_current_parser = NULL;
145         return CSV_ERROR_PARSE;
146     }
147     
148     /* Success - transfer ownership of document */
149     *document = g_current_document;
150     g_current_document = NULL;
151     g_current_parser = NULL;
152     
153     return CSV_SUCCESS;
154 }
155
156 const char *csv_parser_get_error(csv_parser_t *parser)
157 {
158     if (!parser) {
159         return "Invalid parser";
160     }
161     
162     return parser->has_error ? parser->error_message : "No error";
163 }
164
165 void csv_document_free(csv_document_t *document)
166 {
167     free_document_internal(document);
168 }
169
170 const char *csv_error_string(csv_error_t error)
171 {
172     switch (error) {
173         case CSV_SUCCESS:
174             return "Success";
175         case CSV_ERROR_MEMORY:
176             return "Memory allocation error";
177         case CSV_ERROR_PARSE:
178             return "Parse error";
179         case CSV_ERROR_INVALID_PARAM:
180             return "Invalid parameter";
181         case CSV_ERROR_IO:
182             return "I/O error";
183         default:
184             return "Unknown error";
185     }
186 }
187
188 /* Data access helper functions */
189
190 bool csv_document_has_header(const csv_document_t *document)
191 {
192     return document && document->header != NULL;
193 }
194
195 csv_record_t *csv_document_get_first_record(const csv_document_t *document)
196 {
197     return document ? document->records : NULL;
198 }
199
200 csv_record_t *csv_record_get_next(const csv_record_t *record)
201 {
202     return record ? record->next : NULL;
203 }
204
205 csv_field_t *csv_record_get_first_field(const csv_record_t *record)
206 {
207     return record ? record->fields : NULL;
208 }
209
210 csv_field_t *csv_field_get_next(const csv_field_t *field)
211 {
212     return field ? field->next : NULL;
213 }
214
215 const char *csv_field_get_content(const csv_field_t *field)
216 {
217     return field ? field->content : NULL;
218 }
219
220 /* Internal helper functions */
221
222 static csv_field_t *convert_field_list(field_node_t *field_nodes)
223 {
224     if (!field_nodes) {
225         return NULL;
226     }
227     
228     csv_field_t *first_field = NULL;
229     csv_field_t *last_field = NULL;
230     
231     field_node_t *current_node = field_nodes;
232     while (current_node) {
233         csv_field_t *field = malloc(sizeof(csv_field_t));
234         if (!field) {
235             /* Clean up already allocated fields */
236             while (first_field) {
237                 csv_field_t *next = first_field->next;
238                 free(first_field->content);
239                 free(first_field);
240                 first_field = next;
241             }
242             return NULL;
243         }
244         
245         /* Copy content (take ownership) */
246         field->content = current_node->content;
247         current_node->content = NULL; /* Transfer ownership */
248         field->next = NULL;
249         
250         if (!first_field) {
251             first_field = field;
252             last_field = field;
253         } else {
254             last_field->next = field;
255             last_field = field;
256         }
257         
258         current_node = current_node->next;
259     }
260     
261     return first_field;
262 }
263
264 static csv_record_t *create_record_from_fields(field_node_t *field_nodes)
265 {
266     csv_record_t *record = malloc(sizeof(csv_record_t));
267     if (!record) {
268         return NULL;
269     }
270     
271     record->fields = convert_field_list(field_nodes);
272     record->next = NULL;
273     
274     /* Count fields */
275     record->field_count = 0;
276     csv_field_t *current = record->fields;
277     while (current) {
278         record->field_count++;
279         current = current->next;
280     }
281     
282     return record;
283 }
284
285 static void free_document_internal(csv_document_t *doc)
286 {
287     if (!doc) {
288         return;
289     }
290     
291     /* Free header */
292     if (doc->header) {
293         csv_field_t *field = doc->header->fields;
294         while (field) {
295             csv_field_t *next = field->next;
296             free(field->content);
297             free(field);
298             field = next;
299         }
300         free(doc->header);
301     }
302     
303     /* Free records */
304     csv_record_t *record = doc->records;
305     while (record) {
306         csv_record_t *next_record = record->next;
307         
308         csv_field_t *field = record->fields;
309         while (field) {
310             csv_field_t *next_field = field->next;
311             free(field->content);
312             free(field);
313             field = next_field;
314         }
315         
316         free(record);
317         record = next_record;
318     }
319     
320     free(doc);
321 }
322
323 /* Functions called by the Bison parser */
324
325 void csv_parser_add_record(field_node_t *fields)
326 {
327     if (!g_current_document || g_parse_error) {
328         return;
329     }
330     
331     /* Skip empty records (single empty field) */
332     if (fields && !fields->next && fields->content && fields->content[0] == '\0') {
333         return;
334     }
335     
336     csv_record_t *record = create_record_from_fields(fields);
337     if (!record) {
338         g_parse_error = true;
339         strncpy(g_error_message, "Memory allocation failed while creating record", sizeof(g_error_message) - 1);
340         return;
341     }
342     
343     /* Add to document */
344     if (!g_current_document->records) {
345         g_current_document->records = record;
346     } else {
347         /* Find the last record and append */
348         csv_record_t *last = g_current_document->records;
349         while (last->next) {
350             last = last->next;
351         }
352         last->next = record;
353     }
354     
355     g_current_document->record_count++;
356 }
357
358 void csv_parser_set_error(const char *error)
359 {
360     g_parse_error = true;
361     if (error) {
362         strncpy(g_error_message, error, sizeof(g_error_message) - 1);
363         g_error_message[sizeof(g_error_message) - 1] = '\0';
364     }
365 }
366
367 csv_error_t csv_document_set_first_record_as_header(csv_document_t *document)
368 {
369     if (!document || !document->records) {
370         return CSV_ERROR_INVALID_PARAM;
371     }
372     
373     /* Move first record to header */
374     document->header = document->records;
375     document->records = document->records->next;
376     document->header->next = NULL;
377     document->record_count--;
378     
379     return CSV_SUCCESS;
380 }