Reversed kjv.

This commit is contained in:
Neil 2023-04-19 15:11:44 -07:00
parent 092e530bca
commit 0f637febc9
11 changed files with 82 additions and 58 deletions

View File

@ -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);

View File

@ -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 *);

View File

@ -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;
}

View File

@ -6,8 +6,8 @@
#define TREE_HEAD
#include "../src/tree.h"
/* fixme?? */
#include <stddef.h>
#include <stddef.h> /* 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 *);

View File

@ -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;
}

View File

@ -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 *);

View File

@ -3,11 +3,6 @@
Scan journal entries for score. */
#if 0
#include <inttypes.h> /* C99 */
#include <limits.h>
#endif
#include "../src/journal.h"
#include "../src/scores.h"
#include <stdio.h>
@ -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 <stdlib.h>
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 <stdlib.h>
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:

View File

@ -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);

View File

@ -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;
}

View File

@ -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));

View File

@ -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;