libcsv v0.1.0
Loading...
Searching...
No Matches
Classes | Enumerations | Functions
libcsv.h File Reference
#include <stdio.h>
Include dependency graph for libcsv.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  CSVRow
 Structure representing a single CSV row. More...
 
struct  CSVParser
 Structure managing the state of a CSV file parser. More...
 

Enumerations

enum  CSVError { CSV_SUCCESS , CSV_ERROR_MEMORY , CSV_ERROR_FILE_OPEN , CSV_ERROR_INVALID_ARG }
 Error codes returned by library operations. More...
 

Functions

CSVParsercsv_parser_create (const char *filename, char delimiter)
 Creates and initializes a parser to read a CSV file.
 
void csv_parser_destroy (CSVParser *parser)
 Frees resources associated with the parser and closes the open file.
 
CSVRowcsv_read_row (CSVParser *parser)
 Reads the next row from the CSV file using the parser.
 
CSVRowcsv_row_create (int field_count, const char **fields)
 Manually creates and allocates a CSVRow structure from an array of strings.
 
void csv_row_destroy (CSVRow *row)
 Frees memory allocated for a CSVRow structure and all its internal fields.
 
CSVRowcsv_parse_string (const char *csv_string, char delimiter)
 Parses an individual CSV-formatted string into a CSVRow structure.
 
char * csv_write_string (const CSVRow *row, char delimiter)
 Converts a CSVRow structure into a single formatted string.
 
int csv_write_file (const CSVRow *row, const char *filename, char delimiter)
 Writes the contents of a CSVRow directly to a file.
 
char * csv_get_field (const CSVRow *row, int field_index)
 Retrieves the value of a specific field in a row by its index.
 
void csv_set_delimiter (CSVParser *parser, char new_delimiter)
 Updates the delimiter character used by an active parser.
 
char csv_get_delimiter (const CSVParser *parser)
 Gets the current delimiter configured for the parser.
 
const char * csv_error_message (CSVError error)
 Returns a human-readable message corresponding to an error code.
 
CSVError csv_get_last_error (void)
 Gets the last error code produced by the library on the calling thread.
 

Enumeration Type Documentation

◆ CSVError

enum CSVError

Error codes returned by library operations.

Enumerator
CSV_SUCCESS 

Operation completed successfully.

CSV_ERROR_MEMORY 

Memory allocation failure.

CSV_ERROR_FILE_OPEN 

Error opening or accessing file.

CSV_ERROR_INVALID_ARG 

Invalid argument passed to function.

Function Documentation

◆ csv_error_message()

const char * csv_error_message ( CSVError  error)

Returns a human-readable message corresponding to an error code.

Parameters
errorThe CSVError code.
Returns
Constant string describing the error.

◆ csv_get_delimiter()

char csv_get_delimiter ( const CSVParser parser)

Gets the current delimiter configured for the parser.

Parameters
parserPointer to the CSVParser instance.
Returns
The delimiter character currently in use.

◆ csv_get_field()

char * csv_get_field ( const CSVRow row,
int  field_index 
)

Retrieves the value of a specific field in a row by its index.

Parameters
rowPointer to the CSV row structure.
field_indexZero-based index of the target field.
Returns
Pointer to the field string, or NULL if index is out of bounds.

◆ csv_get_last_error()

CSVError csv_get_last_error ( void  )

Gets the last error code produced by the library on the calling thread.

Returns
The CSVError code corresponding to the last failure.

◆ csv_parse_string()

CSVRow * csv_parse_string ( const char *  csv_string,
char  delimiter 
)

Parses an individual CSV-formatted string into a CSVRow structure.

Parameters
csv_stringThe raw CSV-formatted input string.
delimiterField delimiter character.
Returns
Pointer to the allocated CSVRow structure, or NULL on error.

◆ csv_parser_create()

CSVParser * csv_parser_create ( const char *  filename,
char  delimiter 
)

Creates and initializes a parser to read a CSV file.

Parameters
filenamePath to the CSV file to be opened.
delimiterField delimiter character.
Returns
Pointer to the allocated CSVParser structure, or NULL on error.

◆ csv_parser_destroy()

void csv_parser_destroy ( CSVParser parser)

Frees resources associated with the parser and closes the open file.

Parameters
parserPointer to the CSVParser structure to destroy.

◆ csv_read_row()

CSVRow * csv_read_row ( CSVParser parser)

Reads the next row from the CSV file using the parser.

Parameters
parserPointer to the active CSVParser.
Returns
Pointer to the populated CSVRow structure, or NULL on EOF/error.

◆ csv_row_create()

CSVRow * csv_row_create ( int  field_count,
const char **  fields 
)

Manually creates and allocates a CSVRow structure from an array of strings.

Parameters
field_countNumber of fields to insert.
fieldsArray of pointers to field strings.
Returns
Pointer to the created CSVRow structure, or NULL on error.

◆ csv_row_destroy()

void csv_row_destroy ( CSVRow row)

Frees memory allocated for a CSVRow structure and all its internal fields.

Parameters
rowPointer to the CSVRow structure to destroy.

◆ csv_set_delimiter()

void csv_set_delimiter ( CSVParser parser,
char  new_delimiter 
)

Updates the delimiter character used by an active parser.

Parameters
parserPointer to the CSVParser instance.
new_delimiterNew delimiter character to set.

◆ csv_write_file()

int csv_write_file ( const CSVRow row,
const char *  filename,
char  delimiter 
)

Writes the contents of a CSVRow directly to a file.

Parameters
rowPointer to the row to write.
filenamePath to the output destination file.
delimiterField delimiter character.
Returns
0 on success, or an error code on failure.

◆ csv_write_string()

char * csv_write_string ( const CSVRow row,
char  delimiter 
)

Converts a CSVRow structure into a single formatted string.

Parameters
rowPointer to the CSV row structure.
delimiterCharacter to place between fields.
Returns
Dynamically allocated string with formatted data (must be freed by caller), or NULL.