From 0ee2ec205c32de9b2563cc4175d1f6d3b65eb220 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Fri, 21 Aug 2020 13:37:11 +0200 Subject: fix(doku): Dokumentation überarbeitet und deutlich ausgebaut. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- csv-tutorial.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 csv-tutorial.c (limited to 'csv-tutorial.c') diff --git a/csv-tutorial.c b/csv-tutorial.c new file mode 100644 index 0000000..17b95c8 --- /dev/null +++ b/csv-tutorial.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include + +#include "csv.h" + +bool +processFile(const char *filename) +{ + FILE *in; + + if ( (in = fopen(filename, "r")) == NULL ) { + return false; + } + + csv_t csv; + csv_init(&csv); + + while ( csv_read(&csv, in) ) { + const size_t nf = csv_nfields(&csv); + (void) nf; + // ... + } + csv_cleanup(&csv); + + fclose(in); + return true; +} + +bool +processFile_options(const char *filename) +{ + FILE *in; + + if ( (in = fopen(filename, "r")) == NULL ) { + return false; + } + + csv_options_t csv_options = csv_default_options; + csv_options.field_delimiter = '"'; + csv_options.field_separator = ','; + + csv_t csv; + csv_init_opt(&csv, &csv_options); + + while ( csv_read(&csv, in) ) { + const size_t nf = csv_nfields(&csv); + (void) nf; + // ... + } + csv_cleanup(&csv); + + fclose(in); + return true; +} + +static void +csvErrorHandler(csv_err_t err, void *cb_arg) +{ + longjmp(*((jmp_buf *) cb_arg), err); +} + +bool +processFile_error(const char *filename) +{ + FILE *in; + + if ( (in = fopen(filename, "r")) == NULL ) { + return false; + } + + csv_options_t csv_options = csv_default_options; + csv_options.field_delimiter = '"'; + csv_options.field_separator = ','; + + jmp_buf env; + csv_options.cb_error = csvErrorHandler; + csv_options.cb_error_arg = &env; + + csv_t csv; + csv_init_opt(&csv, &csv_options); + + switch ( setjmp(env) ) { + case CSV_ERR_OK: + while ( csv_read(&csv, in) ) { + const size_t nf = csv_nfields(&csv); + (void) nf; + // ... + } + break; + + case CSV_ERR_OUT_OF_MEMORY: + csv_cleanup(&csv); + break; + + case CSV_ERR_OUT_OF_RANGE: + csv_cleanup(&csv); + break; + + case CSV_ERR_IO_READ: + csv_cleanup(&csv); + break; + + case CSV_ERR_IO_WRITE: + csv_cleanup(&csv); + break; + } + + fclose(in); + return true; +} + +int +main(void) +{ + return EXIT_SUCCESS; +} -- cgit v1.3