From dd7e73ec6c990c23a08d4e82f050804caa9789df Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 21 Jun 2020 11:12:18 +0200 Subject: Benutze unterschiedliche Callback-Argumente für die Memory- und Error-Callback-Handler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- csv-test.c | 2 +- csv.c | 14 +++++++------- csv.h | 5 ++++- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/csv-test.c b/csv-test.c index 15d868c..3bd6b7b 100644 --- a/csv-test.c +++ b/csv-test.c @@ -76,7 +76,7 @@ test_exception_longjmp(FILE *f) /* setup error handler */ jmp_buf env; csv_options.cb_error = my_error_handler; - csv_options.cb_arg = &env; + csv_options.cb_error_arg = &env; /* setup csv-Object with custom options */ csv_t csv[1]; diff --git a/csv.c b/csv.c index 1456d6e..d8321bd 100644 --- a/csv.c +++ b/csv.c @@ -50,7 +50,7 @@ INLINE void csv_fatal_error(const char *msg, const csv_options_t * const csv_options) { if ( csv_options->cb_error != NULL ) { - (*csv_options->cb_error)(msg, csv_options->cb_arg); + (*csv_options->cb_error)(msg, csv_options->cb_error_arg); } fprintf(stderr, "fatal error: %s\n", msg); @@ -95,7 +95,7 @@ csv_string_append(csv_string_t *csv_string, char ch, const csv_options_t * const if ( csv_string->pos == csv_string->cap ) { /* grow if needed */ if ( csv_string->str == NULL ) { /* first call? */ - if ( (csv_string->str = csv_options->cb_allocate(INITIAL_CAP, 1, csv_options->cb_arg)) == NULL ) { + if ( (csv_string->str = csv_options->cb_allocate(INITIAL_CAP, 1, csv_options->cb_memory_arg)) == NULL ) { csv_fatal_error("out of memory...", csv_options); return; } @@ -104,7 +104,7 @@ csv_string_append(csv_string_t *csv_string, char ch, const csv_options_t * const } else { /* subsequent call */ int cap = (csv_string->cap * 3) / 2; /* *= 1.5 */ - char *str = csv_options->cb_reallocate(csv_string->str, cap, 1, csv_options->cb_arg); + char *str = csv_options->cb_reallocate(csv_string->str, cap, 1, csv_options->cb_memory_arg); if ( str == NULL ) { csv_fatal_error("out of memory...", csv_options); return; @@ -123,7 +123,7 @@ csv_string_free(csv_string_t *csv_string, const csv_options_t * const csv_option assert(csv_string != NULL); assert(csv_options != NULL); - csv_options->cb_free(csv_string->str, csv_string->cap, csv_options->cb_arg); + csv_options->cb_free(csv_string->str, csv_string->cap, csv_options->cb_memory_arg); /* call *_init() for sane default values; prevent possible double-free */ csv_string_init(csv_string); @@ -159,7 +159,7 @@ csv_field_append(csv_field_t *csv_field, int idx, const csv_options_t * const cs if ( csv_field->pos == csv_field->cap ) { /* grow if needed */ if ( csv_field->fields == NULL ) { - if ( (csv_field->fields = csv_options->cb_allocate(INITIAL_CAP, sizeof(csv_field->fields[0]), csv_options->cb_arg)) == NULL ) { + if ( (csv_field->fields = csv_options->cb_allocate(INITIAL_CAP, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg)) == NULL ) { csv_fatal_error("out of memory", csv_options); return; } @@ -168,7 +168,7 @@ csv_field_append(csv_field_t *csv_field, int idx, const csv_options_t * const cs } else { int cap = (csv_field->cap * 3) / 2; /* *= 1.5 */ - int *fields = csv_options->cb_reallocate(csv_field->fields, cap, sizeof(csv_field->fields[0]), csv_options->cb_arg); + int *fields = csv_options->cb_reallocate(csv_field->fields, cap, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg); if ( fields == NULL ) { csv_fatal_error("out of memory", csv_options); return; @@ -187,7 +187,7 @@ csv_field_free(csv_field_t *csv_field, const csv_options_t * const csv_options) assert(csv_field != NULL); assert(csv_options != NULL); - csv_options->cb_free(csv_field->fields, csv_field->cap, csv_options->cb_arg); + csv_options->cb_free(csv_field->fields, csv_field->cap, csv_options->cb_memory_arg); csv_field_init(csv_field); } diff --git a/csv.h b/csv.h index 3a529b7..fe59f5b 100644 --- a/csv.h +++ b/csv.h @@ -11,11 +11,14 @@ extern "C" { typedef struct { char quote_symbol; char sep_symbol; - void *cb_arg; + void (*cb_error)(const char *, void *); + void *cb_error_arg; + void *(*cb_allocate)(size_t, size_t, void *); void *(*cb_reallocate)(void *, size_t, size_t, void *); void (*cb_free)(void *, size_t sz, void *); + void *cb_memory_arg; } csv_options_t; extern const csv_options_t csv_default_options; -- cgit v1.3