1 #include "csv_parser.h"
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);
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] = "";
19 /* CSV parser structure */
21 char error_message[256];
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);
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);
36 csv_parser_t *csv_parser_create(void)
38 csv_parser_t *parser = malloc(sizeof(csv_parser_t));
43 parser->error_message[0] = '\0';
44 parser->has_error = false;
49 void csv_parser_destroy(csv_parser_t *parser)
56 csv_error_t csv_parser_parse_string(csv_parser_t *parser, const char *input, csv_document_t **document)
58 if (!parser || !input || !document) {
59 return CSV_ERROR_INVALID_PARAM;
64 /* Create a temporary file from the string input */
65 FILE *temp_file = tmpfile();
67 strncpy(parser->error_message, "Failed to create temporary file", sizeof(parser->error_message) - 1);
68 parser->has_error = true;
72 /* Write input to temporary file */
73 if (fputs(input, temp_file) == EOF) {
75 strncpy(parser->error_message, "Failed to write to temporary file", sizeof(parser->error_message) - 1);
76 parser->has_error = true;
80 /* Reset file position to beginning */
83 /* Parse from the temporary file */
84 csv_error_t result = csv_parser_parse_file(parser, temp_file, document);
90 csv_error_t csv_parser_parse_file(csv_parser_t *parser, FILE *file, csv_document_t **document)
92 if (!parser || !file || !document) {
93 return CSV_ERROR_INVALID_PARAM;
97 parser->has_error = false;
98 parser->error_message[0] = '\0';
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;
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';
115 /* Initialize the reentrant 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;
124 /* Set up the lexer input */
125 csvset_in(file, scanner);
127 /* Parse the input */
128 int parse_result = csvparse(parser, scanner);
130 /* Clean up lexer state */
131 csvlex_destroy(scanner);
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);
138 strncpy(parser->error_message, "Parse error", sizeof(parser->error_message) - 1);
140 parser->has_error = true;
142 free_document_internal(g_current_document);
143 g_current_document = NULL;
144 g_current_parser = NULL;
145 return CSV_ERROR_PARSE;
148 /* Success - transfer ownership of document */
149 *document = g_current_document;
150 g_current_document = NULL;
151 g_current_parser = NULL;
156 const char *csv_parser_get_error(csv_parser_t *parser)
159 return "Invalid parser";
162 return parser->has_error ? parser->error_message : "No error";
165 void csv_document_free(csv_document_t *document)
167 free_document_internal(document);
170 const char *csv_error_string(csv_error_t error)
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";
184 return "Unknown error";
188 /* Data access helper functions */
190 bool csv_document_has_header(const csv_document_t *document)
192 return document && document->header != NULL;
195 csv_record_t *csv_document_get_first_record(const csv_document_t *document)
197 return document ? document->records : NULL;
200 csv_record_t *csv_record_get_next(const csv_record_t *record)
202 return record ? record->next : NULL;
205 csv_field_t *csv_record_get_first_field(const csv_record_t *record)
207 return record ? record->fields : NULL;
210 csv_field_t *csv_field_get_next(const csv_field_t *field)
212 return field ? field->next : NULL;
215 const char *csv_field_get_content(const csv_field_t *field)
217 return field ? field->content : NULL;
220 /* Internal helper functions */
222 static csv_field_t *convert_field_list(field_node_t *field_nodes)
228 csv_field_t *first_field = NULL;
229 csv_field_t *last_field = NULL;
231 field_node_t *current_node = field_nodes;
232 while (current_node) {
233 csv_field_t *field = malloc(sizeof(csv_field_t));
235 /* Clean up already allocated fields */
236 while (first_field) {
237 csv_field_t *next = first_field->next;
238 free(first_field->content);
245 /* Copy content (take ownership) */
246 field->content = current_node->content;
247 current_node->content = NULL; /* Transfer ownership */
254 last_field->next = field;
258 current_node = current_node->next;
264 static csv_record_t *create_record_from_fields(field_node_t *field_nodes)
266 csv_record_t *record = malloc(sizeof(csv_record_t));
271 record->fields = convert_field_list(field_nodes);
275 record->field_count = 0;
276 csv_field_t *current = record->fields;
278 record->field_count++;
279 current = current->next;
285 static void free_document_internal(csv_document_t *doc)
293 csv_field_t *field = doc->header->fields;
295 csv_field_t *next = field->next;
296 free(field->content);
304 csv_record_t *record = doc->records;
306 csv_record_t *next_record = record->next;
308 csv_field_t *field = record->fields;
310 csv_field_t *next_field = field->next;
311 free(field->content);
317 record = next_record;
323 /* Functions called by the Bison parser */
325 void csv_parser_add_record(field_node_t *fields)
327 if (!g_current_document || g_parse_error) {
331 /* Skip empty records (single empty field) */
332 if (fields && !fields->next && fields->content && fields->content[0] == '\0') {
336 csv_record_t *record = create_record_from_fields(fields);
338 g_parse_error = true;
339 strncpy(g_error_message, "Memory allocation failed while creating record", sizeof(g_error_message) - 1);
343 /* Add to document */
344 if (!g_current_document->records) {
345 g_current_document->records = record;
347 /* Find the last record and append */
348 csv_record_t *last = g_current_document->records;
355 g_current_document->record_count++;
358 void csv_parser_set_error(const char *error)
360 g_parse_error = true;
362 strncpy(g_error_message, error, sizeof(g_error_message) - 1);
363 g_error_message[sizeof(g_error_message) - 1] = '\0';
367 csv_error_t csv_document_set_first_record_as_header(csv_document_t *document)
369 if (!document || !document->records) {
370 return CSV_ERROR_INVALID_PARAM;
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--;