csvpp 1.2.0
CSV parsing / writing libraries
Loading...
Searching...
No Matches
Typedefs | Functions
CSV_reader

CSV Reader / parser. More...

Collaboration diagram for CSV_reader:

Typedefs

typedef struct CSV_reader CSV_reader
 

Functions

CSV_reader * CSV_reader_init_from_filename (const char *filename)
 Create a new CSV_reader object parsing from a file.
 
CSV_reader * CSV_reader_init_from_file (FILE *file)
 Create a new CSV_reader object parsing from a FILE *.
 
CSV_reader * CSV_reader_init_from_str (const char *input)
 Create a new CSV_reader object parsing from an in-memory string.
 
void CSV_reader_free (CSV_reader *reader)
 Free a CSV_reader object.
 
void CSV_reader_set_delimiter (CSV_reader *reader, const char delimiter)
 Change delimiter character.
 
void CSV_reader_set_quote (CSV_reader *reader, const char quote)
 Change the quote character.
 
void CSV_reader_set_lenient (CSV_reader *reader, const bool lenient)
 Enable / disable lenient parsing.
 
char * CSV_reader_read_field (CSV_reader *reader)
 Read a single field.
 
CSV_status CSV_reader_read_v (CSV_reader *reader,...)
 Reads fields into variadic arguments.
 
CSV_row * CSV_reader_read_row (CSV_reader *reader)
 Read the current row from the CSV file and advance to the next.
 
CSV_status CSV_reader_read_row_ptr (CSV_reader *reader, char ***fields, size_t *num_fields)
 Read a row into an array of strings.
 
bool CSV_reader_eof (const CSV_reader *reader)
 Check for end of input.
 
bool CSV_reader_end_of_row (const CSV_reader *reader)
 Check for end of current row.
 
const char * CSV_reader_get_error_msg (const CSV_reader *reader)
 Get message for last error.
 
CSV_status CSV_reader_get_error (const CSV_reader *reader)
 Get error code for last error.
 

Detailed Description

CSV Reader / parser.

By default, parses according to RFC 4180 rules, and will stop with an error when given non-conformant input. The field delimiter and quote characters may be changed, and there is a lenient parsing option to ignore violations.

Blank rows are ignored and skipped over.

Contains both row-wise and field-wise methods. Mixing these is not recommended, but is possible. Row-wise methods will act as if the current position is the start of a row, regardless of any fields that have been read from the current row so far.

Function Documentation

◆ CSV_reader_end_of_row()

bool CSV_reader_end_of_row ( const CSV_reader * reader)

Check for end of current row.

Returns
true if no fields remain in the current row

◆ CSV_reader_eof()

bool CSV_reader_eof ( const CSV_reader * reader)

Check for end of input.

Returns
true if no data remains to be read

◆ CSV_reader_free()

void CSV_reader_free ( CSV_reader * reader)

Free a CSV_reader object.

Closes the file if created with CSV_reader_init_from_filename

◆ CSV_reader_get_error()

CSV_status CSV_reader_get_error ( const CSV_reader * reader)

Get error code for last error.

Returns
CSV_status for last error that occurred

◆ CSV_reader_get_error_msg()

const char * CSV_reader_get_error_msg ( const CSV_reader * reader)

Get message for last error.

Returns
a string with details about the last error
NULL if no error has occurred

◆ CSV_reader_init_from_file()

CSV_reader * CSV_reader_init_from_file ( FILE * file)

Create a new CSV_reader object parsing from a FILE *.

Parameters
fileFILE * opened in read mode. Caller remains responsible to call fclose on the file use
Returns
New CSV_reader object. Free with CSV_reader_free()
Warning
Do close the input file until finished reading from it

◆ CSV_reader_init_from_filename()

CSV_reader * CSV_reader_init_from_filename ( const char * filename)

Create a new CSV_reader object parsing from a file.

Parameters
filenamePath to file
Returns
New CSV_reader object. Free with CSV_reader_free()
NULL if unable to open the file. Use strerror / perror for details

◆ CSV_reader_init_from_str()

CSV_reader * CSV_reader_init_from_str ( const char * input)

Create a new CSV_reader object parsing from an in-memory string.

Parameters
inputString to read from. Caller remains responsible to free the string after use
Returns
New CSV_reader object. Free with CSV_reader_free()
Warning
Do not free the input string until finished reading from it

◆ CSV_reader_read_field()

char * CSV_reader_read_field ( CSV_reader * reader)

Read a single field.

Check CSV_reader_end_of_row() to see if this is the last field in the current row

Returns
The next field from the row. Caller should free this with free()
NULL if past the end of the input or an error occurred. Check CSV_reader_get_error() to distinguish

◆ CSV_reader_read_row()

CSV_row * CSV_reader_read_row ( CSV_reader * reader)

Read the current row from the CSV file and advance to the next.

Returns
CSV_row array containing fields for this row. Free with CSV_row_free().
NULL if past the end of the input or an error occurred. Check CSV_reader_get_error() to distinguish

◆ CSV_reader_read_row_ptr()

CSV_status CSV_reader_read_row_ptr ( CSV_reader * reader,
char *** fields,
size_t * num_fields )

Read a row into an array of strings.

If fields is null, this will allocate a new array (and pass ownership to the caller), and return the final size into num_fields.

Otherwise, it overwrites fields contents with strings (owned by caller) up to the limit specified in num_fields, discarding any fields remaining until the end of the row

Parameters
[in,out]fieldsPointer to string array to write fields into. Caller must free all strings returned in the array.
If NULL is passed, a new array will be allocated and returned by this call. Caller should free this array as well as the strings within
[in,out]num_fieldsPointer to size of fields.
If NULL is passed to fields, the size of the new array returned into fields will be returned into num_fields
Returns
CSV_OK on successful read
CSV_TOO_MANY_FIELDS_WARNING when fields is not null and there are more fields than num_fields
CSV_EOF if no rows remain to be read
Other CSV_status error code if an error occurred when reading

◆ CSV_reader_read_v()

CSV_status CSV_reader_read_v ( CSV_reader * reader,
... )

Reads fields into variadic arguments.

Warning
This may be used to fields from multiple rows at a time. Use with caution if the number of fields per row is not known beforehand.
Parameters
[out]...char** variables to read into. Last argument must be NULL If more parameters are passed than there are fields remaining, the remaining parameters will be set to NULL

◆ CSV_reader_set_delimiter()

void CSV_reader_set_delimiter ( CSV_reader * reader,
const char delimiter )

Change delimiter character.

Parameters
delimiterNew delimiter character

◆ CSV_reader_set_lenient()

void CSV_reader_set_lenient ( CSV_reader * reader,
const bool lenient )

Enable / disable lenient parsing.

Lenient parsing will attempt to ignore syntax errors in CSV input.

◆ CSV_reader_set_quote()

void CSV_reader_set_quote ( CSV_reader * reader,
const char quote )

Change the quote character.

Parameters
quoteNew quote character