diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-06-18 10:53:43 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-06-18 10:53:43 +0200 |
| commit | 575ae35abccb0a22d17e3121c19dd59a71aebf3f (patch) | |
| tree | 6fbddb4b4f4ef1b0b14da8fb7107305eab28b090 | |
| parent | 36c50e4e77d472e9b7838e1f6385f36ddb52d258 (diff) | |
| download | libcsv-575ae35abccb0a22d17e3121c19dd59a71aebf3f.tar.gz libcsv-575ae35abccb0a22d17e3121c19dd59a71aebf3f.tar.bz2 libcsv-575ae35abccb0a22d17e3121c19dd59a71aebf3f.zip | |
header datei
| -rw-r--r-- | csv-test.c | 46 | ||||
| -rw-r--r-- | csv.h | 51 |
2 files changed, 57 insertions, 40 deletions
| @@ -7,6 +7,8 @@ | |||
| 7 | #include <assert.h> | 7 | #include <assert.h> |
| 8 | #include <signal.h> | 8 | #include <signal.h> |
| 9 | 9 | ||
| 10 | #include "csv.h" | ||
| 11 | |||
| 10 | #define INLINE static inline | 12 | #define INLINE static inline |
| 11 | 13 | ||
| 12 | /* === CSV-MEMORY Interface === */ | 14 | /* === CSV-MEMORY Interface === */ |
| @@ -37,18 +39,6 @@ csv_mem_free(void *ptr, void *cb_arg) | |||
| 37 | 39 | ||
| 38 | /* === CSV-OPTIONS Interface === */ | 40 | /* === CSV-OPTIONS Interface === */ |
| 39 | 41 | ||
| 40 | typedef struct { | ||
| 41 | char quote_symbol; | ||
| 42 | char sep_symbol; | ||
| 43 | void *cb_arg; | ||
| 44 | void (*cb_error)(const char *, void *); | ||
| 45 | void *(*cb_allocate)(size_t, size_t, void *); | ||
| 46 | void *(*cb_reallocate)(void *, size_t, size_t, void *); | ||
| 47 | void (*cb_free)(void *, void *); | ||
| 48 | } csv_options_t; | ||
| 49 | |||
| 50 | extern const csv_options_t csv_default_options; | ||
| 51 | |||
| 52 | const csv_options_t csv_default_options = { | 42 | const csv_options_t csv_default_options = { |
| 53 | .quote_symbol = '"', | 43 | .quote_symbol = '"', |
| 54 | .sep_symbol = ',', | 44 | .sep_symbol = ',', |
| @@ -73,11 +63,6 @@ csv_fatal_error(const char *msg, const csv_options_t * const csv_options) | |||
| 73 | 63 | ||
| 74 | /* === CSV-STRING Interface === */ | 64 | /* === CSV-STRING Interface === */ |
| 75 | 65 | ||
| 76 | typedef struct { | ||
| 77 | char *str; | ||
| 78 | int cap, pos; | ||
| 79 | } csv_string_t; | ||
| 80 | |||
| 81 | INLINE void | 66 | INLINE void |
| 82 | csv_string_init(csv_string_t *csv_string) | 67 | csv_string_init(csv_string_t *csv_string) |
| 83 | { | 68 | { |
| @@ -150,11 +135,6 @@ csv_string_free(csv_string_t *csv_string, const csv_options_t * const csv_option | |||
| 150 | 135 | ||
| 151 | /* === CSV-FIELD Interface === */ | 136 | /* === CSV-FIELD Interface === */ |
| 152 | 137 | ||
| 153 | typedef struct { | ||
| 154 | int *fields; | ||
| 155 | int cap, pos; | ||
| 156 | } csv_field_t; | ||
| 157 | |||
| 158 | INLINE void | 138 | INLINE void |
| 159 | csv_field_init(csv_field_t *csv_field) | 139 | csv_field_init(csv_field_t *csv_field) |
| 160 | { | 140 | { |
| @@ -218,20 +198,6 @@ csv_field_free(csv_field_t *csv_field, const csv_options_t * const csv_options) | |||
| 218 | 198 | ||
| 219 | /* === CSV Interface === */ | 199 | /* === CSV Interface === */ |
| 220 | 200 | ||
| 221 | typedef struct { | ||
| 222 | const csv_options_t *csv_options; | ||
| 223 | csv_string_t csv_string; | ||
| 224 | csv_field_t csv_field; | ||
| 225 | } csv_t; | ||
| 226 | |||
| 227 | void csv_init(csv_t *csv); | ||
| 228 | void csv_init_opt(csv_t *csv, const csv_options_t * const csv_options); | ||
| 229 | void csv_free(csv_t *csv); | ||
| 230 | int csv_nfields(csv_t *csv); | ||
| 231 | const char * csv_field(csv_t *csv, int idx); | ||
| 232 | int csv_read(csv_t *csv, FILE *in); | ||
| 233 | void csv_write(const csv_options_t * const csv_options, int n, const char *fields[], FILE *out); | ||
| 234 | |||
| 235 | void | 201 | void |
| 236 | csv_init(csv_t *csv) | 202 | csv_init(csv_t *csv) |
| 237 | { | 203 | { |
| @@ -436,7 +402,7 @@ csv_read(csv_t *csv, FILE *in) | |||
| 436 | } | 402 | } |
| 437 | 403 | ||
| 438 | static void | 404 | static void |
| 439 | csv_write_field(const csv_options_t * const csv_options, const char *field, FILE *out) | 405 | csv_write_field(FILE *out, const char *field, const csv_options_t * const csv_options) |
| 440 | { | 406 | { |
| 441 | assert(csv_options != NULL); | 407 | assert(csv_options != NULL); |
| 442 | assert(field != NULL); | 408 | assert(field != NULL); |
| @@ -462,7 +428,7 @@ csv_write_field(const csv_options_t * const csv_options, const char *field, FILE | |||
| 462 | } | 428 | } |
| 463 | 429 | ||
| 464 | void | 430 | void |
| 465 | csv_write(const csv_options_t * const csv_options, int n, const char *fields[], FILE *out) | 431 | csv_write(FILE *out, int n, const char *fields[], const csv_options_t * const csv_options) |
| 466 | { | 432 | { |
| 467 | assert(csv_options != NULL); | 433 | assert(csv_options != NULL); |
| 468 | assert(fields != NULL); | 434 | assert(fields != NULL); |
| @@ -472,12 +438,12 @@ csv_write(const csv_options_t * const csv_options, int n, const char *fields[], | |||
| 472 | 438 | ||
| 473 | /* process first field */ | 439 | /* process first field */ |
| 474 | if ( 0 != n && fields[0] != NULL ) { | 440 | if ( 0 != n && fields[0] != NULL ) { |
| 475 | csv_write_field(csv_options, fields[0], out); | 441 | csv_write_field(out, fields[0], csv_options); |
| 476 | 442 | ||
| 477 | /* process next fields */ | 443 | /* process next fields */ |
| 478 | for ( int i = 1; i != n && fields[i] != NULL; ++i ) { | 444 | for ( int i = 1; i != n && fields[i] != NULL; ++i ) { |
| 479 | putc(SEP, out); | 445 | putc(SEP, out); |
| 480 | csv_write_field(csv_options, fields[i], out); | 446 | csv_write_field(out, fields[i], csv_options); |
| 481 | } | 447 | } |
| 482 | } | 448 | } |
| 483 | fprintf(out, "\r\n"); | 449 | fprintf(out, "\r\n"); |
| @@ -0,0 +1,51 @@ | |||
| 1 | #ifndef ITS1_CSV_H_INCLUDED | ||
| 2 | #define ITS1_CSV_H_INCLUDED | ||
| 3 | |||
| 4 | #include <stdio.h> /* FILE */ | ||
| 5 | #include <stddef.h> /* size_t */ | ||
| 6 | |||
| 7 | #ifdef __cplusplus | ||
| 8 | extern "C" { | ||
| 9 | #endif | ||
| 10 | |||
| 11 | typedef struct { | ||
| 12 | char quote_symbol; | ||
| 13 | char sep_symbol; | ||
| 14 | void *cb_arg; | ||
| 15 | void (*cb_error)(const char *, void *); | ||
| 16 | void *(*cb_allocate)(size_t, size_t, void *); | ||
| 17 | void *(*cb_reallocate)(void *, size_t, size_t, void *); | ||
| 18 | void (*cb_free)(void *, void *); | ||
| 19 | } csv_options_t; | ||
| 20 | |||
| 21 | extern const csv_options_t csv_default_options; | ||
| 22 | |||
| 23 | typedef struct { | ||
| 24 | char *str; | ||
| 25 | int cap, pos; | ||
| 26 | } csv_string_t; | ||
| 27 | |||
| 28 | typedef struct { | ||
| 29 | int *fields; | ||
| 30 | int cap, pos; | ||
| 31 | } csv_field_t; | ||
| 32 | |||
| 33 | typedef struct { | ||
| 34 | const csv_options_t *csv_options; | ||
| 35 | csv_string_t csv_string; | ||
| 36 | csv_field_t csv_field; | ||
| 37 | } csv_t; | ||
| 38 | |||
| 39 | void csv_init(csv_t *csv); | ||
| 40 | void csv_init_opt(csv_t *csv, const csv_options_t * const csv_options); | ||
| 41 | void csv_free(csv_t *csv); | ||
| 42 | int csv_nfields(csv_t *csv); | ||
| 43 | const char * csv_field(csv_t *csv, int idx); | ||
| 44 | int csv_read(csv_t *csv, FILE *in); | ||
| 45 | void csv_write(FILE *out, int n, const char *fields[], const csv_options_t * const csv_options); | ||
| 46 | |||
| 47 | #ifdef __cplusplus | ||
| 48 | } | ||
| 49 | #endif | ||
| 50 | |||
| 51 | #endif | ||
