diff options
| -rw-r--r-- | csv-test.c | 21 |
1 files changed, 14 insertions, 7 deletions
| @@ -9,7 +9,7 @@ | |||
| 9 | 9 | ||
| 10 | #define INLINE static inline | 10 | #define INLINE static inline |
| 11 | 11 | ||
| 12 | /* === CSV Options Interface === */ | 12 | /* === CSV-OPTIONS Interface === */ |
| 13 | 13 | ||
| 14 | typedef struct { | 14 | typedef struct { |
| 15 | char quote_symbol; | 15 | char quote_symbol; |
| @@ -18,7 +18,9 @@ typedef struct { | |||
| 18 | void (*cb_func)(const char *, void *); | 18 | void (*cb_func)(const char *, void *); |
| 19 | } csv_options_t; | 19 | } csv_options_t; |
| 20 | 20 | ||
| 21 | static csv_options_t default_csv_options = { | 21 | extern const csv_options_t csv_default_options; |
| 22 | |||
| 23 | const csv_options_t csv_default_options = { | ||
| 22 | .quote_symbol = '"', | 24 | .quote_symbol = '"', |
| 23 | .sep_symbol = ',', | 25 | .sep_symbol = ',', |
| 24 | .cb_func = NULL | 26 | .cb_func = NULL |
| @@ -183,13 +185,13 @@ csv_field_free(csv_field_t *csv_field) | |||
| 183 | /* === CSV Interface === */ | 185 | /* === CSV Interface === */ |
| 184 | 186 | ||
| 185 | typedef struct { | 187 | typedef struct { |
| 186 | csv_options_t *csv_options; | 188 | const csv_options_t *csv_options; |
| 187 | csv_string_t csv_string; | 189 | csv_string_t csv_string; |
| 188 | csv_field_t csv_field; | 190 | csv_field_t csv_field; |
| 189 | } csv_t; | 191 | } csv_t; |
| 190 | 192 | ||
| 191 | void csv_init(csv_t *csv); | 193 | void csv_init(csv_t *csv); |
| 192 | void csv_init_opt(csv_t *csv, csv_options_t *csv_options); | 194 | void csv_init_opt(csv_t *csv, const csv_options_t * const csv_options); |
| 193 | void csv_free(csv_t *csv); | 195 | void csv_free(csv_t *csv); |
| 194 | int csv_nfields(csv_t *csv); | 196 | int csv_nfields(csv_t *csv); |
| 195 | const char * csv_field(csv_t *csv, int idx); | 197 | const char * csv_field(csv_t *csv, int idx); |
| @@ -201,15 +203,20 @@ csv_init(csv_t *csv) | |||
| 201 | { | 203 | { |
| 202 | assert(csv != NULL); | 204 | assert(csv != NULL); |
| 203 | 205 | ||
| 204 | csv_init_opt(csv, &default_csv_options); | 206 | csv_init_opt(csv, NULL); |
| 205 | } | 207 | } |
| 206 | 208 | ||
| 207 | void | 209 | void |
| 208 | csv_init_opt(csv_t *csv, csv_options_t *csv_options) | 210 | csv_init_opt(csv_t *csv, const csv_options_t * const csv_options) |
| 209 | { | 211 | { |
| 210 | assert(csv != NULL); | 212 | assert(csv != NULL); |
| 211 | 213 | ||
| 212 | csv->csv_options = ( csv_options != NULL ) ? csv_options : &default_csv_options; | 214 | if ( csv_options != NULL ) { |
| 215 | csv->csv_options = csv_options; | ||
| 216 | } | ||
| 217 | else { | ||
| 218 | csv->csv_options = &csv_default_options; | ||
| 219 | } | ||
| 213 | 220 | ||
| 214 | csv_string_init(&csv->csv_string); | 221 | csv_string_init(&csv->csv_string); |
| 215 | csv_field_init(&csv->csv_field); | 222 | csv_field_init(&csv->csv_field); |
