summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2020-06-21 11:12:18 +0200
committerThomas Schmucker <ts@its1.de>2020-06-21 11:12:18 +0200
commitdd7e73ec6c990c23a08d4e82f050804caa9789df (patch)
tree5091c9c5e79e7b632b588a897444f2aa18a3304b
parent4f23f3d2b3137ca369c5e27f833a13e329431505 (diff)
downloadlibcsv-dd7e73ec6c990c23a08d4e82f050804caa9789df.tar.gz
libcsv-dd7e73ec6c990c23a08d4e82f050804caa9789df.tar.bz2
libcsv-dd7e73ec6c990c23a08d4e82f050804caa9789df.zip
Benutze unterschiedliche Callback-Argumente für die Memory- und Error-Callback-Handler
-rw-r--r--csv-test.c2
-rw-r--r--csv.c14
-rw-r--r--csv.h5
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)
76 /* setup error handler */ 76 /* setup error handler */
77 jmp_buf env; 77 jmp_buf env;
78 csv_options.cb_error = my_error_handler; 78 csv_options.cb_error = my_error_handler;
79 csv_options.cb_arg = &env; 79 csv_options.cb_error_arg = &env;
80 80
81 /* setup csv-Object with custom options */ 81 /* setup csv-Object with custom options */
82 csv_t csv[1]; 82 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
50csv_fatal_error(const char *msg, const csv_options_t * const csv_options) 50csv_fatal_error(const char *msg, const csv_options_t * const csv_options)
51{ 51{
52 if ( csv_options->cb_error != NULL ) { 52 if ( csv_options->cb_error != NULL ) {
53 (*csv_options->cb_error)(msg, csv_options->cb_arg); 53 (*csv_options->cb_error)(msg, csv_options->cb_error_arg);
54 } 54 }
55 55
56 fprintf(stderr, "fatal error: %s\n", msg); 56 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
95 95
96 if ( csv_string->pos == csv_string->cap ) { /* grow if needed */ 96 if ( csv_string->pos == csv_string->cap ) { /* grow if needed */
97 if ( csv_string->str == NULL ) { /* first call? */ 97 if ( csv_string->str == NULL ) { /* first call? */
98 if ( (csv_string->str = csv_options->cb_allocate(INITIAL_CAP, 1, csv_options->cb_arg)) == NULL ) { 98 if ( (csv_string->str = csv_options->cb_allocate(INITIAL_CAP, 1, csv_options->cb_memory_arg)) == NULL ) {
99 csv_fatal_error("out of memory...", csv_options); 99 csv_fatal_error("out of memory...", csv_options);
100 return; 100 return;
101 } 101 }
@@ -104,7 +104,7 @@ csv_string_append(csv_string_t *csv_string, char ch, const csv_options_t * const
104 } 104 }
105 else { /* subsequent call */ 105 else { /* subsequent call */
106 int cap = (csv_string->cap * 3) / 2; /* *= 1.5 */ 106 int cap = (csv_string->cap * 3) / 2; /* *= 1.5 */
107 char *str = csv_options->cb_reallocate(csv_string->str, cap, 1, csv_options->cb_arg); 107 char *str = csv_options->cb_reallocate(csv_string->str, cap, 1, csv_options->cb_memory_arg);
108 if ( str == NULL ) { 108 if ( str == NULL ) {
109 csv_fatal_error("out of memory...", csv_options); 109 csv_fatal_error("out of memory...", csv_options);
110 return; 110 return;
@@ -123,7 +123,7 @@ csv_string_free(csv_string_t *csv_string, const csv_options_t * const csv_option
123 assert(csv_string != NULL); 123 assert(csv_string != NULL);
124 assert(csv_options != NULL); 124 assert(csv_options != NULL);
125 125
126 csv_options->cb_free(csv_string->str, csv_string->cap, csv_options->cb_arg); 126 csv_options->cb_free(csv_string->str, csv_string->cap, csv_options->cb_memory_arg);
127 127
128 /* call *_init() for sane default values; prevent possible double-free */ 128 /* call *_init() for sane default values; prevent possible double-free */
129 csv_string_init(csv_string); 129 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
159 159
160 if ( csv_field->pos == csv_field->cap ) { /* grow if needed */ 160 if ( csv_field->pos == csv_field->cap ) { /* grow if needed */
161 if ( csv_field->fields == NULL ) { 161 if ( csv_field->fields == NULL ) {
162 if ( (csv_field->fields = csv_options->cb_allocate(INITIAL_CAP, sizeof(csv_field->fields[0]), csv_options->cb_arg)) == NULL ) { 162 if ( (csv_field->fields = csv_options->cb_allocate(INITIAL_CAP, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg)) == NULL ) {
163 csv_fatal_error("out of memory", csv_options); 163 csv_fatal_error("out of memory", csv_options);
164 return; 164 return;
165 } 165 }
@@ -168,7 +168,7 @@ csv_field_append(csv_field_t *csv_field, int idx, const csv_options_t * const cs
168 } 168 }
169 else { 169 else {
170 int cap = (csv_field->cap * 3) / 2; /* *= 1.5 */ 170 int cap = (csv_field->cap * 3) / 2; /* *= 1.5 */
171 int *fields = csv_options->cb_reallocate(csv_field->fields, cap, sizeof(csv_field->fields[0]), csv_options->cb_arg); 171 int *fields = csv_options->cb_reallocate(csv_field->fields, cap, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg);
172 if ( fields == NULL ) { 172 if ( fields == NULL ) {
173 csv_fatal_error("out of memory", csv_options); 173 csv_fatal_error("out of memory", csv_options);
174 return; 174 return;
@@ -187,7 +187,7 @@ csv_field_free(csv_field_t *csv_field, const csv_options_t * const csv_options)
187 assert(csv_field != NULL); 187 assert(csv_field != NULL);
188 assert(csv_options != NULL); 188 assert(csv_options != NULL);
189 189
190 csv_options->cb_free(csv_field->fields, csv_field->cap, csv_options->cb_arg); 190 csv_options->cb_free(csv_field->fields, csv_field->cap, csv_options->cb_memory_arg);
191 191
192 csv_field_init(csv_field); 192 csv_field_init(csv_field);
193} 193}
diff --git a/csv.h b/csv.h
index 3a529b7..fe59f5b 100644
--- a/csv.h
+++ b/csv.h
@@ -11,11 +11,14 @@ extern "C" {
11typedef struct { 11typedef struct {
12 char quote_symbol; 12 char quote_symbol;
13 char sep_symbol; 13 char sep_symbol;
14 void *cb_arg; 14
15 void (*cb_error)(const char *, void *); 15 void (*cb_error)(const char *, void *);
16 void *cb_error_arg;
17
16 void *(*cb_allocate)(size_t, size_t, void *); 18 void *(*cb_allocate)(size_t, size_t, void *);
17 void *(*cb_reallocate)(void *, size_t, size_t, void *); 19 void *(*cb_reallocate)(void *, size_t, size_t, void *);
18 void (*cb_free)(void *, size_t sz, void *); 20 void (*cb_free)(void *, size_t sz, void *);
21 void *cb_memory_arg;
19} csv_options_t; 22} csv_options_t;
20 23
21extern const csv_options_t csv_default_options; 24extern const csv_options_t csv_default_options;