diff --git a/Makefile b/Makefile index fa69778..db6c063 100644 --- a/Makefile +++ b/Makefile @@ -36,10 +36,10 @@ default: $(projects) # success bin/test-text: build/text.o build/test_text.o -bin/test-kjv: build/text.o build/kjv.o build/test_kjv.o bin/test-journal: build/text.o build/journal.o build/test_journal.o +bin/test-kjv: build/text.o build/kjv.o build/test_kjv.o bin/kjv: build/text.o build/journal.o build/kjv.o build/scan_kjv.o -bin/flight: build/text.o build/journal.o build/scan_flight.o build/driver_flighthours.o +bin/flight: build/text.o build/journal.o build/flight.o build/driver_flighthours.o bin/%: @echo "\033[1;36mlinking $@\033[0m" diff --git a/src/journal.h b/src/journal.h index 1af65cc..f64c1d0 100644 --- a/src/journal.h +++ b/src/journal.h @@ -1,6 +1,5 @@ #ifndef OMIT_BASE /* */ -#ifndef OMIT_DAY /* */ +#else /* external --> */ #ifndef OMIT_PROTO /* */ diff --git a/src/journal.re.c b/src/journal.re.c index a725779..3ad769b 100644 --- a/src/journal.re.c +++ b/src/journal.re.c @@ -1,4 +1,7 @@ -#define OMIT_DAY +/** Reading all journal entries in yyyy/mm/dd.txt. + @std GNU, C11, assumes reverse order unions. */ + +#define OMIT_STRUCT #define OMIT_PROTO #include "../src/journal.h" /* base */ #include /* C99 */ @@ -18,11 +21,13 @@ void date32_to_string(const union date32 d, char (*const a)[12]) { } static int day_compare(const union date32 a, const union date32 b) { return a.u32 > b.u32; } -static void day_to_string(const union date32 d, const union load *const entry, +typedef const char *ccs; +static void day_to_string(const union date32 d, + /*const union load *const entry*/ccs *const entry, char (*const a)[12]) { (void)entry; date32_to_string(d, a); } #define TREE_NAME day #define TREE_KEY union date32 -#define TREE_VALUE union load +#define TREE_VALUE /*union load*/ /*size_t*/ /*const char **/ ccs #define TREE_COMPARE #define TREE_TO_STRING #include "../src/tree.h" @@ -102,7 +107,7 @@ static unsigned looks_like_day(const char *const a) { #define OMIT_BASE -#define OMIT_DAY +#define OMIT_STRUCT #include "../src/journal.h" /* Just prototypes. */ /** Dynamic memory allocation for `j` will be zero, will @@ -118,13 +123,14 @@ struct journal journal(void) { const char *const dir_journal = "journal"; struct journal j = {0}; char *intent = 0; + //union { const char **text; uintptr_t *offset; } v; DIR *dir = 0; struct dirent *de = 0; struct int_array years = int_array(), months = int_array(), days = int_array(); int *y = 0, *y_end, *m = 0, *m_end, *d = 0, *d_end; struct day_tree_iterator it; - union load *v; + union { const char **text; uintptr_t *offset; } v; /* Get the years list as directories matching a year. */ if(chdir(dir_journal) == -1 || !(dir = opendir("."))) goto catch; @@ -183,19 +189,18 @@ struct journal journal(void) { fn, int_array_to_string(&days)); for(d = days.data, d_end = d + days.size; d < d_end; d++) { - union load *load; const union date32 d32 = { .year = (uint32_t)*y, .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 catch; - switch(day_tree_bulk_add(&j.days, d32, &load)) { + switch(day_tree_bulk_add(&j.days, d32, &v.text)) { 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); + *v.offset = (uintptr_t)(contents - j.backing.a.data); } d = 0, int_array_clear(&days); if(chdir("..") == -1) goto catch; @@ -208,10 +213,11 @@ struct journal journal(void) { /* Structure is now stable because we aren't going to move it; convert all of offsets to pointers. */ - it = day_tree_begin(&j.days); while(day_tree_next(&it, 0, &v)) { - /*printf("%zu\n", v->offset);*/ - v->text = j.backing.a.data + v->offset; - /*printf("%.60s\n", v->text);*/ + it = day_tree_begin(&j.days); + while(day_tree_next(&it, 0, &v.text)) { + /*printf("[%zu]...", *v.offset);*/ + *v.text = j.backing.a.data + *v.offset; + /*printf("<%.32s>\n", *v.text);*/ } /*fprintf(stderr, "Journal has entries: %s\n", day_tree_to_string(&j.days));*/ @@ -252,7 +258,9 @@ struct journal_iterator journal_begin(struct journal *const j) { }*/ int journal_next(struct journal_iterator *const it, - union date32 *const k, union load **v) { - return day_tree_next(&it->_, k, v); + union date32 *const k, const char **v) { + const char **needless_modifiable; + if(!day_tree_next(&it->_, k, &needless_modifiable)) return 0; + *v = *needless_modifiable; + return 1; } - diff --git a/test/test_journal.c b/test/test_journal.c index 8726365..b2110e7 100644 --- a/test/test_journal.c +++ b/test/test_journal.c @@ -9,6 +9,14 @@ int main(void) { int success = EXIT_SUCCESS; if(!journal_is_valid(&j)) goto catch; printf("Journal: %s.\n", journal_to_string(&j)); + { + struct journal_iterator it = journal_begin(&j); + union date32 date; + const char *value; + if(!(journal_next(&it, &date, &value))) goto catch; + printf("%u-%u-%u\n" + "<%s>", date.year, date.month, date.day, value); + } goto finally; catch: success = EXIT_FAILURE;