solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
Classes | Typedefs | Functions
csvparser.h File Reference

CSV Parser Library: Provides functions for parsing CSV (Comma-Separated Values) data. More...

#include <stdbool.h>
#include <stddef.h>
Include dependency graph for csvparser.h:
This graph shows which files directly or indirectly include this file:

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)
 

Detailed Description

CSV Parser Library: Provides functions for parsing CSV (Comma-Separated Values) data.

Definition in file csvparser.h.

Typedef Documentation

◆ CsvReader

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.

◆ CsvWriter

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.

Function Documentation

◆ csv_reader_free()

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.

Parameters
readerA pointer to the CsvReader.

Definition at line 296 of file csvparser.c.

References csv_reader_free().

Referenced by csv_reader_free().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ csv_reader_new()

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.

Parameters
filenameThe filename of the CSV file to parse.
arena_memoryThe maximum size of the memory for the parser arena. Pass 0 to use the default or override with -DCSV_ARENA_BLOCK_SIZE.
Returns
A pointer to the created CsvReader, or NULL on failure.

Definition at line 49 of file csvparser.c.

References csv_reader_new().

Referenced by csv_reader_new().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ csv_reader_numrows()

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.

Parameters
readerA pointer to the CsvReader.
Returns
The number of rows.

Definition at line 294 of file csvparser.c.

References csv_reader_numrows().

Referenced by csv_reader_numrows().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ csv_reader_parse()

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.

Parameters
readerA pointer to the CsvReader.
Returns
A pointer to the next CsvRow, or NULL if there are no more rows or an error occurs.

Definition at line 138 of file csvparser.c.

References csv_reader_parse().

Referenced by csv_reader_parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ csv_reader_parse_async()

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.

Parameters
readerA pointer to the CsvReader.
alloc_maxThe maximum number of rows to allocate at once.
Returns
void.

Definition at line 218 of file csvparser.c.

References csv_reader_parse_async().

Referenced by csv_reader_parse_async().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ csvwriter_write_row()

bool csvwriter_write_row ( CsvWriter *  writer,
const char **  fields,
size_t  numfields 
)

Optimized CSV writer function with comprehensive error handling.

Parameters
writerPointer to CsvWriter instance.
fieldsArray of field strings to write.
numfieldsNumber of fields in the array.
Returns
true on success, false on error (check errno for details).

Definition at line 611 of file csvparser.c.

References csvwriter_write_row().

Referenced by csvwriter_write_row().

Here is the call graph for this function:
Here is the caller graph for this function: