8 /* Forward declarations */
9 typedef struct csv_parser csv_parser_t;
10 typedef struct csv_field csv_field_t;
11 typedef struct csv_record csv_record_t;
12 typedef struct csv_document csv_document_t;
17 CSV_ERROR_MEMORY = -1,
19 CSV_ERROR_INVALID_PARAM = -3,
23 /* Error information structure */
25 const char *message; /* Error message (valid until next parse operation) */
26 int line; /* Line number where error occurred (1-based, 0 if unknown) */
27 int column; /* Column number where error occurred (1-based, 0 if unknown) */
28 bool has_location; /* Whether line/column information is available */
31 /* CSV field structure */
33 char *content; /* Field content (null-terminated) */
34 struct csv_field *next; /* Next field in the record */
37 /* CSV record structure */
39 csv_field_t *fields; /* Linked list of fields */
40 size_t field_count; /* Number of fields in this record */
41 struct csv_record *next; /* Next record in the document */
44 /* CSV document structure */
46 csv_record_t *records; /* Linked list of records (excluding header) */
47 size_t record_count; /* Number of data records (excluding header) */
48 csv_record_t *header; /* Optional header record (can be NULL) */
51 /* Parser context (opaque) */
55 * Create a new CSV parser instance
56 * @return Parser instance or NULL on error
58 csv_parser_t *csv_parser_create(void);
61 * Destroy a CSV parser instance and free all associated memory
62 * @param parser Parser instance to destroy (NULL is safe to pass)
64 void csv_parser_destroy(csv_parser_t *parser);
67 * Parse CSV data from a string
68 * @param parser Parser instance (must not be NULL)
69 * @param input Input CSV string (null-terminated, must not be NULL)
70 * @param document Pointer to store the parsed document (must not be NULL)
71 * @return CSV_SUCCESS on success, error code on failure
72 * @note The parser will set *document to NULL on failure
74 csv_error_t csv_parser_parse_string(csv_parser_t *parser, const char *input, csv_document_t **document);
77 * Parse CSV data from a file pointer
78 * @param parser Parser instance (must not be NULL)
79 * @param file Open file pointer (must not be NULL and readable)
80 * @param document Pointer to store the parsed document (must not be NULL)
81 * @return CSV_SUCCESS on success, error code on failure
82 * @note The parser will set *document to NULL on failure
83 * @note File position will be advanced to end of parsed content
85 csv_error_t csv_parser_parse_file(csv_parser_t *parser, FILE *file, csv_document_t **document);
88 * Get detailed error information from the parser
89 * @param parser Parser instance (must not be NULL)
90 * @return Error information structure
91 * @note The returned message pointer is valid until the next parse operation
93 csv_error_info_t csv_parser_get_error_info(csv_parser_t *parser);
96 * Free a CSV document and all associated memory
97 * @param document Document to free (NULL is safe to pass)
99 void csv_document_free(csv_document_t *document);
102 * Convert error code to human-readable string
103 * @param error Error code
104 * @return Error description string
106 const char *csv_error_string(csv_error_t error);
108 /* Data access helper functions */
111 * Check if document has a header record
112 * @param document CSV document (must not be NULL)
113 * @return true if header exists, false if not
115 bool csv_document_has_header(const csv_document_t *document);
118 * Get the first record from document (skipping header if present)
119 * @param document CSV document (must not be NULL)
120 * @return First data record or NULL if empty
122 csv_record_t *csv_document_get_first_record(const csv_document_t *document);
125 * Get the next record in sequence
126 * @param record Current record (must not be NULL)
127 * @return Next record or NULL if at end
129 csv_record_t *csv_record_get_next(const csv_record_t *record);
132 * Get the first field from a record
133 * @param record CSV record (must not be NULL)
134 * @return First field or NULL if record is empty
136 csv_field_t *csv_record_get_first_field(const csv_record_t *record);
139 * Get the next field in sequence
140 * @param field Current field (must not be NULL)
141 * @return Next field or NULL if at end
143 csv_field_t *csv_field_get_next(const csv_field_t *field);
146 * Get field content as string
147 * @param field CSV field (must not be NULL)
148 * @return Field content string or NULL on error
150 const char *csv_field_get_content(const csv_field_t *field);
153 * Set the first record as the header (moves it from records to header)
154 * @param document CSV document (must not be NULL and have at least one record)
155 * @return CSV_SUCCESS on success, error code on failure
157 csv_error_t csv_document_set_first_record_as_header(csv_document_t *document);
159 #endif /* CSV_PARSER_H */