From 7cf4aa222ab5cbda4bf8a6ffc968a85304cc8333 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Thu, 2 Jul 2020 09:12:20 +0200 Subject: csv-read() sollte bei Dateiende EOF liefern, statt -1. Damit wird der Clientcode lesbarer und verständlicher. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- csv-perf.c | 8 ++++---- csv-test.c | 14 +++++++------- csv.c | 8 ++++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/csv-perf.c b/csv-perf.c index efd2155..ddb4808 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) != -1 ) { + while ( csv_read(&csv, f) != EOF ) { ++line; } @@ -27,7 +27,7 @@ test_exception(FILE *f) csv_t csv[1]; csv_init(csv); - for ( int n; (n = csv_read(csv, f)) != -1; ) { + for ( int n; (n = csv_read(csv, f)) != EOF; ) { if (++line == 500000) csv_field(csv, -1); } @@ -52,7 +52,7 @@ test_exception_with_signal(FILE *f) csv_t csv[1]; csv_init(csv); - for ( int n; (n = csv_read(csv, f)) != -1; ) { + for ( int n; (n = csv_read(csv, f)) != EOF; ) { if (++line == 500000) csv_field(csv, -1); } @@ -88,7 +88,7 @@ test_exception_longjmp(FILE *f) if ( setjmp(env) == 0 ) { int line = 0; - for ( int n; (n = csv_read(csv, f)) != -1; ) { + for ( int n; (n = csv_read(csv, f)) != EOF; ) { // process data... if (++line == 500000) csv_field(csv, -1); } diff --git a/csv-test.c b/csv-test.c index c5152ea..552bbb8 100644 --- a/csv-test.c +++ b/csv-test.c @@ -39,7 +39,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) == -1 ); + assert( csv_read(&csv, f) == EOF ); FMEMCLOSE(f); } @@ -67,7 +67,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) == -1 ); + assert( csv_read(&csv, f) == EOF ); FMEMCLOSE(f); } @@ -216,7 +216,7 @@ test_empty_fields(void) assert( strcmp(csv_field(&csv, 2), "") == 0 ); // EOF - assert( csv_read(&csv, f) == -1 ); + assert( csv_read(&csv, f) == EOF ); FMEMCLOSE(f); } @@ -365,7 +365,7 @@ test_simple_fields(void) assert( strcmp(csv_field(&csv, 2), "C") == 0 ); // EOF - assert( csv_read(&csv, f) == -1 ); + assert( csv_read(&csv, f) == EOF ); FMEMCLOSE(f); } @@ -388,7 +388,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) == -1 ); + assert( csv_read(&csv, f) == EOF ); FMEMCLOSE(f); } @@ -407,7 +407,7 @@ test_wrong_quoted_field(void) assert( csv_nfields(&csv) == 1 ), assert( strcmp(csv_field(&csv, 0), "foo") == 0 ); - assert( csv_read(&csv, f) == -1 ); + assert( csv_read(&csv, f) == EOF ); FMEMCLOSE(f); } @@ -471,7 +471,7 @@ test_read_error(void) switch ( setjmp(env) ) { case CSV_ERR_OK: - assert( csv_read(&csv, f) == -1 ); + assert( csv_read(&csv, f) == EOF ); break; case CSV_ERR_IO_READ: diff --git a/csv.c b/csv.c index 430630b..71c0e0f 100644 --- a/csv.c +++ b/csv.c @@ -291,13 +291,13 @@ csv_read(csv_t *csv, FILE *in) if ( ferror(in) ) { csv_fatal_error(CSV_ERR_IO_READ, csv->csv_options); - return -1; + return EOF; } // do not try to read if EOF has already been seen if ( feof(in) ) { csv_cleanup(csv); - return -1; + return EOF; } enum { @@ -429,12 +429,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 -1; + return EOF; } if ( csv_string_empty(&csv->csv_string) ) { csv_cleanup(csv); - return -1; // EOF reached + return EOF; // EOF reached } /* -- cgit v1.3