From 0f637febc96801c35de0e3754d53e2e3ce0aaf65 Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 19 Apr 2023 15:11:44 -0700 Subject: [PATCH] Reversed kjv. --- src/flighthours.c | 2 +- src/journal.h | 2 +- src/journal.re.c | 17 +++++++++---- src/kjv.h | 5 ++-- src/kjv.re.c | 58 ++++++++++++++++++++++++++++----------------- src/scores.h | 1 + src/scores.re.c | 46 +++++++++++++++++------------------ src/source.h | 2 +- src/table.h | 3 ++- test/test_journal.c | 2 +- test/test_source.c | 2 +- 11 files changed, 82 insertions(+), 58 deletions(-) diff --git a/src/flighthours.c b/src/flighthours.c index 9b6fefb..cd18f3b 100644 --- a/src/flighthours.c +++ b/src/flighthours.c @@ -14,7 +14,7 @@ int main(void) { int success = EXIT_SUCCESS; errno = 0; - struct journal j = journal(); + struct journal j = journal("../journal"); struct sources s = sources(&j); struct flight_tree f = flights(&j); diff --git a/src/journal.h b/src/journal.h index 728abb5..7186871 100644 --- a/src/journal.h +++ b/src/journal.h @@ -23,7 +23,7 @@ union line64 { #include "text.h" struct journal { struct day_tree days; struct char_array backing; }; struct journal_iterator { struct day_tree_iterator _; }; -struct journal journal(void); +struct journal journal(const char *); void journal_(struct journal *); int journal_is_empty(const struct journal *); const char *journal_to_string(const struct journal *); diff --git a/src/journal.re.c b/src/journal.re.c index 66bc249..dedf01a 100644 --- a/src/journal.re.c +++ b/src/journal.re.c @@ -112,10 +112,9 @@ void journal_(struct journal *const j) { text_(&j->backing); } -/** @return A completed journal out of "journal". Any reading errors and +/** @return A completed journal out of `dir_journal`. Any reading errors and `errno` will be set, it will be idle. */ -struct journal journal(void) { - const char *const dir_journal = "../journal"; +struct journal journal(const char *const dir_journal) { struct journal j = {0}; char *intent = 0; DIR *dir = 0; @@ -125,9 +124,13 @@ struct journal journal(void) { int *y = 0, *y_end, *m = 0, *m_end, *d = 0, *d_end; struct day_tree_iterator it; union { const char **text; uintptr_t *offset; } v; + char cwd[256]; + int is_dir_journal = 0; /* Get the years list as directories matching a year. */ - if(chdir(dir_journal) == -1 || !(dir = opendir("."))) goto catch; + if(!getcwd(cwd, sizeof cwd) || chdir(dir_journal) == -1) goto catch; + is_dir_journal = 1; + if(!(dir = opendir("."))) goto catch; while((de = readdir(dir))) { struct stat st; int year, *p; @@ -203,7 +206,7 @@ struct journal journal(void) { m = 0, int_array_clear(&months); if(chdir("..") == -1) goto catch; } - if(chdir("..") == -1 || !day_tree_bulk_finish(&j.days)) goto catch; + if(chdir("../..") == -1 || !day_tree_bulk_finish(&j.days)) goto catch; /* Structure is now stable because we aren't going to move it; convert all of offsets back to pointers. */ @@ -225,6 +228,10 @@ recatch: journal_(&j); finally: if(dir) { if(closedir(dir)) { dir = 0; goto recatch; } dir = 0; } + if(is_dir_journal) { + if(chdir(cwd) == -1) { is_dir_journal = 0; goto recatch; } + is_dir_journal = 0; + } int_array_(&years), int_array_(&months), int_array_(&days); /* Temporary. */ return j; } diff --git a/src/kjv.h b/src/kjv.h index 7c500cf..4a5bbdc 100644 --- a/src/kjv.h +++ b/src/kjv.h @@ -6,8 +6,8 @@ #define TREE_HEAD #include "../src/tree.h" -/* fixme?? */ -#include +#include /* fixme?? */ + void kjv_line_(struct kjvline_tree *); struct kjvline_tree kjv_line(struct journal *); int kjv_line_is_empty(const struct kjvline_tree *); @@ -15,3 +15,4 @@ const char *kjv_line_to_string(const struct kjvline_tree *); struct kjvline_tree_iterator kjv_line_iterator(struct kjvline_tree *); int kjv_line_next(struct kjvline_tree_iterator *, union line64 *, const struct kjvrange **); +int kjv_graphs(struct journal *, struct sources *); diff --git a/src/kjv.re.c b/src/kjv.re.c index 43ed702..bbbd7e2 100644 --- a/src/kjv.re.c +++ b/src/kjv.re.c @@ -233,31 +233,19 @@ int kjv_line_next(struct kjvline_tree_iterator *const it, union line64 *const k, return 1; } -int main(void) { - int success = EXIT_SUCCESS; - const char *reason = 0; - errno = 0; - struct kjvcount_table count = {0}; - struct journal jrnl = {0}; - struct sources srcs = {0}; +/** @return Success. */ +int kjv_graphs(struct journal *const jrnl, struct sources *const srcs) { struct kjvline_tree lines = {0}; + struct kjvcount_table count = {0}; + struct kjvset_table set = kjv_set(); size_t no_total; + const char *reason = 0; count = kjv_count(&no_total); fprintf(stderr, "KJV count: %s.\n", kjv_count_to_string(&count)); if(!no_total) { reason = "kjv failed to load"; goto catch; } - jrnl = journal(); - fprintf(stderr, "Journal: %s.\n", journal_to_string(&jrnl)); - if(journal_is_empty(&jrnl)) - { reason = "journal failed to load"; goto catch; } - - srcs = sources(&jrnl); - //fprintf(stderr, "Sources: %s.\n", sources_to_string(&srcs)); - if(sources_is_empty(&srcs)) - { reason = "sources failed to parse"; goto catch; } - - lines = kjv_line(&jrnl); + lines = kjv_line(jrnl); fprintf(stderr, "KJV lines: %s.\n", kjv_line_to_string(&lines)); if(kjv_line_is_empty(&lines)) { reason = "KJV lines parsing failed"; goto catch; } @@ -265,7 +253,6 @@ int main(void) { struct kjvline_tree_iterator it = kjv_line_iterator(&lines); union line64 line; const struct kjvrange *range; - struct kjvset_table set = kjv_set(); /*printf("set term postscript eps enhanced\n" "set output \"kjv.eps\"\n");*/ @@ -281,7 +268,7 @@ int main(void) { while(kjv_line_next(&it, &line, &range)) { char citestr[12], datestr[12]; size_t words = 0, newwords = 0; - const struct source *src = source_lookup(&srcs, line); /* Source. */ + const struct source *src = source_lookup(srcs, line); /* Source. */ if(!src || !src->name.a) { errno = EDOM; goto catch; } date32_to_string(line.date, &datestr); /* Date. */ kjvcite_to_string(range->start, &citestr); /* KJV cite. */ @@ -348,13 +335,40 @@ int main(void) { " for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n", no_total, no_total, no_total, no_total, no_total, no_total, no_total); goto finally; +catch: + perror(reason); +finally: + kjv_line_(&lines); + kjv_count_(&count); + return !reason; +} + +int main(void) { + int success = EXIT_SUCCESS; + const char *reason = 0; + errno = 0; + struct journal jrnl = {0}; + struct sources srcs = {0}; + + jrnl = journal("../journal"); + fprintf(stderr, "Journal: %s.\n", journal_to_string(&jrnl)); + if(journal_is_empty(&jrnl)) + { reason = "journal failed to load"; goto catch; } + + srcs = sources(&jrnl); + //fprintf(stderr, "Sources: %s.\n", sources_to_string(&srcs)); + if(sources_is_empty(&srcs)) + { reason = "sources failed to parse"; goto catch; } + + if(!kjv_graphs(&jrnl, &srcs)) + { reason = "graphs failed"; goto catch; } + + goto finally; catch: success = EXIT_FAILURE; perror("journal"); if(reason) fprintf(stderr, "Details: %s.\n", reason); finally: - kjv_line_(&lines); - kjv_count_(&count); journal_(&jrnl); return success; } diff --git a/src/scores.h b/src/scores.h index 1e6f49d..24151ca 100644 --- a/src/scores.h +++ b/src/scores.h @@ -26,4 +26,5 @@ struct scores { void scores_(struct scores *); struct scores scores(struct journal *); int scores_is_empty(const struct scores *); +int scores_graph(struct scores *); const char *scores_to_string(const struct scores *); diff --git a/src/scores.re.c b/src/scores.re.c index 85cff21..15a98a1 100644 --- a/src/scores.re.c +++ b/src/scores.re.c @@ -3,11 +3,6 @@ Scan journal entries for score. */ -#if 0 -#include /* C99 */ -#include -#endif - #include "../src/journal.h" #include "../src/scores.h" #include @@ -191,27 +186,14 @@ int scores_is_empty(const struct scores *const s) const char *scores_to_string(const struct scores *const s) { return assert(s), scorelist_array_to_string(&s->list); } -#include - -int main(void) { - const char *fail = 0; - struct journal jrnl = {0}; - struct scores scrs = {0}; - errno = 0; - - jrnl = journal(); - fprintf(stderr, "Journal: %s.\n", journal_to_string(&jrnl)); - if(journal_is_empty(&jrnl)) { fail = "journal failed to load"; goto catch; } - scrs = scores(&jrnl); - fprintf(stderr, "Scores: %s.\n", scores_to_string(&scrs)); - if(scores_is_empty(&scrs)) { fail = "scores failed to parse"; goto catch; } - - struct score_tree_iterator it = score_tree_iterator(&scrs.dates); +int scores_graph(struct scores *const scrs) { + struct score_tree_iterator it = score_tree_iterator(&scrs->dates); union line64 line; struct score *score; /* Set score to zero to verify count with paper journal. */ - for(size_t i = 0; i < scrs.list.size; i++) scrs.list.data[i].tempscore = 0; + for(size_t i = 0; i < scrs->list.size; i++) + scrs->list.data[i].tempscore = 0; /* 840 with legend; only useful to me. */ printf("set terminal pngcairo dashed transparent truecolor" @@ -221,7 +203,7 @@ int main(void) { "# date, key, key score\n"); while(score_tree_next(&it)) { line = score_tree_key(&it); - score = scrs.list.data + *score_tree_value(&it); + score = scrs->list.data + *score_tree_value(&it); char datestr[12]; date32_to_string(line.date, &datestr); score->tempscore++; @@ -270,6 +252,24 @@ int main(void) { " (total/2.): \\\n" " (getIndex(strcol(2))) w boxxy lc var lw 1, \\\n" " for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n"); + return 1; +} + +#include + +int main(void) { + const char *fail = 0; + struct journal jrnl = {0}; + struct scores scrs = {0}; + errno = 0; + + jrnl = journal("../journal"); + fprintf(stderr, "Journal: %s.\n", journal_to_string(&jrnl)); + if(journal_is_empty(&jrnl)) { fail = "journal failed to load"; goto catch; } + scrs = scores(&jrnl); + fprintf(stderr, "Scores: %s.\n", scores_to_string(&scrs)); + if(scores_is_empty(&scrs)) { fail = "scores failed to parse"; goto catch; } + if(!scores_graph(&scrs)) goto catch; goto finally; catch: diff --git a/src/source.h b/src/source.h index 55cf67c..d6729f1 100644 --- a/src/source.h +++ b/src/source.h @@ -25,4 +25,4 @@ struct sources sources(struct journal *); void sources_(struct sources *); int sources_is_empty(const struct sources *); const char *sources_to_string(const struct sources *); -const struct source *source_lookup(struct sources *s, const union line64 range); +const struct source *source_lookup(struct sources *, const union line64); diff --git a/src/table.h b/src/table.h index 53a9c6d..abcd243 100644 --- a/src/table.h +++ b/src/table.h @@ -524,9 +524,10 @@ static struct PN_(bucket) *PN_(element)(const struct PN_(iterator) *const it) /** @return Whether `it` even has a next. */ static int PN_(next)(struct PN_(iterator) *const it) { const struct N_(table) *const t = it->table; - const PN_(uint) limit = PN_(capacity)(t); + PN_(uint) limit; assert(it && it->table); if(!it->table->buckets) return 0; /* Idle. */ + limit = PN_(capacity)(t); while(++it->i < limit) if(t->buckets[it->i].next != TABLE_NULL) return 1; return 0; } diff --git a/test/test_journal.c b/test/test_journal.c index c42a468..dee17fd 100644 --- a/test/test_journal.c +++ b/test/test_journal.c @@ -7,7 +7,7 @@ int main(void) { errno = 0; /* `errno` is not set correctly to 0 in some debug situations. */ - struct journal j = journal(); + struct journal j = journal("../journal"); int success = EXIT_SUCCESS; if(errno) goto catch; printf("Journal: %s.\n", journal_to_string(&j)); diff --git a/test/test_source.c b/test/test_source.c index 3c64710..d24f7ad 100644 --- a/test/test_source.c +++ b/test/test_source.c @@ -7,7 +7,7 @@ int main(void) { int success = EXIT_SUCCESS; errno = 0; /* `errno` is not set correctly to 0 in some debug situations. */ - struct journal j = journal(); + struct journal j = journal("../journal"); struct sources s = sources(&j); if(errno) goto catch;