load -> string

This commit is contained in:
Neil 2023-02-01 22:05:55 -08:00
parent 82852d94b0
commit 04acf72734
4 changed files with 38 additions and 25 deletions

View File

@ -36,10 +36,10 @@ default: $(projects)
# success # success
bin/test-text: build/text.o build/test_text.o 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-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/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/%: bin/%:
@echo "\033[1;36mlinking $@\033[0m" @echo "\033[1;36mlinking $@\033[0m"

View File

@ -1,6 +1,5 @@
#ifndef OMIT_BASE /* <!-- base */ #ifndef OMIT_BASE /* <!-- base */
#include <stddef.h> #include <stddef.h>
union load { const char *text; size_t offset; };
#include <stdint.h> /* C99 */ #include <stdint.h> /* C99 */
/** Assumes: reverse ordering of byte-fields; unsigned is defined; C11 or GNU /** Assumes: reverse ordering of byte-fields; unsigned is defined; C11 or GNU
anonymous unions. */ anonymous unions. */
@ -18,7 +17,7 @@ union line64 {
#endif /* !base --> */ #endif /* !base --> */
#ifndef OMIT_DAY /* <!-- page */ #ifndef OMIT_STRUCT /* <!-- external */
struct tree_day_node; struct tree_day_node;
struct tree_day_tree { struct tree_day_node *node; unsigned height; }; struct tree_day_tree { struct tree_day_node *node; unsigned height; };
struct day_tree { struct tree_day_tree root; }; struct day_tree { struct tree_day_tree root; };
@ -27,9 +26,9 @@ struct tree_day_iterator {
struct tree_day_tree *root; struct tree_day_ref ref; int seen; struct tree_day_tree *root; struct tree_day_ref ref; int seen;
}; };
struct day_tree_iterator { struct tree_day_iterator _; }; struct day_tree_iterator { struct tree_day_iterator _; };
#else /* page --><!-- !page */ #else /* external --><!-- internal */
#undef OMIT_DAY #undef OMIT_STRUCT
#endif /* !page --> */ #endif /* internal --> */
#ifndef OMIT_PROTO /* <!-- proto */ #ifndef OMIT_PROTO /* <!-- proto */
@ -41,9 +40,7 @@ void journal_(struct journal *);
int journal_is_valid(const struct journal *); int journal_is_valid(const struct journal *);
const char *journal_to_string(const struct journal *); const char *journal_to_string(const struct journal *);
struct journal_iterator journal_begin(struct journal *const j); struct journal_iterator journal_begin(struct journal *const j);
struct journal_iterator journal_begin_at(struct journal *, const union date32); int journal_next(struct journal_iterator *, union date32 *, /*union load ***/const char **);
union load;
int journal_next(struct journal_iterator *, union date32 *, union load **);
#else /* proto --><!-- !proto */ #else /* proto --><!-- !proto */
#undef OMIT_PROTO #undef OMIT_PROTO
#endif /* !proto --> */ #endif /* !proto --> */

View File

@ -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 #define OMIT_PROTO
#include "../src/journal.h" /* base */ #include "../src/journal.h" /* base */
#include <inttypes.h> /* C99 */ #include <inttypes.h> /* 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) static int day_compare(const union date32 a, const union date32 b)
{ return a.u32 > b.u32; } { 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); } char (*const a)[12]) { (void)entry; date32_to_string(d, a); }
#define TREE_NAME day #define TREE_NAME day
#define TREE_KEY union date32 #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_COMPARE
#define TREE_TO_STRING #define TREE_TO_STRING
#include "../src/tree.h" #include "../src/tree.h"
@ -102,7 +107,7 @@ static unsigned looks_like_day(const char *const a) {
#define OMIT_BASE #define OMIT_BASE
#define OMIT_DAY #define OMIT_STRUCT
#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 /** Dynamic memory allocation for `j` will be zero, <fn:journal_is_valid> will
@ -118,13 +123,14 @@ struct journal journal(void) {
const char *const dir_journal = "journal"; const char *const dir_journal = "journal";
struct journal j = {0}; struct journal j = {0};
char *intent = 0; char *intent = 0;
//union { const char **text; uintptr_t *offset; } v;
DIR *dir = 0; DIR *dir = 0;
struct dirent *de = 0; struct dirent *de = 0;
struct int_array years = int_array(), months = int_array(), struct int_array years = int_array(), months = int_array(),
days = int_array(); days = int_array();
int *y = 0, *y_end, *m = 0, *m_end, *d = 0, *d_end; int *y = 0, *y_end, *m = 0, *m_end, *d = 0, *d_end;
struct day_tree_iterator it; 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. */ /* Get the years list as directories matching a year. */
if(chdir(dir_journal) == -1 || !(dir = opendir("."))) goto catch; if(chdir(dir_journal) == -1 || !(dir = opendir("."))) goto catch;
@ -183,19 +189,18 @@ struct journal journal(void) {
fn, int_array_to_string(&days)); fn, int_array_to_string(&days));
for(d = days.data, d_end = d + days.size; d < d_end; d++) { for(d = days.data, d_end = d + days.size; d < d_end; d++) {
union load *load;
const union date32 d32 = { .year = (uint32_t)*y, const union date32 d32 = { .year = (uint32_t)*y,
.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 catch; 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_PRESENT: intent = "not unique", errno = EDOM; /*sic*/
case TREE_ERROR: goto catch; 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); *v.offset = (uintptr_t)(contents - j.backing.a.data);
} }
d = 0, int_array_clear(&days); d = 0, int_array_clear(&days);
if(chdir("..") == -1) goto catch; 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; /* Structure is now stable because we aren't going to move it;
convert all of offsets to pointers. */ convert all of offsets to pointers. */
it = day_tree_begin(&j.days); while(day_tree_next(&it, 0, &v)) { it = day_tree_begin(&j.days);
/*printf("%zu\n", v->offset);*/ while(day_tree_next(&it, 0, &v.text)) {
v->text = j.backing.a.data + v->offset; /*printf("[%zu]...", *v.offset);*/
/*printf("%.60s\n", v->text);*/ *v.text = j.backing.a.data + *v.offset;
/*printf("<%.32s>\n", *v.text);*/
} }
/*fprintf(stderr, "Journal has entries: %s\n", /*fprintf(stderr, "Journal has entries: %s\n",
day_tree_to_string(&j.days));*/ 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, int journal_next(struct journal_iterator *const it,
union date32 *const k, union load **v) { union date32 *const k, const char **v) {
return day_tree_next(&it->_, k, v); const char **needless_modifiable;
if(!day_tree_next(&it->_, k, &needless_modifiable)) return 0;
*v = *needless_modifiable;
return 1;
} }

View File

@ -9,6 +9,14 @@ int main(void) {
int success = EXIT_SUCCESS; int success = EXIT_SUCCESS;
if(!journal_is_valid(&j)) goto catch; if(!journal_is_valid(&j)) goto catch;
printf("Journal: %s.\n", journal_to_string(&j)); 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; goto finally;
catch: catch:
success = EXIT_FAILURE; success = EXIT_FAILURE;