From 575ae35abccb0a22d17e3121c19dd59a71aebf3f Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Thu, 18 Jun 2020 10:53:43 +0200 Subject: header datei --- csv-test.c | 46 ++++++---------------------------------------- csv.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 40 deletions(-) create mode 100644 csv.h diff --git a/csv-test.c b/csv-test.c index fe8afff..de92e46 100644 --- a/csv-test.c +++ b/csv-test.c @@ -7,6 +7,8 @@ #include #include +#include "csv.h" + #define INLINE static inline /* === CSV-MEMORY Interface === */ @@ -37,18 +39,6 @@ csv_mem_free(void *ptr, void *cb_arg) /* === CSV-OPTIONS Interface === */ -typedef struct { - char quote_symbol; - char sep_symbol; - void *cb_arg; - void (*cb_error)(const char *, void *); - void *(*cb_allocate)(size_t, size_t, void *); - void *(*cb_reallocate)(void *, size_t, size_t, void *); - void (*cb_free)(void *, void *); -} csv_options_t; - -extern const csv_options_t csv_default_options; - const csv_options_t csv_default_options = { .quote_symbol = '"', .sep_symbol = ',', @@ -73,11 +63,6 @@ csv_fatal_error(const char *msg, const csv_options_t * const csv_options) /* === CSV-STRING Interface === */ -typedef struct { - char *str; - int cap, pos; -} csv_string_t; - INLINE void csv_string_init(csv_string_t *csv_string) { @@ -150,11 +135,6 @@ csv_string_free(csv_string_t *csv_string, const csv_options_t * const csv_option /* === CSV-FIELD Interface === */ -typedef struct { - int *fields; - int cap, pos; -} csv_field_t; - INLINE void csv_field_init(csv_field_t *csv_field) { @@ -218,20 +198,6 @@ csv_field_free(csv_field_t *csv_field, const csv_options_t * const csv_options) /* === CSV Interface === */ -typedef struct { - const csv_options_t *csv_options; - csv_string_t csv_string; - csv_field_t csv_field; -} csv_t; - -void csv_init(csv_t *csv); -void csv_init_opt(csv_t *csv, const csv_options_t * const csv_options); -void csv_free(csv_t *csv); -int csv_nfields(csv_t *csv); -const char * csv_field(csv_t *csv, int idx); -int csv_read(csv_t *csv, FILE *in); -void csv_write(const csv_options_t * const csv_options, int n, const char *fields[], FILE *out); - void csv_init(csv_t *csv) { @@ -436,7 +402,7 @@ csv_read(csv_t *csv, FILE *in) } static void -csv_write_field(const csv_options_t * const csv_options, const char *field, FILE *out) +csv_write_field(FILE *out, const char *field, const csv_options_t * const csv_options) { assert(csv_options != NULL); assert(field != NULL); @@ -462,7 +428,7 @@ csv_write_field(const csv_options_t * const csv_options, const char *field, FILE } void -csv_write(const csv_options_t * const csv_options, int n, const char *fields[], FILE *out) +csv_write(FILE *out, int n, const char *fields[], const csv_options_t * const csv_options) { assert(csv_options != NULL); assert(fields != NULL); @@ -472,12 +438,12 @@ csv_write(const csv_options_t * const csv_options, int n, const char *fields[], /* process first field */ if ( 0 != n && fields[0] != NULL ) { - csv_write_field(csv_options, fields[0], out); + csv_write_field(out, fields[0], csv_options); /* process next fields */ for ( int i = 1; i != n && fields[i] != NULL; ++i ) { putc(SEP, out); - csv_write_field(csv_options, fields[i], out); + csv_write_field(out, fields[i], csv_options); } } fprintf(out, "\r\n"); diff --git a/csv.h b/csv.h new file mode 100644 index 0000000..83755f3 --- /dev/null +++ b/csv.h @@ -0,0 +1,51 @@ +#ifndef ITS1_CSV_H_INCLUDED +#define ITS1_CSV_H_INCLUDED + +#include /* FILE */ +#include /* size_t */ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + char quote_symbol; + char sep_symbol; + void *cb_arg; + void (*cb_error)(const char *, void *); + void *(*cb_allocate)(size_t, size_t, void *); + void *(*cb_reallocate)(void *, size_t, size_t, void *); + void (*cb_free)(void *, void *); +} csv_options_t; + +extern const csv_options_t csv_default_options; + +typedef struct { + char *str; + int cap, pos; +} csv_string_t; + +typedef struct { + int *fields; + int cap, pos; +} csv_field_t; + +typedef struct { + const csv_options_t *csv_options; + csv_string_t csv_string; + csv_field_t csv_field; +} csv_t; + +void csv_init(csv_t *csv); +void csv_init_opt(csv_t *csv, const csv_options_t * const csv_options); +void csv_free(csv_t *csv); +int csv_nfields(csv_t *csv); +const char * csv_field(csv_t *csv, int idx); +int csv_read(csv_t *csv, FILE *in); +void csv_write(FILE *out, int n, const char *fields[], const csv_options_t * const csv_options); + +#ifdef __cplusplus +} +#endif + +#endif -- cgit v1.3