More robust journal.

This commit is contained in:
Neil 2022-12-28 09:34:19 -08:00
parent bed73757ed
commit af7678e802
2 changed files with 30 additions and 19 deletions

View File

@ -14,10 +14,8 @@
union load { const char *text; size_t offset; };
static void date32_to_string(const union date32 d, char (*const a)[12]) {
assert(a
&& d.year < 10000 && d.month && d.month <= 31 && d.day && d.day <= 31);
sprintf(*a, "%" PRIu32 "-%2.2" PRIu32 "-%2.2" PRIu32,
(unsigned)d.year % 10000, (unsigned)d.month, (unsigned)d.day);
d.year % 10000, d.month % 100, d.day % 100);
}
static int day_compare(const union date32 a, const union date32 b)
{ return a.u32 > b.u32; }
@ -141,12 +139,15 @@ static unsigned weekday(union date32 d) {
#define OMIT_DAY
#include "../src/journal.h" /* Just prototypes. */
/** Dynamic memory allocation for `j` will be zero, <fn:journal_is_valid> will
be false. */
void journal_(struct journal *const j) {
if(!j) return;
day_tree_(&j->days);
text_(&j->backing);
}
/** @return A completed journal out of "journal". */
struct journal journal(void) {
const char *const dir_journal = "journal";
struct journal j = {0};
@ -191,7 +192,7 @@ struct journal journal(void) {
}
if(closedir(dir)) { dir = 0; goto catch; } dir = 0;
qsort(months.data, months.size, sizeof *months.data, &void_int_cmp);
fprintf(stderr, "Months in <<%s>>: %s.)\n",
fprintf(stderr, "Months in <<%s>>: %s.\n",
fn, int_array_to_string(&months));
/* Go though each month. */
@ -219,43 +220,45 @@ struct journal journal(void) {
.month = (uint32_t)*m, .day = (uint32_t)*d };
char *contents = 0;
sprintf(fn, "%.2d.txt", *d); /* Reconstruct. */
if(!(contents = text_append_file(&j.backing, fn))) goto syntax;
if(!(contents = text_append_file(&j.backing, fn))) goto catch;
switch(day_tree_bulk_add(&j.days, d32, &load)) {
case TREE_PRESENT: intent = "not unique", errno = EDOM; /*Sic*/
case TREE_ERROR: goto syntax;
case TREE_PRESENT: intent = "not unique", errno = EDOM; /*sic*/
case TREE_ERROR: goto catch;
case TREE_ABSENT: break; /* Expected. */
}
/* The pointers are not stable while we are loading it. */
load->offset = (size_t)(contents - j.backing.a.data);
continue;
syntax:
goto catch;
}
d = 0, int_array_clear(&days);
if(chdir("..") == -1) goto catch;
}
m = 0, int_array_clear(&months);
if(chdir("..") == -1) goto catch;
/* fixme: Expand, contact is the next thing that it doesn't get. */
if(*y == 1993/*1996*/) break;
/*if(*y == 1993) break;*/
}
day_tree_bulk_finish(&j.days);
fprintf(stderr, "Journal has entries: %s\n", day_tree_to_string(&j.days));
if(chdir("..") == -1) goto catch;
/*fprintf(stderr, "Journal has entries: %s\n",
day_tree_to_string(&j.days));*/
goto finally;
catch:
fprintf(stderr, "On date: %d-%.2d-%.2d.\n",
y ? *y : -1, m ? *m : -1, d ? *d : -1);
fprintf(stderr, "On date: %s/%d-%.2d-%.2d.\n",
dir_journal, y ? *y : 0, m ? *m : 0, d ? *d : 0 );
if(intent) fprintf(stderr, "Explanation: %s.\n", intent);
recatch:
day_tree_(&j.days);
text_(&j.backing);
finally:
if(dir) closedir(dir); dir = 0;
if(dir) if(closedir(dir)) { dir = 0; goto recatch; } dir = 0;
int_array_(&years), int_array_(&months), int_array_(&days);
return j;
}
/** @return `j` read in some data? */
int journal_is_valid(const struct journal *const j) {
return 0;
return j && j->days.root.node && j->backing.a.data;
}
/** @return `j` as a string. */
const char *journal_to_string(const struct journal *const j)
{ return day_tree_to_string(&j->days); }

View File

@ -6,8 +6,16 @@
int main(void) {
struct journal j = journal();
//verify...
return 0;
int success = EXIT_SUCCESS;
if(!journal_is_valid(&j)) goto catch;
printf("Journal: %s.\n", journal_to_string(&j));
goto finally;
catch:
success = EXIT_FAILURE;
perror("journal");
finally:
journal_(&j);
return success;
}
#if 0