diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-09-13 10:53:05 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-09-13 10:53:05 +0200 |
| commit | 41d8eaca5bbe16144cbb0d98c8522c8f6a137c1d (patch) | |
| tree | 0074057a35738f9ec584e58b6ada59eb25b7a1c1 /csv.c | |
| parent | 2ff8751874e7b3c1fc8480215d98cca3c48ea37b (diff) | |
| download | libcsv-41d8eaca5bbe16144cbb0d98c8522c8f6a137c1d.tar.gz libcsv-41d8eaca5bbe16144cbb0d98c8522c8f6a137c1d.tar.bz2 libcsv-41d8eaca5bbe16144cbb0d98c8522c8f6a137c1d.zip | |
Allgemeine Funktion zum Vergrößern eines Array implementiert
Diffstat (limited to 'csv.c')
| -rw-r--r-- | csv.c | 17 |
1 files changed, 11 insertions, 6 deletions
| @@ -1,6 +1,7 @@ | |||
| 1 | #include "csv.h" | 1 | #include "csv.h" |
| 2 | 2 | ||
| 3 | #include <assert.h> | 3 | #include <assert.h> |
| 4 | #include <stddef.h> | ||
| 4 | #include <stdio.h> | 5 | #include <stdio.h> |
| 5 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| 6 | #include <string.h> | 7 | #include <string.h> |
| @@ -100,16 +101,22 @@ csv_string_isempty(csv_string_t *csv_string) | |||
| 100 | return (csv_string->pos == 0) ? 1 : 0; | 101 | return (csv_string->pos == 0) ? 1 : 0; |
| 101 | } | 102 | } |
| 102 | 103 | ||
| 104 | static inline size_t | ||
| 105 | growth_strategy(size_t current_cap) | ||
| 106 | { | ||
| 107 | static const size_t INITIAL_CAP = 16; | ||
| 108 | |||
| 109 | return (current_cap == 0) ? INITIAL_CAP : (current_cap * 3) / 2; | ||
| 110 | } | ||
| 111 | |||
| 103 | static inline void | 112 | static inline void |
| 104 | csv_string_grow_if_needed(csv_string_t *csv_string, const csv_options_t *const csv_options) | 113 | csv_string_grow_if_needed(csv_string_t *csv_string, const csv_options_t *const csv_options) |
| 105 | { | 114 | { |
| 106 | assert(csv_string != NULL); | 115 | assert(csv_string != NULL); |
| 107 | assert(csv_options != NULL); | 116 | assert(csv_options != NULL); |
| 108 | 117 | ||
| 109 | static const size_t INITIAL_CAP = 16; | ||
| 110 | |||
| 111 | if ( csv_string->pos == csv_string->cap ) { | 118 | if ( csv_string->pos == csv_string->cap ) { |
| 112 | size_t cap = (csv_string->str == NULL) ? INITIAL_CAP : (csv_string->cap * 3) / 2; | 119 | size_t cap = growth_strategy(csv_string->cap); |
| 113 | char *str = csv_options->cb_reallocate(csv_string->str, cap, 1, csv_options->cb_memory_arg); | 120 | char *str = csv_options->cb_reallocate(csv_string->str, cap, 1, csv_options->cb_memory_arg); |
| 114 | if ( str == NULL ) { | 121 | if ( str == NULL ) { |
| 115 | csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); | 122 | csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); |
| @@ -169,10 +176,8 @@ csv_field_grow_if_needed(csv_field_t *csv_field, const csv_options_t *const csv_ | |||
| 169 | assert(csv_field != NULL); | 176 | assert(csv_field != NULL); |
| 170 | assert(csv_options != NULL); | 177 | assert(csv_options != NULL); |
| 171 | 178 | ||
| 172 | static const size_t INITIAL_CAP = 16; | ||
| 173 | |||
| 174 | if ( csv_field->pos == csv_field->cap ) { | 179 | if ( csv_field->pos == csv_field->cap ) { |
| 175 | size_t cap = (csv_field->fields == NULL) ? INITIAL_CAP : (csv_field->cap * 3) / 2; | 180 | size_t cap = growth_strategy(csv_field->cap); |
| 176 | size_t *fields = csv_options->cb_reallocate(csv_field->fields, cap, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg); | 181 | size_t *fields = csv_options->cb_reallocate(csv_field->fields, cap, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg); |
| 177 | if ( fields == NULL ) { | 182 | if ( fields == NULL ) { |
| 178 | csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); | 183 | csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); |
