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 */
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;
}

View File

@ -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 <KJV/>. */
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;
}