libcsv v0.1.0
Loading...
Searching...
No Matches
libcsv

CSV parser library in C.

Appendix

This is a C library that provides simple functions to parse CSV data, manipulate CSV rows, and handle common CSV errors. Whether you need to read CSV files, generate CSV strings, or work with CSV data in your C application.

Features

  • Parse CSV data from files or strings
  • Create and manipulate CSV rows
  • Write CSV data to files or strings
  • Retrieve CSV field values by index
  • Handle common CSV errors with ease

Tech Stack

Languages: C

Build: Make and Docker

Installation

Clone the project

git clone https://codeberg.org/luizvilasboas/libcsv.git

Go to the project directory

cd libcsv

Compile the library

make

Install the library

sudo make install

Usage/Examples

#include <stdio.h>
#include <libcsv.h>
int main() {
// Create a CSVParser to read a CSV file
CSVParser *parser = csv_parser_create("example.csv", ',');
if (!parser) {
fprintf(stderr, "Error: Unable to create CSV parser\n");
return 1;
}
// Read rows from the CSV file
CSVRow *row;
while ((row = csv_read_row(parser)) != NULL) {
for (int i = 0; i < row->field_count; i++) {
printf("Field %d: %s\n", i, csv_get_field(row, i));
}
// Don't forget to free the row
}
// Clean up the parser
return 0;
}
char * csv_get_field(const CSVRow *row, int field_index)
Retrieves the value of a specific field in a row by its index.
Definition libcsv.c:246
CSVRow * csv_read_row(CSVParser *parser)
Reads the next row from the CSV file using the parser.
Definition libcsv.c:46
void csv_parser_destroy(CSVParser *parser)
Frees resources associated with the parser and closes the open file.
Definition libcsv.c:32
void csv_row_destroy(CSVRow *row)
Frees memory allocated for a CSVRow structure and all its internal fields.
Definition libcsv.c:146
CSVParser * csv_parser_create(const char *filename, char delimiter)
Creates and initializes a parser to read a CSV file.
Definition libcsv.c:11
Structure managing the state of a CSV file parser.
Definition libcsv.h:27
Structure representing a single CSV row.
Definition libcsv.h:19
int field_count
Definition libcsv.h:21

Documentation

"" "Documentation"

Running Tests

To run tests, run the following command

make test

Contributing

If you have a suggestion that would make this better, please fork the repo and create a pull request.

License

LGPLv3