From db6807fd6644b1e129008ff9622384cef4e48654 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 5 Jul 2020 11:54:28 +0200 Subject: Formatiere den Quellcode mit clang-format --- csv.c | 108 +++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 57 insertions(+), 51 deletions(-) (limited to 'csv.c') diff --git a/csv.c b/csv.c index 71c0e0f..2948911 100644 --- a/csv.c +++ b/csv.c @@ -1,29 +1,30 @@ +#include "csv.h" + +#include #include #include -#include #include -#include "csv.h" #ifndef CSV_DEFAULT_DELIMITER -#define CSV_DEFAULT_DELIMITER '"' +# define CSV_DEFAULT_DELIMITER '"' #endif #ifndef CSV_DEFAULT_SEPARATOR -#define CSV_DEFAULT_SEPARATOR ',' +# define CSV_DEFAULT_SEPARATOR ',' #endif #if defined(__GNUC__) || defined(__clang__) -#define mul_overflow __builtin_mul_overflow +# define mul_overflow __builtin_mul_overflow #else static inline int mul_overflow(size_t a, size_t b, size_t *out) { - *out = a * b; - return ( a != 0 && (*out / a) != b ); + *out = a * b; + return (a != 0 && (*out / a) != b); } #endif -#define UNUSED(x) (void)(sizeof((x), 0)) +#define UNUSED(x) (void) (sizeof((x), 0)) // === CSV-MEMORY Interface === @@ -66,16 +67,16 @@ csv_mem_free(void *ptr, size_t n, size_t sz, void *cb_arg) const csv_options_t csv_default_options = { .field_delimiter = CSV_DEFAULT_DELIMITER, .field_separator = CSV_DEFAULT_SEPARATOR, - .cb_error = NULL, - .cb_allocate = csv_mem_allocate, - .cb_reallocate = csv_mem_reallocate, - .cb_free = csv_mem_free + .cb_error = NULL, + .cb_allocate = csv_mem_allocate, + .cb_reallocate = csv_mem_reallocate, + .cb_free = csv_mem_free }; // === CSV-ERROR Interface === static void -csv_fatal_error(csv_err_t csv_err, const csv_options_t * const csv_options) +csv_fatal_error(csv_err_t csv_err, const csv_options_t *const csv_options) { if ( csv_options->cb_error != NULL ) { (*csv_options->cb_error)(csv_err, csv_options->cb_error_arg); @@ -109,27 +110,27 @@ csv_string_empty(csv_string_t *csv_string) { assert(csv_string != NULL); - return ( csv_string->pos == 0 ) ? 1 : 0; + return (csv_string->pos == 0) ? 1 : 0; } static inline void -csv_string_append(csv_string_t *csv_string, char ch, const csv_options_t * const csv_options) +csv_string_append(csv_string_t *csv_string, char ch, const csv_options_t *const csv_options) { assert(csv_string != NULL); assert(csv_options != NULL); static const int INITIAL_CAP = 16; - if ( csv_string->pos == csv_string->cap ) { // grow if needed - if ( csv_string->str == NULL ) { // first call? + if ( csv_string->pos == csv_string->cap ) { // grow if needed + if ( csv_string->str == NULL ) { // first call? if ( (csv_string->str = csv_options->cb_allocate(INITIAL_CAP, 1, csv_options->cb_memory_arg)) == NULL ) { csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); return; } csv_string->cap = INITIAL_CAP; } - else { // subsequent call - int cap = (csv_string->cap * 3) / 2; // *= 1.5 + else { // subsequent call + int cap = (csv_string->cap * 3) / 2; // *= 1.5 char *str = csv_options->cb_reallocate(csv_string->str, cap, 1, csv_options->cb_memory_arg); if ( str == NULL ) { csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); @@ -140,11 +141,11 @@ csv_string_append(csv_string_t *csv_string, char ch, const csv_options_t * const } } - csv_string->str[csv_string->pos++] = ch; // append char + csv_string->str[csv_string->pos++] = ch; // append char } static inline void -csv_string_free(csv_string_t *csv_string, const csv_options_t * const csv_options) +csv_string_free(csv_string_t *csv_string, const csv_options_t *const csv_options) { assert(csv_string != NULL); assert(csv_options != NULL); @@ -163,8 +164,8 @@ csv_field_init(csv_field_t *csv_field) assert(csv_field != NULL); csv_field->fields = NULL; - csv_field->cap = 0; - csv_field->pos = 0; + csv_field->cap = 0; + csv_field->pos = 0; } static inline void @@ -176,30 +177,30 @@ csv_field_reset(csv_field_t *csv_field) } static inline void -csv_field_append(csv_field_t *csv_field, int idx, const csv_options_t * const csv_options) +csv_field_append(csv_field_t *csv_field, int idx, const csv_options_t *const csv_options) { assert(csv_field != NULL); assert(csv_options != NULL); static const size_t INITIAL_CAP = 16; - if ( csv_field->pos == csv_field->cap ) { // grow if needed - if ( csv_field->fields == NULL ) { // first call + if ( csv_field->pos == csv_field->cap ) { // grow if needed + if ( csv_field->fields == NULL ) { // first call if ( (csv_field->fields = csv_options->cb_allocate(INITIAL_CAP, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg)) == NULL ) { csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); return; } csv_field->cap = INITIAL_CAP; } - else { // subsequent call - int cap = (csv_field->cap * 3) / 2; // *= 1.5 + else { // subsequent call + int cap = (csv_field->cap * 3) / 2; // *= 1.5 int *fields = csv_options->cb_reallocate(csv_field->fields, cap, sizeof(csv_field->fields[0]), csv_options->cb_memory_arg); if ( fields == NULL ) { csv_fatal_error(CSV_ERR_OUT_OF_MEMORY, csv_options); return; } csv_field->fields = fields; - csv_field->cap = cap; + csv_field->cap = cap; } } @@ -207,7 +208,7 @@ csv_field_append(csv_field_t *csv_field, int idx, const csv_options_t * const cs } static inline void -csv_field_free(csv_field_t *csv_field, const csv_options_t * const csv_options) +csv_field_free(csv_field_t *csv_field, const csv_options_t *const csv_options) { assert(csv_field != NULL); assert(csv_options != NULL); @@ -228,7 +229,7 @@ csv_init(csv_t *csv) } void -csv_init_opt(csv_t *csv, const csv_options_t * const csv_options) +csv_init_opt(csv_t *csv, const csv_options_t *const csv_options) { assert(csv != NULL); @@ -255,7 +256,7 @@ csv_cleanup(csv_t *csv) } int -csv_nfields(const csv_t * const csv) +csv_nfields(const csv_t *const csv) { assert(csv != NULL); @@ -266,7 +267,7 @@ csv_nfields(const csv_t * const csv) } const char * -csv_field(const csv_t * const csv, int idx) +csv_field(const csv_t *const csv, int idx) { assert(csv != NULL); assert(idx >= 0 && idx < csv->csv_field.pos); @@ -310,12 +311,12 @@ csv_read(csv_t *csv, FILE *in) }; register const int DELIM = csv->csv_options->field_delimiter; - register const int SEP = csv->csv_options->field_separator; + register const int SEP = csv->csv_options->field_separator; csv_string_reset(&csv->csv_string); csv_field_reset(&csv->csv_field); - for ( int state = STATE_START_FIELD; ; ) { + for ( int state = STATE_START_FIELD;; ) { int ch; switch ( state ) { @@ -326,9 +327,9 @@ csv_read(csv_t *csv, FILE *in) if ( ch == EOF ) { state = STATE_END_FILE; } - else if ( ch == '\r' ) { // test for CR.. + else if ( ch == '\r' ) { // test for CR.. ch = getc(in); - if ( ch != '\n' ) { // ..LF + if ( ch != '\n' ) { // ..LF ungetc(ch, in); } state = STATE_END_LINE; @@ -379,7 +380,7 @@ csv_read(csv_t *csv, FILE *in) } else { csv_string_append(&csv->csv_string, DELIM, csv->csv_options); - ungetc(ch, in); // we have read too far... Put the character back! + ungetc(ch, in); // we have read too far... Put the character back! } } else { @@ -434,7 +435,7 @@ csv_read(csv_t *csv, FILE *in) if ( csv_string_empty(&csv->csv_string) ) { csv_cleanup(csv); - return EOF; // EOF reached + return EOF; // EOF reached } /* @@ -458,18 +459,24 @@ const char * csv_err_str(csv_err_t csv_err) { switch ( csv_err ) { - case CSV_ERR_OK: return "no error"; - case CSV_ERR_OUT_OF_MEMORY: return "out of memory"; - case CSV_ERR_OUT_OF_RANGE: return "index out of range"; - case CSV_ERR_IO_READ: return "read error"; - case CSV_ERR_IO_WRITE: return "write error"; - default: return "unknown error"; + case CSV_ERR_OK: + return "no error"; + case CSV_ERR_OUT_OF_MEMORY: + return "out of memory"; + case CSV_ERR_OUT_OF_RANGE: + return "index out of range"; + case CSV_ERR_IO_READ: + return "read error"; + case CSV_ERR_IO_WRITE: + return "write error"; + default: + return "unknown error"; } // NOT REACHED } static void -csv_write_field(FILE *out, const char *field, const csv_options_t * const csv_options) +csv_write_field(FILE *out, const char *field, const csv_options_t *const csv_options) { assert(csv_options != NULL); assert(field != NULL); @@ -499,7 +506,7 @@ csv_write_field(FILE *out, const char *field, const csv_options_t * const csv_op } void -csv_write(FILE *out, const csv_t * const csv) +csv_write(FILE *out, const csv_t *const csv) { assert(out != NULL); assert(csv != NULL); @@ -510,8 +517,8 @@ csv_write(FILE *out, const csv_t * const csv) register const int SEP = csv->csv_options->field_separator; if ( 0 < csv->csv_field.pos ) { - const char *str = csv->csv_string.str; - const int *field = csv->csv_field.fields; + const char *str = csv->csv_string.str; + const int * field = csv->csv_field.fields; csv_write_field(out, &str[*field++], csv->csv_options); @@ -529,7 +536,7 @@ csv_write(FILE *out, const csv_t * const csv) } void -csv_write_ex(FILE *out, int n, const char *fields[], const csv_options_t * csv_options) +csv_write_ex(FILE *out, int n, const char *fields[], const csv_options_t *csv_options) { assert(out != NULL); assert(fields != NULL); @@ -558,4 +565,3 @@ csv_write_ex(FILE *out, int n, const char *fields[], const csv_options_t * csv_o csv_fatal_error(CSV_ERR_IO_WRITE, csv_options); } } - -- cgit v1.3