|
solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
|
CSV Parser Library: Provides functions for parsing CSV (Comma-Separated Values) data. More...
#include <stdbool.h>#include <stddef.h>Go to the source code of this file.
Classes | |
| struct | Row |
| Structure representing a CSV row. More... | |
Typedefs | |
| typedef struct CsvReader | CsvReader |
| Opaque structure representing a CSV parser. Create a new CSV parser with csv_reader_new and free it with csv_reader_free. Use csv_reader_parse to parse the CSV data and retrieve all the rows at once. Use csv_reader_parse_async to parse the CSV data and pass each processed row back in a callback. Use csv_reader_getnumrows to get the number of rows in the CSV data. Use csv_reader_setdelim to set the delimiter character for CSV fields. | |
| typedef struct CsvWriter | CsvWriter |
Functions | |
| CsvReader * | csv_reader_new (const char *filename, size_t arena_memory) |
| Create a new CSV reader associated with a filename. | |
| Row ** | csv_reader_parse (CsvReader *reader) |
| Parse the CSV data and retrieve all the rows at once. | |
| void | csv_reader_parse_async (CsvReader *reader, CsvRowCallback callback, size_t alloc_max) |
| Parse the CSV data and pass each processed row back in a callback. Return true from the callback to stop early. The parser file descriptor and stream will automatically be closed. | |
| size_t | csv_reader_numrows (const CsvReader *reader) |
| Get the number of rows in the CSV data. | |
| void | csv_reader_free (CsvReader *reader) |
| Free memory used by the CsvReader and CsvRow structures. | |
| bool | csvwriter_write_row (CsvWriter *writer, const char **fields, size_t numfields) |
CSV Parser Library: Provides functions for parsing CSV (Comma-Separated Values) data.
Definition in file csvparser.h.
| typedef struct CsvReader CsvReader |
Opaque structure representing a CSV parser. Create a new CSV parser with csv_reader_new and free it with csv_reader_free. Use csv_reader_parse to parse the CSV data and retrieve all the rows at once. Use csv_reader_parse_async to parse the CSV data and pass each processed row back in a callback. Use csv_reader_getnumrows to get the number of rows in the CSV data. Use csv_reader_setdelim to set the delimiter character for CSV fields.
You can redefine before including header the MAX_FIELD_SIZE macro to change the maximum size of the csv line and the CSV_ARENA_BLOCK_SIZE macro to change the size of the arena block.
Definition at line 48 of file csvparser.h.
| typedef struct CsvWriter CsvWriter |
CsvWriter* writer = csvwriter_new("test.csv"); if (!writer) { printf("Error creating CSV writer\n"); return; }
csvwriter_write_row(writer, (const char*[]){"name", "age"}, 2); csvwriter_write_row(writer, (const char*[]){"Alice", "25"}, 2); csvwriter_write_row(writer, (const char*[]){"Bob", "30"}, 2); csvwriter_write_row(writer, (const char*[]){"Charlie", "35"}, 2);
csvwriter_free(writer);
Definition at line 164 of file csvparser.h.
| void csv_reader_free | ( | CsvReader * | reader | ) |
Free memory used by the CsvReader and CsvRow structures.
This function releases the memory used by the CsvReader and any CsvRow structures created with it.
| reader | A pointer to the CsvReader. |
Definition at line 296 of file csvparser.c.
References csv_reader_free().
Referenced by csv_reader_free().
| CsvReader * csv_reader_new | ( | const char * | filename, |
| size_t | arena_memory | ||
| ) |
Create a new CSV reader associated with a filename.
This function initializes a new CSV reader and associates it with the given filename.
| filename | The filename of the CSV file to parse. |
| arena_memory | The maximum size of the memory for the parser arena. Pass 0 to use the default or override with -DCSV_ARENA_BLOCK_SIZE. |
Definition at line 49 of file csvparser.c.
References csv_reader_new().
Referenced by csv_reader_new().
| size_t csv_reader_numrows | ( | const CsvReader * | reader | ) |
Get the number of rows in the CSV data.
This function returns the total number of rows in the CSV data, excluding empty lines and comments.
| reader | A pointer to the CsvReader. |
Definition at line 294 of file csvparser.c.
References csv_reader_numrows().
Referenced by csv_reader_numrows().
| Row ** csv_reader_parse | ( | CsvReader * | reader | ) |
Parse the CSV data and retrieve all the rows at once.
This function parses the CSV data and returns all the rows as an array of CsvRow structure.
The parser file descriptor and stream will automatically be closed. Note that this function allocates an array of all items on the heap that you must free with csv_parser_free.
| reader | A pointer to the CsvReader. |
Definition at line 138 of file csvparser.c.
References csv_reader_parse().
Referenced by csv_reader_parse().
| void csv_reader_parse_async | ( | CsvReader * | reader, |
| CsvRowCallback | callback, | ||
| size_t | alloc_max | ||
| ) |
Parse the CSV data and pass each processed row back in a callback. Return true from the callback to stop early. The parser file descriptor and stream will automatically be closed.
If alloc_max is 0, the parser will allocate all rows at once; otherwise it will allocate alloc_max rows.
| reader | A pointer to the CsvReader. |
| alloc_max | The maximum number of rows to allocate at once. |
Definition at line 218 of file csvparser.c.
References csv_reader_parse_async().
Referenced by csv_reader_parse_async().
| bool csvwriter_write_row | ( | CsvWriter * | writer, |
| const char ** | fields, | ||
| size_t | numfields | ||
| ) |
Optimized CSV writer function with comprehensive error handling.
| writer | Pointer to CsvWriter instance. |
| fields | Array of field strings to write. |
| numfields | Number of fields in the array. |
Definition at line 611 of file csvparser.c.
References csvwriter_write_row().
Referenced by csvwriter_write_row().