From eb8d42ba3e92ac99b84bfcc3eabdc4c1d2948ce4 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 12 Dec 2022 00:45:41 -0800 Subject: [PATCH] enum kjv --- kjv/src/kjv.h | 73 +++++++++++++++++++++++++++++++++++++++++++++- kjv/src/kjv.re_c.c | 10 +++---- kjv/src/main.c | 35 +++++++++++----------- 3 files changed, 95 insertions(+), 23 deletions(-) diff --git a/kjv/src/kjv.h b/kjv/src/kjv.h index 104531d..5d6f6b1 100644 --- a/kjv/src/kjv.h +++ b/kjv/src/kjv.h @@ -1 +1,72 @@ -int kjv_filename(const char *, unsigned *); +enum kjv_book { +Genesis, +Exodus, +Leviticus, +Numbers, +Deuteronomy, +Joshua, +Judges, +Ruth, +ISamuel, +IISamuel, +IKings, +IIKings, +IChronicles, +IIChronicles, +Ezra, +Nehemiah, +Esther, +Job, +Psalms, +Proverbs, +Ecclesiastes, +Song_of_Solomon, +Isaiah, +Jeremiah, +Lamentations, +Ezekiel, +Daniel, +Hosea, +Joel, +Amos, +Obadiah, +Jonah, +Micah, +Nahum, +Habakkuk, +Zephaniah, +Haggai, +Zechariah, +Malachi, + +Matthew, +Mark, +Luke, +John, +Acts, +Romans, +ICorinthians, +IICorinthians, +Galatians, +Ephesians, +Philippians, +Colossians, +IThessalonians, +IIThessalonians, +ITimothy, +IITimothy, +Titus, +Philemon, +Hebrews, +James, +IPeter, +IIPeter, +IJohn, +IIJohn, +IIIJohn, +Jude, +Revelation, + +KJV_BOOK_SIZE }; + +int kjv_filename(const char *, enum kjv_book *); diff --git a/kjv/src/kjv.re_c.c b/kjv/src/kjv.re_c.c index e32eecf..549eea4 100644 --- a/kjv/src/kjv.re_c.c +++ b/kjv/src/kjv.re_c.c @@ -9,14 +9,14 @@ #include /* debug */ static int parse_natural(const char *s, const char *const e, unsigned *const n) { - unsigned a = 0; + unsigned accum = 0; while(s < e) { - unsigned b = a * 10 + (unsigned)(*s - '0'); - if(a >= b) return errno = ERANGE, 0; - a = b; + unsigned next = accum * 10 + (unsigned)(*s - '0'); + if(accum >= next) return errno = ERANGE, 0; + accum = next; s++; } - *n = a; + *n = accum; return 1; } diff --git a/kjv/src/main.c b/kjv/src/main.c index 53c611c..10efdce 100644 --- a/kjv/src/main.c +++ b/kjv/src/main.c @@ -50,38 +50,39 @@ finally: } int main(void) { + const char *const dir_name = "KJV"; int success = EXIT_SUCCESS; DIR *dir = 0; struct dirent *de = 0; - struct char_array book[66] = { 0 }; - const unsigned book_size = sizeof book / sizeof *book; + struct char_array book[KJV_BOOK_SIZE] = { 0 }; unsigned i; errno = 0; - /* Read all files in . */ - if(chdir("KJV") == -1 || !(dir = opendir("."))) goto catch; + /* Read all files in `dir_name`. */ + if(chdir(dir_name) == -1 || !(dir = opendir("."))) goto catch; while((de = readdir(dir))) { - unsigned book_no; - if(!kjv_filename(de->d_name, &book_no)) + unsigned ordinal; + enum kjv_book b; + if(!kjv_filename(de->d_name, &ordinal)) { fprintf(stderr, "Ignored <%s>.\n", de->d_name); continue; } - printf("<%s> book_no: %u\n", de->d_name, book_no); - if(!book_no || book_no > book_size || book[book_no - 1].data) - { errno = ERANGE; goto catch; } - if(!append_file(book + book_no - 1, de->d_name)) goto catch; + printf("<%s> ordinal: %u\n", de->d_name, ordinal); + if(ordinal < 1 || ordinal > KJV_BOOK_SIZE) + { errno = ERANGE; goto catch; } /* Not in range. */ + if(book[b = ordinal - 1].data) + { errno = EDOM; goto catch; } /* Duplicate. */ + if(!append_file(book + b, de->d_name)) goto catch; } closedir(dir), de = 0, dir = 0; - - /* Check to see if all books are accounted for. */ - for(i = 0; i < book_size; i++) - if(!book[i].data) { errno = EDOM; goto catch; } + for(i = 0; i < KJV_BOOK_SIZE; i++) + if(!book[i].data) { errno = EDOM; goto catch; } /* Not there. */ /**/ goto finally; catch: success = EXIT_FAILURE; - perror(de ? de->d_name : "kjv"); - if(dir && closedir(dir)) perror("dir"); + perror(de ? de->d_name : dir_name); + if(dir && closedir(dir)) perror(dir_name); finally: - for(i = 0; i < book_size; i++) char_array_(&book[i]); + for(i = 0; i < KJV_BOOK_SIZE; i++) char_array_(&book[i]); return success; }