aboutsummaryrefslogtreecommitdiff
path: root/csv.c
diff options
context:
space:
mode:
Diffstat (limited to 'csv.c')
-rw-r--r--csv.c26
1 files changed, 15 insertions, 11 deletions
diff --git a/csv.c b/csv.c
index d36eb63..4f3cd14 100644
--- a/csv.c
+++ b/csv.c
@@ -44,34 +44,34 @@ mul_overflow(size_t a, size_t b, size_t *out)
44#endif 44#endif
45 45
46static void * 46static void *
47csv_mem_allocate(size_t n, size_t sz, void *cb_arg) 47csv_mem_allocate(size_t number, size_t size, void *cb_arg)
48{ 48{
49 UNUSED(cb_arg); 49 UNUSED(cb_arg);
50 50
51 size_t len = 0; 51 size_t len = 0;
52 if ( n == 0 || sz == 0 || mul_overflow(n, sz, &len) ) { 52 if ( number == 0 || size == 0 || mul_overflow(number, size, &len) ) {
53 return NULL; 53 return NULL;
54 } 54 }
55 return malloc(len); 55 return malloc(len);
56} 56}
57 57
58static void * 58static void *
59csv_mem_reallocate(void *ptr, size_t n, size_t sz, void *cb_arg) 59csv_mem_reallocate(void *ptr, size_t number, size_t size, void *cb_arg)
60{ 60{
61 UNUSED(cb_arg); 61 UNUSED(cb_arg);
62 62
63 size_t len = 0; 63 size_t len = 0;
64 if ( n == 0 || sz == 0 || mul_overflow(n, sz, &len) ) { 64 if ( number == 0 || size == 0 || mul_overflow(number, size, &len) ) {
65 return NULL; 65 return NULL;
66 } 66 }
67 return realloc(ptr, len); 67 return realloc(ptr, len);
68} 68}
69 69
70static void 70static void
71csv_mem_free(void *ptr, size_t n, size_t sz, void *cb_arg) 71csv_mem_free(void *ptr, size_t number, size_t size, void *cb_arg)
72{ 72{
73 UNUSED(n); 73 UNUSED(number);
74 UNUSED(sz); 74 UNUSED(size);
75 UNUSED(cb_arg); 75 UNUSED(cb_arg);
76 76
77 free(ptr); 77 free(ptr);
@@ -138,10 +138,12 @@ csv_string_append(csv_string_t *csv_string, int ch, const csv_options_t *const c
138 138
139 if ( csv_string->pos == csv_string->cap ) { // grow if needed 139 if ( csv_string->pos == csv_string->cap ) { // grow if needed
140 if ( csv_string->str == NULL ) { // first call? 140 if ( csv_string->str == NULL ) { // first call?
141 if ( (csv_string->str = csv_options->cb_allocate(INITIAL_CAP, 1, csv_options->cb_memory_arg)) == NULL ) { 141 char *str = csv_options->cb_allocate(INITIAL_CAP, 1, csv_options->cb_memory_arg);
142 if ( str == NULL ) {
142 csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); 143 csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options);
143 return; 144 return;
144 } 145 }
146 csv_string->str = str;
145 csv_string->cap = INITIAL_CAP; 147 csv_string->cap = INITIAL_CAP;
146 } 148 }
147 else { // subsequent call 149 else { // subsequent call
@@ -201,11 +203,13 @@ csv_field_append(csv_field_t *csv_field, size_t idx, const csv_options_t *const
201 203
202 if ( csv_field->pos == csv_field->cap ) { // grow if needed 204 if ( csv_field->pos == csv_field->cap ) { // grow if needed
203 if ( csv_field->fields == NULL ) { // first call 205 if ( csv_field->fields == NULL ) { // first call
204 if ( (csv_field->fields = csv_options->cb_allocate(INITIAL_CAP, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg)) == NULL ) { 206 size_t *fields = csv_options->cb_allocate(INITIAL_CAP, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg);
207 if ( fields == NULL ) {
205 csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); 208 csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options);
206 return; 209 return;
207 } 210 }
208 csv_field->cap = INITIAL_CAP; 211 csv_field->fields = fields;
212 csv_field->cap = INITIAL_CAP;
209 } 213 }
210 else { // subsequent call 214 else { // subsequent call
211 size_t cap = (csv_field->cap * 3) / 2; // *= 1.5 215 size_t cap = (csv_field->cap * 3) / 2; // *= 1.5
@@ -219,7 +223,7 @@ csv_field_append(csv_field_t *csv_field, size_t idx, const csv_options_t *const
219 } 223 }
220 } 224 }
221 225
222 csv_field->fields[csv_field->pos++] = idx; // append idx 226 csv_field->fields[csv_field->pos++] = idx; // append index
223} 227}
224 228
225static inline void 229static inline void