From fb6562779b76997d5207c2f2115d8965da926164 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Wed, 17 Jun 2020 16:30:03 +0200 Subject: mit abort() lässt sich eine bessere Fehlerbehandlung durchführen, wenn das zugehörige Signal in einem Handler abgefangen wird! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- csv-test.c | 59 ++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 19 deletions(-) (limited to 'csv-test.c') diff --git a/csv-test.c b/csv-test.c index 220337e..85e0e91 100644 --- a/csv-test.c +++ b/csv-test.c @@ -5,6 +5,7 @@ #include #include #include +#include #define INLINE static inline @@ -41,7 +42,14 @@ csv_string_empty(csv_string_t *csv_string) return ( csv_string->pos == 0 ) ? true : false; } -INLINE bool +INLINE void +csv_fatal_error(const char *msg) +{ + fprintf(stderr, "fatal error: %s\n", msg); + abort(); +} + +INLINE void csv_string_append(csv_string_t *csv_string, char ch) { assert(csv_string != NULL); @@ -51,7 +59,8 @@ csv_string_append(csv_string_t *csv_string, char ch) if ( csv_string->pos == csv_string->cap ) { /* grow if needed */ if ( csv_string->str == NULL ) { /* first call? */ if ( (csv_string->str = malloc(INITIAL_CAP)) == NULL ) { - return false; + csv_fatal_error("out of memory..."); + return; } csv_string->cap = INITIAL_CAP; @@ -60,7 +69,8 @@ csv_string_append(csv_string_t *csv_string, char ch) int cap = (csv_string->cap * 3) / 2; /* *= 1.5 */ char *str = realloc(csv_string->str, cap); if ( str == NULL ) { - return false; + csv_fatal_error("out of memory..."); + return; } csv_string->str = str; csv_string->cap = cap; @@ -68,8 +78,6 @@ csv_string_append(csv_string_t *csv_string, char ch) } csv_string->str[csv_string->pos++] = ch; /* append char */ - - return true; } INLINE void @@ -108,7 +116,7 @@ csv_field_reset(csv_field_t *csv_field) csv_field->pos = 0; } -INLINE bool +INLINE void csv_field_append(csv_field_t *csv_field, int idx) { assert(csv_field != NULL); @@ -118,7 +126,8 @@ csv_field_append(csv_field_t *csv_field, int idx) if ( csv_field->pos == csv_field->cap ) { /* grow if needed */ if ( csv_field->fields == NULL ) { if ( (csv_field->fields = calloc(INITIAL_CAP, sizeof(csv_field->fields[0]))) == NULL ) { - return false; + csv_fatal_error("out of memory"); + return; } csv_field->cap = INITIAL_CAP; @@ -127,7 +136,8 @@ csv_field_append(csv_field_t *csv_field, int idx) int cap = (csv_field->cap * 3) / 2; /* *= 1.5 */ int *fields = reallocarray(csv_field->fields, cap, sizeof(csv_field->fields[0])); if ( fields == NULL ) { - return false; + csv_fatal_error("out of memory"); + return; } csv_field->fields = fields; csv_field->cap = cap; @@ -135,8 +145,6 @@ csv_field_append(csv_field_t *csv_field, int idx) } csv_field->fields[csv_field->pos++] = idx; /* append idx */ - - return true; } INLINE void @@ -220,13 +228,11 @@ csv_field(csv_t *csv, int idx) assert(csv != NULL); assert(idx >= 0 && idx < csv->csv_field.pos); - if ( idx >= 0 && idx < csv->csv_field.pos) { - return &csv->csv_string.str[csv->csv_field.fields[idx]]; - } - else { - /* TODO: error handling */ - return NULL; + if ( idx < 0 || idx >= csv->csv_field.pos ) { + csv_fatal_error("range error"); } + + return &csv->csv_string.str[csv->csv_field.fields[idx]]; } int @@ -418,12 +424,22 @@ csv_write(csv_t *csv, int n, const char *fields[], FILE *out) fprintf(out, "\r\n"); } +jmp_buf env; + +static void sig_handler(int sig) +{ + (void) sig; + longjmp(env, 1); +} + int main(void) { csv_t csv[1]; int n, line = 0; + signal(SIGABRT, sig_handler); + FILE *in = fopen("500000 Records.csv", "r"); if ( in == NULL ) { fprintf(stderr, "failed to open testfile...\n"); @@ -433,9 +449,14 @@ main(void) clock_t start = clock(); csv_init(csv); - while ( (n = csv_read(csv, in)) != -1 ) { - //csv_field(csv, -1); - ++line; + if ( setjmp(env) == 0 ) { + while ( (n = csv_read(csv, in)) != -1 ) { + csv_field(csv, -1); + ++line; + } + } + else { + printf("Fehler beim Verarbeiten der Datei...\n"); } csv_free(csv); -- cgit v1.3