From 8b0b851755427bef0278a421b79a159835c6182a Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 29 Dec 2022 10:10:41 -0800 Subject: [PATCH] output format and exceptions --- src/journal.re.c | 2 +- src/kjv.re.c | 14 +++++++++----- src/scan_kjv.re.c | 39 ++++++++++++++++++++++----------------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/journal.re.c b/src/journal.re.c index 8f77deb..fa70c0d 100644 --- a/src/journal.re.c +++ b/src/journal.re.c @@ -222,7 +222,7 @@ recatch: day_tree_(&j.days); text_(&j.backing); finally: - if(dir) if(closedir(dir)) { dir = 0; goto recatch; } dir = 0; + if(dir) { if(closedir(dir)) { dir = 0; goto recatch; } dir = 0; } int_array_(&years), int_array_(&months), int_array_(&days); return j; } diff --git a/src/kjv.re.c b/src/kjv.re.c index 0b38c4e..83d1832 100644 --- a/src/kjv.re.c +++ b/src/kjv.re.c @@ -166,10 +166,11 @@ struct kjv kjv(void) { struct dirent *de = 0; struct { size_t offset; int is; } build[KJV_BOOK_SIZE] = { 0 }; enum kjv_book b = 0; - int attempted_closedir = 0; + int is_in_kjv = 0; /* For all files in directory KJV with <#>*.txt, read into backing. */ - if(chdir(dir_kjv) == -1 || !(dir = opendir("."))) goto catch; + if(chdir(dir_kjv) == -1 || (is_in_kjv = 1, !(dir = opendir(".")))) + goto catch; while((de = readdir(dir))) { unsigned ordinal; char *unstable_backing; @@ -184,7 +185,7 @@ struct kjv kjv(void) { build[b].is = 1; build[b].offset = (size_t)(unstable_backing - backing.a.data); } - if(attempted_closedir = 1, closedir(dir) == -1) goto catch; dir = 0; + if(closedir(dir) == -1) { dir = 0; goto catch; } dir = 0; /* Now backing is stable; count all the words for each verse. */ for(b = 0; b < KJV_BOOK_SIZE; b++) { @@ -209,10 +210,13 @@ struct kjv kjv(void) { } goto finally; catch: - if(de) fprintf(stderr, "While reading %s.\n", de->d_name); - if(dir && !attempted_closedir && closedir(dir) == -1) perror(dir_kjv); + if(de) fprintf(stderr, "While reading %s/%s.\n", dir_kjv, de->d_name); + else fprintf(stderr, "In directory %s/.\n", dir_kjv); +recatch: kjv_(&kjv); finally: + if(dir) { if(closedir(dir)) { dir = 0; goto recatch; } dir = 0; } + if(is_in_kjv && (is_in_kjv = 0, chdir("..") == -1)) goto recatch; text_(&backing); return kjv; } diff --git a/src/scan_kjv.re.c b/src/scan_kjv.re.c index 2e09927..f02867d 100644 --- a/src/scan_kjv.re.c +++ b/src/scan_kjv.re.c @@ -20,6 +20,7 @@ static int scan(union date32 date, const char *const buffer, uint32_t chapter, verse; enum YYCONDTYPE condition = yycline; size_t line = 1; + char datestr[12] = {0}; assert(buffer && kj); YYCURSOR = YYMARKER = yyt1 = buffer; /*!re2c /**/ @@ -38,8 +39,8 @@ static int scan(union date32 date, const char *const buffer, ("-" (natural ":")? natural [ab]?)? ws* "--"; */ for( ; ; ) { /*!re2c /**/ - * { printf("*! %zu\n", line);goto error; } - "\x00" { printf("%zu lines\n", line); return 1; } + * { goto catch; } + "\x00" { return 1; } "\n" { line++; continue; } * :=> skip "Genesis" / lookat => book { book = Genesis; continue; } @@ -61,19 +62,23 @@ static int scan(union date32 date, const char *const buffer, // [^\n\x00]* newline { printf("throw\n"); line++; continue; } ws+ @s0 natural @s1 ":" @t0 natural @t1 [ab]? => skip { if(!helper_natural(s0, s1, &chapter) - || !helper_natural(t0, t1, &verse)) goto error; - union kjvcite c + || !helper_natural(t0, t1, &verse)) goto catch; + union kjvcite cite = { .book = book, .chapter = chapter, .verse = verse }; - char a[12]; - kjvcite_to_string(c, &a), printf("Parsed %s\n", a); - if(!kjv_add(kj, c)) goto error; + const size_t old_set_words = kj->set_words; + char citestr[12]; + if(!kjv_add(kj, cite)) goto catch; + if(!*datestr) date32_to_string(date, &datestr); + kjvcite_to_string(cite, &citestr); + printf("%s\t%zu\t%zu\t# %s\n", + datestr, old_set_words, kj->set_words, citestr); continue; } [^\n\x00]* "\n" => line { line++; continue; } //=> bible { x->s0 = s0, x->s1 = s1; return x->symbol = KJV_BOOK, 1; } */ } -error: - errno = EILSEQ; +catch: + if(!errno) errno = EILSEQ; { char a[12]; date32_to_string(date, &a); @@ -84,21 +89,21 @@ error: int main(void) { int success = EXIT_SUCCESS; - struct journal j = journal(); + struct journal j; struct journal_iterator it; struct kjv kj = kjv(); union date32 k; union load *v; size_t i; - scan((union date32){.year=2000, .month=1, .day=1}, - "\n\n\n" - "Romans 3:23 -- ``For all have sinned, and come short of the glory of God.''\n", &kj); + /*scan((union date32){.year=2000, .month=1, .day=1}, "\n\n\n" + "Romans 3:23 -- ``For all have sinned, " + "and come short of the glory of God.''\n", &kj);*/ + j = journal(); if(!journal_is_valid(&j)) goto catch; - printf("Journal: %s.\n", journal_to_string(&j)); + fprintf(stderr, "Journal: %s.\n", journal_to_string(&j)); + printf("# date\told\tnew / %zu\n", kj.total_words); it = journal_begin(&j), i = 0; while(journal_next(&it, &k, &v)) { - char a[12]; - date32_to_string(k, &a), printf("%s: %.6s\n", a, v->text); - if(!scan(k, v->text, &kj/*, fp*/)) goto catch; + if(!scan(k, v->text, &kj)) goto catch; if(++i > 32) break; } goto finally;