From 062053677f578b273110372d5c8f6778077e4feb Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Wed, 17 Jun 2020 15:30:24 +0200 Subject: aufräumen... MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- csv-test.c | 117 ++++++++++++++----------------------------------------------- 1 file changed, 27 insertions(+), 90 deletions(-) (limited to 'csv-test.c') diff --git a/csv-test.c b/csv-test.c index 55a41bc..e16091c 100644 --- a/csv-test.c +++ b/csv-test.c @@ -149,75 +149,32 @@ static csv_options_t default_csv_options = { }; typedef struct { - FILE *in; csv_options_t *csv_options; csv_string_t csv_string; csv_field_t csv_field; - jmp_buf env; } csv_t; -int csv_init(csv_t *csv, FILE *in); -int csv_init_opt(csv_t *csv, FILE *in, csv_options_t *csv_options); +int csv_init(csv_t *csv); +int csv_init_opt(csv_t *csv, csv_options_t *csv_options); void csv_free(csv_t *csv); int csv_nfields(csv_t *csv); const char * csv_field(csv_t *csv, int idx); -int csv_read(csv_t *csv); +int csv_read(csv_t *csv, FILE *in); void csv_write(csv_t *csv, int n, const char *fields[], FILE *out); -int csv_try(csv_t *csv, FILE *in); -void csv_throw(csv_t *csv, int x); - -#define CSV_NO_MEMORY_EXCEPTION (1) -#define CSV_OUT_OF_BOUND_EXCEPTION (2) - - -#if 0 -#define TRY do { jmp_buf ex_buf__; switch( setjmp(ex_buf__) ) { case 0: while(1) { -#define CATCH(x) break; case x: -#define FINALLY break; } default: { -#define ETRY break; } } }while(0) -#define THROW(x) longjmp(ex_buf__, x) -#endif - -#define CSV_TRY(csv, in) do{ switch( csv_try(csv, in) ){ case 0: while(1) { -#define CSV_CATCH(x) break; case x: -#define CSV_FINALLY break; } default: { -#define CSV_ETRY break; } } } while(0) -#define CSV_THROW(csv, x) csv_throw(csv, x) - -int -csv_try(csv_t *csv, FILE *in) -{ - int err = csv_init(csv, in); - if ( err ) { - return err; - } - err = setjmp(csv->env); - return err; -} - -void -csv_throw(csv_t *csv, int x) -{ - longjmp(csv->env, x); - printf("d\n"); -} - int -csv_init(csv_t *csv, FILE *in) +csv_init(csv_t *csv) { assert(csv != NULL); - return csv_init_opt(csv, in, &default_csv_options); + return csv_init_opt(csv, &default_csv_options); } int -csv_init_opt(csv_t *csv, FILE *in, csv_options_t *csv_options) +csv_init_opt(csv_t *csv, csv_options_t *csv_options) { assert(csv != NULL); - assert(in != NULL); - csv->in = in; csv->csv_options = ( csv_options != NULL ) ? csv_options : &default_csv_options; if ( !csv_string_init(&csv->csv_string) ) { @@ -255,20 +212,19 @@ const char * csv_field(csv_t *csv, int idx) { assert(csv != NULL); - //assert(idx >= 0 && idx < csv->csv_field.pos); + assert(idx >= 0 && idx < csv->csv_field.pos); - if ( idx < 0 || idx >= csv->csv_field.pos) { - printf("idx: %d\n", idx); - CSV_THROW(csv, CSV_OUT_OF_BOUND_EXCEPTION); - return NULL; + if ( idx >= 0 && idx < csv->csv_field.pos) { + return &csv->csv_string.str[csv->csv_field.fields[idx]]; } else { - return &csv->csv_string.str[csv->csv_field.fields[idx]]; + /* TODO: error handling */ + return NULL; } } int -csv_read(csv_t *csv) +csv_read(csv_t *csv, FILE *in) { assert(csv != NULL); @@ -294,14 +250,14 @@ csv_read(csv_t *csv) case STATE_START_FIELD: csv_field_append(&csv->csv_field, csv->csv_string.pos); - ch = getc(csv->in); + ch = getc(in); if ( ch == EOF ) { state = STATE_END_FILE; } else if ( ch == '\r' ) { /* Teste auf CR.. */ - ch = getc(csv->in); + ch = getc(in); if ( ch != '\n' ) { /* ... LF */ - ungetc(ch, csv->in); + ungetc(ch, in); } state = STATE_END_LINE; } @@ -322,12 +278,12 @@ csv_read(csv_t *csv) case STATE_QUOTED_FIELD: do { - ch = getc(csv->in); + ch = getc(in); if ( ch == EOF ) { state = STATE_END_FILE; } else if ( ch == QUOTE ) { - ch = getc(csv->in); + ch = getc(in); if ( ch == EOF ) { state = STATE_END_FILE; } @@ -338,9 +294,9 @@ csv_read(csv_t *csv) state = STATE_END_FIELD; } else if ( ch == '\r' ) { - ch = getc(csv->in); + ch = getc(in); if ( ch != '\n' ) { - ungetc(ch, csv->in); + ungetc(ch, in); } state = STATE_END_LINE; } @@ -349,7 +305,7 @@ csv_read(csv_t *csv) } else { csv_string_append(&csv->csv_string, QUOTE); - ungetc(ch, csv->in); /* zuviel gelesenes Zeichen zurückstellen */ + ungetc(ch, in); /* zuviel gelesenes Zeichen zurückstellen */ } } else { @@ -360,7 +316,7 @@ csv_read(csv_t *csv) case STATE_SIMPLE_FIELD: do { - ch = getc(csv->in); + ch = getc(in); if ( ch == EOF ) { state = STATE_END_FILE; } @@ -368,9 +324,9 @@ csv_read(csv_t *csv) state = STATE_END_FIELD; } else if ( ch == '\r' ) { - ch = getc(csv->in); + ch = getc(in); if ( ch != '\n' ) { - ungetc(ch, csv->in); + ungetc(ch, in); } state = STATE_END_LINE; } @@ -464,33 +420,14 @@ main(void) clock_t start = clock(); - CSV_TRY(csv, stdin) - { - while ( (n = csv_read(csv)) != -1 ) { - csv_field(csv, -1); + int err = csv_init(csv); + if ( !err ) { + while ( (n = csv_read(csv, stdin)) != -1 ) { + //csv_field(csv, -1); ++line; } - } - CSV_CATCH( CSV_NO_MEMORY_EXCEPTION ) - { - fprintf(stderr, "out of memory!"); - csv_free(csv); - return EXIT_FAILURE; - } - CSV_CATCH( CSV_OUT_OF_BOUND_EXCEPTION ) - { - fprintf(stderr, "out of bound!!\n"); csv_free(csv); - return EXIT_FAILURE; } - CSV_FINALLY - { - printf("finally...\n"); - csv_free(csv); - } - CSV_ETRY; - - // http://groups.di.unipi.it/~nids/docs/longjump_try_trow_catch.html clock_t end = clock(); -- cgit v1.3