#ifndef CSV_PARSER_H #define CSV_PARSER_H #include #include #include /* Forward declarations */ typedef struct csv_parser csv_parser_t; typedef struct csv_field csv_field_t; typedef struct csv_record csv_record_t; typedef struct csv_document csv_document_t; /* Error codes */ typedef enum { CSV_SUCCESS = 0, CSV_ERROR_MEMORY = -1, CSV_ERROR_PARSE = -2, CSV_ERROR_INVALID_PARAMETER = -3, CSV_ERROR_IO = -4 } csv_error_t; /* Error information structure */ typedef struct { const char *message; /* Error message (valid until next parse operation) */ int line; /* Line number where error occurred (1-based, 0 if unknown) */ int column; /* Column number where error occurred (1-based, 0 if unknown) */ bool has_location; /* Whether line/column information is available */ } csv_error_info_t; /* CSV field structure - represents a single cell in the CSV */ struct csv_field { char *value; /* Cell content (null-terminated string) */ struct csv_field *next; /* Next field in the record */ }; /* CSV record structure - represents a single row in the CSV */ struct csv_record { csv_field_t *fields; /* Linked list of fields */ size_t field_count; /* Number of fields in this record */ struct csv_record *next; /* Next record in the document */ }; /* CSV document structure - represents the complete parsed CSV */ struct csv_document { csv_record_t *records; /* Linked list of records */ size_t record_count; /* Number of records */ }; /* Parser (opaque, stateful - maintains error state between operations) */ struct csv_parser; /** * Create a new CSV parser * @note Parser maintains error state; create separate parsers for concurrent use */ csv_parser_t *csv_parser_create(void); /** * Free a CSV parser and all associated memory */ void csv_parser_destroy(csv_parser_t *parser); /** * Parse CSV data from a string * @note Sets *document to NULL on failure */ csv_error_t csv_parser_parse_string(csv_parser_t *parser, const char *input, csv_document_t **document); /** * Parse CSV data from a file * @note Sets *document to NULL on failure * @note Advances file position to end of parsed content */ csv_error_t csv_parser_parse_file(csv_parser_t *parser, FILE *file, csv_document_t **document); /** * Get detailed error information from the parser * @note Message pointer is valid until the next parse operation */ csv_error_info_t csv_parser_get_error_info(csv_parser_t *parser); /** * Free a CSV document and all associated memory */ void csv_document_free(csv_document_t *document); /** * Convert error code to human-readable string */ const char *csv_error_string(csv_error_t error); /* Data access functions */ /** * Get the first record from document */ csv_record_t *csv_document_get_first_record(const csv_document_t *document); /** * Get the next record in sequence */ csv_record_t *csv_record_get_next(const csv_record_t *record); /** * Get the first field from a record */ csv_field_t *csv_record_get_first_field(const csv_record_t *record); /** * Get the next field in sequence */ csv_field_t *csv_field_get_next(const csv_field_t *field); /** * Get cell content as string */ const char *csv_field_get_value(const csv_field_t *field); #endif /* CSV_PARSER_H */