output format and exceptions

This commit is contained in:
Neil 2022-12-29 10:10:41 -08:00
parent 91564c5d03
commit 8b0b851755
3 changed files with 32 additions and 23 deletions

View File

@ -222,7 +222,7 @@ recatch:
day_tree_(&j.days); day_tree_(&j.days);
text_(&j.backing); text_(&j.backing);
finally: 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); int_array_(&years), int_array_(&months), int_array_(&days);
return j; return j;
} }

View File

@ -166,10 +166,11 @@ struct kjv kjv(void) {
struct dirent *de = 0; struct dirent *de = 0;
struct { size_t offset; int is; } build[KJV_BOOK_SIZE] = { 0 }; struct { size_t offset; int is; } build[KJV_BOOK_SIZE] = { 0 };
enum kjv_book b = 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. */ /* 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))) { while((de = readdir(dir))) {
unsigned ordinal; unsigned ordinal;
char *unstable_backing; char *unstable_backing;
@ -184,7 +185,7 @@ struct kjv kjv(void) {
build[b].is = 1; build[b].is = 1;
build[b].offset = (size_t)(unstable_backing - backing.a.data); 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. */ /* Now backing is stable; count all the words for each verse. */
for(b = 0; b < KJV_BOOK_SIZE; b++) { for(b = 0; b < KJV_BOOK_SIZE; b++) {
@ -209,10 +210,13 @@ struct kjv kjv(void) {
} }
goto finally; goto finally;
catch: catch:
if(de) fprintf(stderr, "While reading %s.\n", de->d_name); if(de) fprintf(stderr, "While reading %s/%s.\n", dir_kjv, de->d_name);
if(dir && !attempted_closedir && closedir(dir) == -1) perror(dir_kjv); else fprintf(stderr, "In directory %s/.\n", dir_kjv);
recatch:
kjv_(&kjv); kjv_(&kjv);
finally: 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); text_(&backing);
return kjv; return kjv;
} }

View File

@ -20,6 +20,7 @@ static int scan(union date32 date, const char *const buffer,
uint32_t chapter, verse; uint32_t chapter, verse;
enum YYCONDTYPE condition = yycline; enum YYCONDTYPE condition = yycline;
size_t line = 1; size_t line = 1;
char datestr[12] = {0};
assert(buffer && kj); assert(buffer && kj);
YYCURSOR = YYMARKER = yyt1 = buffer; YYCURSOR = YYMARKER = yyt1 = buffer;
/*!re2c /**/ /*!re2c /**/
@ -38,8 +39,8 @@ static int scan(union date32 date, const char *const buffer,
("-" (natural ":")? natural [ab]?)? ws* "--"; ("-" (natural ":")? natural [ab]?)? ws* "--";
*/ */
for( ; ; ) { /*!re2c /**/ for( ; ; ) { /*!re2c /**/
<skip, book> * { printf("*! %zu\n", line);goto error; } <skip, book> * { goto catch; }
<line> "\x00" { printf("%zu lines\n", line); return 1; } <line> "\x00" { return 1; }
<line> "\n" { line++; continue; } <line> "\n" { line++; continue; }
<line> * :=> skip <line> * :=> skip
<line> "Genesis" / lookat => book { book = Genesis; continue; } <line> "Genesis" / lookat => book { book = Genesis; continue; }
@ -61,19 +62,23 @@ static int scan(union date32 date, const char *const buffer,
//<line> [^\n\x00]* newline { printf("throw\n"); line++; continue; } //<line> [^\n\x00]* newline { printf("throw\n"); line++; continue; }
<book> ws+ @s0 natural @s1 ":" @t0 natural @t1 [ab]? => skip { <book> ws+ @s0 natural @s1 ":" @t0 natural @t1 [ab]? => skip {
if(!helper_natural(s0, s1, &chapter) if(!helper_natural(s0, s1, &chapter)
|| !helper_natural(t0, t1, &verse)) goto error; || !helper_natural(t0, t1, &verse)) goto catch;
union kjvcite c union kjvcite cite
= { .book = book, .chapter = chapter, .verse = verse }; = { .book = book, .chapter = chapter, .verse = verse };
char a[12]; const size_t old_set_words = kj->set_words;
kjvcite_to_string(c, &a), printf("Parsed %s\n", a); char citestr[12];
if(!kjv_add(kj, c)) goto error; 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; continue;
} }
<skip> [^\n\x00]* "\n" => line { line++; continue; } <skip> [^\n\x00]* "\n" => line { line++; continue; }
//=> bible { x->s0 = s0, x->s1 = s1; return x->symbol = KJV_BOOK, 1; } //=> bible { x->s0 = s0, x->s1 = s1; return x->symbol = KJV_BOOK, 1; }
*/ } */ }
error: catch:
errno = EILSEQ; if(!errno) errno = EILSEQ;
{ {
char a[12]; char a[12];
date32_to_string(date, &a); date32_to_string(date, &a);
@ -84,21 +89,21 @@ error:
int main(void) { int main(void) {
int success = EXIT_SUCCESS; int success = EXIT_SUCCESS;
struct journal j = journal(); struct journal j;
struct journal_iterator it; struct journal_iterator it;
struct kjv kj = kjv(); struct kjv kj = kjv();
union date32 k; union date32 k;
union load *v; union load *v;
size_t i; size_t i;
scan((union date32){.year=2000, .month=1, .day=1}, /*scan((union date32){.year=2000, .month=1, .day=1}, "\n\n\n"
"\n\n\n" "Romans 3:23 -- ``For all have sinned, "
"Romans 3:23 -- ``For all have sinned, and come short of the glory of God.''\n", &kj); "and come short of the glory of God.''\n", &kj);*/
j = journal();
if(!journal_is_valid(&j)) goto catch; 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)) { it = journal_begin(&j), i = 0; while(journal_next(&it, &k, &v)) {
char a[12]; if(!scan(k, v->text, &kj)) goto catch;
date32_to_string(k, &a), printf("%s: %.6s\n", a, v->text);
if(!scan(k, v->text, &kj/*, fp*/)) goto catch;
if(++i > 32) break; if(++i > 32) break;
} }
goto finally; goto finally;