From 0ba2797fdf4577588bcf7f5cac9d94ae43b8b31b Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Fri, 24 Jul 2020 12:23:33 +0200 Subject: Benutze size_t statt int bei Größenangaben MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.mk | 2 +- csv-perf.c | 2 +- csv-test.c | 38 +++++++++++++++++++++++++++++++------- csv.c | 28 ++++++++++++++-------------- csv.h | 12 ++++++------ 5 files changed, 53 insertions(+), 29 deletions(-) diff --git a/config.mk b/config.mk index 2ede312..3c8fd05 100644 --- a/config.mk +++ b/config.mk @@ -1,3 +1,3 @@ CC=cc -CFLAGS=-Wall -Wextra -pedantic -std=c99 -O2 +CFLAGS=-Wall -Wextra -Wsign-compare -Wsign-conversion -pedantic -std=c99 -O2 CFLAGS-TEST=$(CFLAGS) -fsanitize=address -ggdb -fprofile-instr-generate -fcoverage-mapping diff --git a/csv-perf.c b/csv-perf.c index 51bf9ff..37616e4 100644 --- a/csv-perf.c +++ b/csv-perf.c @@ -13,7 +13,7 @@ test(FILE *f) int line = 0; csv_t csv = { 0 }; - while ( csv_read(&csv, f) != EOF ) { + while ( csv_read(&csv, f) ) { ++line; } diff --git a/csv-test.c b/csv-test.c index 201e3f1..ab996b4 100644 --- a/csv-test.c +++ b/csv-test.c @@ -56,7 +56,7 @@ test_line_endings1(void) assert(strcmp(csv_field(&csv, 1), "E") == 0); assert(strcmp(csv_field(&csv, 2), "F") == 0); - assert(csv_read(&csv, f) == EOF); + assert(csv_read(&csv, f) == 0); FMEMCLOSE(f); } @@ -84,7 +84,7 @@ test_line_endings2(void) assert(strcmp(csv_field(&csv, 1), "E") == 0); assert(strcmp(csv_field(&csv, 2), "F") == 0); - assert(csv_read(&csv, f) == EOF); + assert(csv_read(&csv, f) == 0); FMEMCLOSE(f); } @@ -233,7 +233,7 @@ test_empty_fields(void) assert(strcmp(csv_field(&csv, 2), "") == 0); // EOF - assert(csv_read(&csv, f) == EOF); + assert(csv_read(&csv, f) == 0); FMEMCLOSE(f); } @@ -382,7 +382,7 @@ test_simple_fields(void) assert(strcmp(csv_field(&csv, 2), "C") == 0); // EOF - assert(csv_read(&csv, f) == EOF); + assert(csv_read(&csv, f) == 0); FMEMCLOSE(f); } @@ -405,7 +405,7 @@ test_quoted_fields(void) assert(strcmp(csv_field(&csv, 3), "foo \"baz\" bar") == 0); assert(strcmp(csv_field(&csv, 4), "foo \"baz\", bar") == 0); - assert(csv_read(&csv, f) == EOF); + assert(csv_read(&csv, f) == 0); FMEMCLOSE(f); } @@ -424,7 +424,7 @@ test_wrong_quoted_field(void) assert(csv_nfields(&csv) == 1); assert(strcmp(csv_field(&csv, 0), "foo") == 0); - assert(csv_read(&csv, f) == EOF); + assert(csv_read(&csv, f) == 0); FMEMCLOSE(f); } @@ -488,7 +488,7 @@ test_read_error(void) switch ( setjmp(env) ) { case CSV_ERR_OK: - assert(csv_read(&csv, f) == EOF); + assert(csv_read(&csv, f) == 0); break; case CSV_ERR_IO_READ: @@ -580,9 +580,33 @@ test_allocation_error1(void) FMEMCLOSE(f); } +void +show_version(void) +{ + const char *version = csv_version; + + printf("Version: "); + for ( ; *version; ++version ) { + putchar(*version); + } + + printf(", Build Date: "); + for ( ++version; *version; ++version ) { + putchar(*version); + } + + printf(", Build Time: "); + for ( ++version; *version; ++version ) { + putchar(*version); + } + putchar('\n'); +} + int main(void) { + show_version(); + test_empty_object(); test_line_endings1(); test_line_endings2(); diff --git a/csv.c b/csv.c index 3804095..ce8cb00 100644 --- a/csv.c +++ b/csv.c @@ -134,7 +134,7 @@ csv_string_append(csv_string_t *csv_string, char ch, const csv_options_t *const assert(csv_string != NULL); assert(csv_options != NULL); - static const int INITIAL_CAP = 16; + static const size_t INITIAL_CAP = 16; if ( csv_string->pos == csv_string->cap ) { // grow if needed if ( csv_string->str == NULL ) { // first call? @@ -144,9 +144,9 @@ csv_string_append(csv_string_t *csv_string, char ch, const csv_options_t *const } csv_string->cap = INITIAL_CAP; } - 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); + else { // subsequent call + size_t 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); return; @@ -192,7 +192,7 @@ 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, size_t idx, const csv_options_t *const csv_options) { assert(csv_field != NULL); assert(csv_options != NULL); @@ -207,9 +207,9 @@ csv_field_append(csv_field_t *csv_field, int idx, const csv_options_t *const csv } csv_field->cap = INITIAL_CAP; } - 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); + else { // subsequent call + size_t cap = (csv_field->cap * 3) / 2; // *= 1.5 + size_t *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; @@ -270,7 +270,7 @@ csv_cleanup(csv_t *csv) csv->csv_options = NULL; } -int +size_t csv_nfields(const csv_t *const csv) { assert(csv != NULL); @@ -279,7 +279,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, size_t idx) { assert(csv != NULL); assert(idx >= 0 && idx < csv->csv_field.pos); @@ -304,13 +304,13 @@ csv_read(csv_t *csv, FILE *in) if ( ferror(in) ) { csv_fatal_error(CSV_ERR_IO_READ, csv->csv_options); - return EOF; + return 0; } // do not try to read if EOF has already been seen if ( feof(in) ) { csv_cleanup(csv); - return EOF; + return 0; } enum { @@ -442,12 +442,12 @@ csv_read(csv_t *csv, FILE *in) case STATE_END_FILE: if ( ferror(in) ) { csv_fatal_error(CSV_ERR_IO_READ, csv->csv_options); - return EOF; + return 0; } if ( csv_string_empty(&csv->csv_string) ) { csv_cleanup(csv); - return EOF; // EOF reached + return 0; // EOF reached } /* diff --git a/csv.h b/csv.h index 44c6ff3..11cc509 100644 --- a/csv.h +++ b/csv.h @@ -31,13 +31,13 @@ typedef struct { extern const csv_options_t csv_default_options; typedef struct { - char *str; - int cap, pos; + char * str; + size_t cap, pos; } csv_string_t; typedef struct { - int *fields; - int cap, pos; + size_t *fields; + size_t cap, pos; } csv_field_t; typedef struct { @@ -60,8 +60,8 @@ void csv_cleanup(csv_t *csv); int csv_read(csv_t *csv, FILE *in); /* field access */ -int csv_nfields(const csv_t *const csv); -const char *csv_field(const csv_t *const csv, int idx); +size_t csv_nfields(const csv_t *const csv); +const char *csv_field(const csv_t *const csv, size_t idx); /* error handling */ const char *csv_err_str(csv_err_t csv_err); -- cgit v1.3