More robust journal.
This commit is contained in:
parent
bed73757ed
commit
af7678e802
@ -14,10 +14,8 @@
|
|||||||
|
|
||||||
union load { const char *text; size_t offset; };
|
union load { const char *text; size_t offset; };
|
||||||
static void date32_to_string(const union date32 d, char (*const a)[12]) {
|
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,
|
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)
|
static int day_compare(const union date32 a, const union date32 b)
|
||||||
{ return a.u32 > b.u32; }
|
{ return a.u32 > b.u32; }
|
||||||
@ -141,12 +139,15 @@ static unsigned weekday(union date32 d) {
|
|||||||
#define OMIT_DAY
|
#define OMIT_DAY
|
||||||
#include "../src/journal.h" /* Just prototypes. */
|
#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) {
|
void journal_(struct journal *const j) {
|
||||||
if(!j) return;
|
if(!j) return;
|
||||||
day_tree_(&j->days);
|
day_tree_(&j->days);
|
||||||
text_(&j->backing);
|
text_(&j->backing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return A completed journal out of "journal". */
|
||||||
struct journal journal(void) {
|
struct journal journal(void) {
|
||||||
const char *const dir_journal = "journal";
|
const char *const dir_journal = "journal";
|
||||||
struct journal j = {0};
|
struct journal j = {0};
|
||||||
@ -191,7 +192,7 @@ struct journal journal(void) {
|
|||||||
}
|
}
|
||||||
if(closedir(dir)) { dir = 0; goto catch; } dir = 0;
|
if(closedir(dir)) { dir = 0; goto catch; } dir = 0;
|
||||||
qsort(months.data, months.size, sizeof *months.data, &void_int_cmp);
|
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));
|
fn, int_array_to_string(&months));
|
||||||
|
|
||||||
/* Go though each month. */
|
/* Go though each month. */
|
||||||
@ -219,43 +220,45 @@ struct journal journal(void) {
|
|||||||
.month = (uint32_t)*m, .day = (uint32_t)*d };
|
.month = (uint32_t)*m, .day = (uint32_t)*d };
|
||||||
char *contents = 0;
|
char *contents = 0;
|
||||||
sprintf(fn, "%.2d.txt", *d); /* Reconstruct. */
|
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)) {
|
switch(day_tree_bulk_add(&j.days, d32, &load)) {
|
||||||
case TREE_PRESENT: intent = "not unique", errno = EDOM; /*Sic*/
|
case TREE_PRESENT: intent = "not unique", errno = EDOM; /*sic*/
|
||||||
case TREE_ERROR: goto syntax;
|
case TREE_ERROR: goto catch;
|
||||||
case TREE_ABSENT: break; /* Expected. */
|
case TREE_ABSENT: break; /* Expected. */
|
||||||
}
|
}
|
||||||
/* The pointers are not stable while we are loading it. */
|
/* The pointers are not stable while we are loading it. */
|
||||||
load->offset = (size_t)(contents - j.backing.a.data);
|
load->offset = (size_t)(contents - j.backing.a.data);
|
||||||
continue;
|
|
||||||
syntax:
|
|
||||||
goto catch;
|
|
||||||
}
|
}
|
||||||
d = 0, int_array_clear(&days);
|
d = 0, int_array_clear(&days);
|
||||||
if(chdir("..") == -1) goto catch;
|
if(chdir("..") == -1) goto catch;
|
||||||
}
|
}
|
||||||
m = 0, int_array_clear(&months);
|
m = 0, int_array_clear(&months);
|
||||||
if(chdir("..") == -1) goto catch;
|
if(chdir("..") == -1) goto catch;
|
||||||
/* fixme: Expand, contact is the next thing that it doesn't get. */
|
/*if(*y == 1993) break;*/
|
||||||
if(*y == 1993/*1996*/) break;
|
|
||||||
}
|
}
|
||||||
day_tree_bulk_finish(&j.days);
|
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;
|
goto finally;
|
||||||
catch:
|
catch:
|
||||||
fprintf(stderr, "On date: %d-%.2d-%.2d.\n",
|
fprintf(stderr, "On date: %s/%d-%.2d-%.2d.\n",
|
||||||
y ? *y : -1, m ? *m : -1, d ? *d : -1);
|
dir_journal, y ? *y : 0, m ? *m : 0, d ? *d : 0 );
|
||||||
if(intent) fprintf(stderr, "Explanation: %s.\n", intent);
|
if(intent) fprintf(stderr, "Explanation: %s.\n", intent);
|
||||||
|
recatch:
|
||||||
day_tree_(&j.days);
|
day_tree_(&j.days);
|
||||||
|
text_(&j.backing);
|
||||||
finally:
|
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);
|
int_array_(&years), int_array_(&months), int_array_(&days);
|
||||||
return j;
|
return j;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return `j` read in some data? */
|
||||||
int journal_is_valid(const struct journal *const j) {
|
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)
|
const char *journal_to_string(const struct journal *const j)
|
||||||
{ return day_tree_to_string(&j->days); }
|
{ return day_tree_to_string(&j->days); }
|
||||||
|
@ -6,8 +6,16 @@
|
|||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
struct journal j = journal();
|
struct journal j = journal();
|
||||||
//verify...
|
int success = EXIT_SUCCESS;
|
||||||
return 0;
|
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
|
#if 0
|
||||||
|
Loading…
Reference in New Issue
Block a user