This commit is contained in:
Neil 2022-12-12 00:45:41 -08:00
parent 08e5346371
commit eb8d42ba3e
3 changed files with 95 additions and 23 deletions

View File

@ -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 *);

View File

@ -9,14 +9,14 @@
#include <stdio.h> /* debug */ #include <stdio.h> /* debug */
static int parse_natural(const char *s, const char *const e, unsigned *const n) { static int parse_natural(const char *s, const char *const e, unsigned *const n) {
unsigned a = 0; unsigned accum = 0;
while(s < e) { while(s < e) {
unsigned b = a * 10 + (unsigned)(*s - '0'); unsigned next = accum * 10 + (unsigned)(*s - '0');
if(a >= b) return errno = ERANGE, 0; if(accum >= next) return errno = ERANGE, 0;
a = b; accum = next;
s++; s++;
} }
*n = a; *n = accum;
return 1; return 1;
} }

View File

@ -50,38 +50,39 @@ finally:
} }
int main(void) { int main(void) {
const char *const dir_name = "KJV";
int success = EXIT_SUCCESS; int success = EXIT_SUCCESS;
DIR *dir = 0; DIR *dir = 0;
struct dirent *de = 0; struct dirent *de = 0;
struct char_array book[66] = { 0 }; struct char_array book[KJV_BOOK_SIZE] = { 0 };
const unsigned book_size = sizeof book / sizeof *book;
unsigned i; unsigned i;
errno = 0; errno = 0;
/* Read all files in <KJV/>. */ /* Read all files in `dir_name`. */
if(chdir("KJV") == -1 || !(dir = opendir("."))) goto catch; if(chdir(dir_name) == -1 || !(dir = opendir("."))) goto catch;
while((de = readdir(dir))) { while((de = readdir(dir))) {
unsigned book_no; unsigned ordinal;
if(!kjv_filename(de->d_name, &book_no)) enum kjv_book b;
if(!kjv_filename(de->d_name, &ordinal))
{ fprintf(stderr, "Ignored <%s>.\n", de->d_name); continue; } { fprintf(stderr, "Ignored <%s>.\n", de->d_name); continue; }
printf("<%s> book_no: %u\n", de->d_name, book_no); printf("<%s> ordinal: %u\n", de->d_name, ordinal);
if(!book_no || book_no > book_size || book[book_no - 1].data) if(ordinal < 1 || ordinal > KJV_BOOK_SIZE)
{ errno = ERANGE; goto catch; } { errno = ERANGE; goto catch; } /* Not in range. */
if(!append_file(book + book_no - 1, de->d_name)) goto catch; 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; closedir(dir), de = 0, dir = 0;
for(i = 0; i < KJV_BOOK_SIZE; i++)
/* Check to see if all books are accounted for. */ if(!book[i].data) { errno = EDOM; goto catch; } /* Not there. */
for(i = 0; i < book_size; i++)
if(!book[i].data) { errno = EDOM; goto catch; }
/**/ /**/
goto finally; goto finally;
catch: catch:
success = EXIT_FAILURE; success = EXIT_FAILURE;
perror(de ? de->d_name : "kjv"); perror(de ? de->d_name : dir_name);
if(dir && closedir(dir)) perror("dir"); if(dir && closedir(dir)) perror(dir_name);
finally: 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; return success;
} }