/** @license 2022 Neil Edelman, distributed under the terms of the [MIT License](https://opensource.org/licenses/MIT). Scan journal. */ #include "../src/journal.h" #include "../src/scan.h" #include #include #include /* Tree mapping from date-line in the journal to indices in whatever array. */ static void linemap_to_string(const union line64 line, const size_t *const u, char (*const a)[12]) { (void)u; date32_to_string(line.date, a); } static int linemap_compare(const union line64 a, const union line64 b) { return a.u64 > b.u64; } #define TREE_NAME linemap #define TREE_KEY union line64 #define TREE_VALUE size_t #define TREE_COMPARE #define TREE_TO_STRING #define TREE_DEFAULT 0 #define TREE_BODY #include "../src/tree.h" /* Array of sources. */ static void source_to_string(const struct source *const s, char (*const z)[12]) { const char *a = s->name.a, *b; char *y = *z; b = s->name.b <= a + 11 ? s->name.b : a + 11; while(a < b) *(y++) = *(a++); *y = '\0'; } #define ARRAY_NAME source #define ARRAY_TYPE struct source #define ARRAY_TO_STRING #define ARRAY_BODY #include "../src/array.h" /* Array of scores. */ static void score_to_string(const struct score *const s, char (*const z)[12]) { const char *a = s->name.a, *b; char *y = *z; b = s->name.b <= a + 11 ? s->name.b : a + 11; while(a < b) *(y++) = *(a++); *y = '\0'; } #define ARRAY_NAME score #define ARRAY_TYPE struct score #define ARRAY_TO_STRING #define ARRAY_BODY #include "../src/array.h" /* Glider tree. */ static void glider_to_string(const union line64 line, const struct glider *g, char (*const a)[12]) { (void)g; date32_to_string(line.date, a); } static int glider_compare(const union line64 a, const union line64 b) { return a.u64 > b.u64; } #define TREE_NAME glider #define TREE_KEY union line64 #define TREE_VALUE struct glider #define TREE_COMPARE #define TREE_TO_STRING #define TREE_BODY #include "../src/tree.h" /* Flight tree. */ static void flight_to_string(const union line64 line, const struct flight *f, char (*const a)[12]) { (void)f; date32_to_string(line.date, a); } static int flight_compare(const union line64 a, const union line64 b) { return a.u64 > b.u64; } #define TREE_NAME flight #define TREE_KEY union line64 #define TREE_VALUE struct flight #define TREE_COMPARE #define TREE_TO_STRING #define TREE_BODY #include "../src/tree.h" /* KJV tree. */ static void kjvline_to_string(const union line64 line, const struct kjvrange *u, char (*const a)[12]) { (void)u; date32_to_string(line.date, a); } static int kjvline_compare(const union line64 a, const union line64 b) { return a.u64 > b.u64; } #define TREE_NAME kjvline #define TREE_KEY union line64 #define TREE_VALUE struct kjvrange #define TREE_COMPARE #define TREE_TO_STRING #define TREE_BODY #include "../src/tree.h" /*!conditions:re2c*/ static int scan_day(struct scan *const scan, union date32 date, const char *const buffer) { const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *yyt3, *s0, *s1, *t0, *t1; enum YYCONDTYPE condition = yycline; size_t line = 1; char datestr[12] = {0}; const char *fail = "perhaps a bat?"; struct score *new_score = 0; struct glider *new_glider = 0; struct flight *new_flight = 0; enum kjv_book book = Revelation; uint32_t chapter = 0, verse = 0, verse_end = 0; assert(scan && date.u32 && buffer); YYCURSOR = YYMARKER = yyt1 = buffer; /*!re2c /**/ re2c:define:YYCTYPE = char; re2c:yyfill:enable = 0; re2c:define:YYGETCONDITION = "condition"; re2c:define:YYSETCONDITION = "condition = @@;"; re2c:define:YYGETCONDITION:naked = 1; re2c:define:YYSETCONDITION:naked = 1; ws = [ \t]; glyph = [^\x00-\x20\x7f]; // [^\x00\n\t ] + all weird semitext = glyph \ ";"; natural = [1-9][0-9]*; zero_natural = [0-9]+; decimal = "-"? zero_natural ("." zero_natural?)?; uint = [0-9]+; keyword = [A-Za-z0-9][A-Za-z0-9_-]*; label = semitext+ (" " semitext+)*; // perhaps??? do we want to include ;? date = natural "-" [0-1][0-9] "-" [0-3][0-9]; minutes = [0-5][0-9]; airport = [A-Z0-9]{4,4}; kjvlookat = ws* natural ":" natural [ab]? ("-" natural [ab]?)? ws+ "--" ws+; first = ("I" | "1") " "?; second = ("II" | "2") " "?; third = ("III" | "3") " "?; */ for( ; ; ) { /*!re2c /**/ [^\n\x00] { continue; } /* Default ignore. */ "\x00" { fail = "no newline at end of file"; goto catch; } "\x00" { return 1; } /* End of day. */ "\n" => line { line++; continue; } * :=> skip "->" :=> place "--" / [^-] :=> source "::" / [^:] :=> score "[glider]" :=> glider_type "[flight]" :=> flight_type * { fail = "place unrecognized"; goto catch; } @t0 "(" decimal "," decimal ")" @t1 ws+ @s0 label @s1 / "\n" => skip { const struct pair keyword = pair(s0, s1); const struct pair coords = pair(t0, t1); fprintf(stderr, "place: %.*s <<%.*s>>\n", (int)(t1-t0), t0, (int)(s1-s0), s0); continue; } @s0 label @s1 / "\n" => skip { const struct pair keyword = pair(s0, s1); fprintf(stderr, "place: <<%.*s>>\n", (int)(s1-s0), s0); continue; } * { fail = "source unrecognized"; goto catch; } @s0 keyword @s1 / "\n" => skip { also_source: { const struct pair keyword = pair(s0, s1); const union line64 key = { { (uint32_t)line, date } }; size_t i, *pi; if(line > UINT32_MAX) { errno = ERANGE; fail = "too many lines of text"; goto catch; } if(!(i = pair_map_table_get(&scan->sources.map, keyword))) { fail = "keyword not introduced"; goto catch; } switch(linemap_tree_bulk_assign(&scan->sources.dates, key, &pi)) { case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch; case TREE_ABSENT: *pi = i; break; } date32_to_string(date, &datestr); fprintf(stderr, "%s: source <<%.*s>>\n", datestr, (int)(s1 - s0), s0); } continue; } /* New source. fixme: desc not set. */ @s0 keyword @s1 ":" [^\x00\n]+ / "\n" => skip { struct pair keyword = pair(s0, s1); size_t *idx; struct source *source; switch(pair_map_table_assign(&scan->sources.map, keyword, &idx)) { case TABLE_PRESENT: errno = EDOM; fail = "new keyword already used"; case TABLE_ERROR: goto catch; /* /\ _Sic_. */ case TABLE_ABSENT: *idx = 0; break; /* Good. */ } if(!(source = source_array_new(&scan->sources.array))) goto catch; *idx = (size_t)(source - scan->sources.array.data); source->name.a = s0, source->name.b = s1; source->desc.a = 0, source->desc.b = 0; date32_to_string(date, &datestr); fprintf(stderr, "%s: new source <<%.*s>> stored in list at %zu.\n", datestr, (int)(s1 - s0), s0, *idx); goto also_source; } * { fail = "score unrecognized"; goto catch; } /* Already there. Use the map to get the index from the keyword and then stick a marker in the tree with that index. */ @s0 keyword @s1 / "\n" => skip { new_score: { const struct pair keyword = pair(s0, s1); const union line64 key = { { (uint32_t)line, date } }; size_t idx, *pidx; if(line > UINT32_MAX) { errno = ERANGE; fail = "too many lines of text"; goto catch; } if(!(idx = pair_map_table_get(&scan->scores.map, keyword))) { fail = "keyword not introduced"; goto catch; } if(scan->scores.array.data[idx].last.u32 >= date.u32) { fail = "duplicate key in same day"; goto catch; } scan->scores.array.data[idx].last.u32 = date.u32; switch(linemap_tree_bulk_assign(&scan->scores.dates, key, &pidx)) { case TREE_PRESENT: assert(0); fail = "duplicate key"; /* _Sic_. */ case TREE_ERROR: goto catch; case TREE_ABSENT: *pidx = idx; break; } date32_to_string(date, &datestr); fprintf(stderr, "%s: score <%.*s>\n", datestr, (int)(s1 - s0), s0); } continue; } /* New score. */ @s0 keyword @s1 ":" => score_name { size_t *idx; assert(!new_score); /* Create a new mapping from dateline to scores array. */ switch(pair_map_table_assign(&scan->scores.map, pair(s0, s1), &idx)) { case TABLE_PRESENT: errno = EDOM; fail = "new keyword already used"; case TABLE_ERROR: goto catch; /* _Sic_. */ case TABLE_ABSENT: *idx = 0; break; } /* New entry in the scores array for this map to point to. */ if(!(new_score = score_array_new(&scan->scores.array))) goto catch; *idx = (size_t)(new_score - scan->scores.array.data); /* Offset. */ /*struct pair key, name; union date32 date, last; unsigned edges;*/ new_score->key.a = s0, new_score->key.b = s1; new_score->name.a = 0, new_score->name.b = 0; new_score->date.u32 = new_score->last.u32 = 0; new_score->edges = 0, new_score->score = 0; date32_to_string(date, &datestr); fprintf(stderr, "%s: new score <%.*s> stored in list at %zu.\n", datestr, (int)(s1 - s0), s0, *idx); goto new_score; } * { fail = "name unrecognized"; goto catch; } ws* @s0 semitext+ (" " semitext+)* @s1 /* ws* */ ";" => score_date { assert(new_score); new_score->name.a = s0, new_score->name.b = s1; continue; } * { fail = "date unrecognized"; goto catch; } ws* "~"? @s0 date ws* ";" => score_edges { assert(new_score); if(!pair_to_date(s0, &new_score->date)) goto catch; continue; } * { fail = "edges unrecognized"; goto catch; } ws* "~"? @s0 uint @s1 ws* / "\n" => skip { assert(new_score); if(!pair_to_natural(s0, s1, &new_score->edges)) goto catch; new_score = 0; /* Done. */ continue; } /* type, reg, launch, how, height, landing, pilot, dual, instr, remarks eg, [glider] 2-33A; C-GCLK; CYQQ; A; 2000'; CYQQ; ;:13;; Peters D1 */ * { fail = "glider type"; goto catch; } ws* @s0 semitext+ @s1 ws* ";" => glider_reg { const union line64 key = {{ (uint32_t)line, date }}; assert(!new_glider); if(line > UINT32_MAX) { fail = "line overflow"; goto catch; } switch(glider_tree_bulk_assign(&scan->gliders, key, &new_glider)) { case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch; case TREE_ABSENT: break; } new_glider->type.a = s0, new_glider->type.b = s1; continue; } * { fail = "glider reg"; goto catch; } ws* @s0 semitext+ @s1 ws* ";" => glider_launch { new_glider->reg.a = s0, new_glider->reg.b = s1; continue; } * { fail = "glider launch"; goto catch; } ws* @s0 airport @s1 ws* ";" => glider_how { new_glider->launch.a = s0, new_glider->launch.b = s1; date32_to_string(date, &datestr); fprintf(stderr, "%s: glider <%.*s> at <%.*s>\n", datestr, (int)(new_glider->reg.b - new_glider->reg.a), new_glider->reg.a, (int)(s1 - s0), s0); continue; } * { fail = "glider how"; goto catch; } ws* @s0 [MWA] ws* ";" => glider_height { switch(*s0) { case 'M': new_glider->how = MotorCarTow; break; case 'W': new_glider->how = Winch; break; case 'A': new_glider->how = AeroTow; break; } continue; } * { fail = "glider height"; goto catch; } ws* @s0 natural @s1 "'" ws* ";" => glider_landing { if(!pair_to_natural(s0, s1, &new_glider->height_ft)); continue; } * { fail = "glider landing"; goto catch; } ws* @s0 airport @s1 ws* ";" => glider_pilot { new_glider->landing.a = s0, new_glider->landing.b = s1; continue;} * { fail = "glider pilot time"; goto catch; } ws* ";" => glider_dual /* not PIC */ { new_glider->pilot_min = 0; continue; } ws* @s0 natural? @s1 ":" @t0 minutes @t1 ws* ";" => glider_dual { if(!pair_colon_to_minutes(s0, s1, t0, t1, &new_glider->pilot_min)) { fail = "glider pilot time"; goto catch; } continue; } * { fail = "glider dual time"; goto catch; } ws* ";" => glider_instr { new_glider->dual_min = 0; continue; } ws* @s0 natural? @s1 ":" @t0 minutes @t1 ws* ";" => glider_instr { if(!pair_colon_to_minutes(s0, s1, t0, t1, &new_glider->dual_min)) { fail = "glider dual time"; goto catch; } continue; } * { fail = "glider instr time"; goto catch; } ws* ";" => glider_remarks { new_glider->instr_min = 0; continue; } ws* @s0 natural? @s1 ":" @t0 minutes @t1 ws* ";" => glider_remarks { if(!pair_hours_to_minutes(s0, s1, t0, t1, &new_glider->instr_min)) { fail = "glider instr time"; goto catch; } continue; } * { fail = "glider remarks"; goto catch; } ws* "\n" => line { new_glider->remarks.a = new_glider->remarks.b = 0; new_glider = 0; line++; continue; } ws* @s0 glyph+ (" " glyph+)* @s1 "\n" => line { new_glider->remarks.a = s0, new_glider->remarks.b = s1; new_glider = 0; line++; continue; } /* type; registration; launch -- landing; pic; sic; single engine day dual; pilot; instrument simulated; actual; remarks */ * { fail = "flight type"; goto catch; } ws* @s0 semitext+ @s1 ws* ";" => flight_reg { const union line64 key = {{ (uint32_t)line, {{ date.day, date.month, date.year }} }}; assert(!new_flight); if(line > UINT32_MAX) { fail = "line overflow"; goto catch; } switch(flight_tree_bulk_assign(&scan->flights, key, &new_flight)) { case TREE_PRESENT: fail = "duplicate key"; case TREE_ERROR: goto catch; case TREE_ABSENT: break; } new_flight->type.a = s0, new_flight->type.b = s1; continue; } * { fail = "flight reg"; goto catch; } ws* @s0 semitext+ @s1 ws* ";" => flight_airports { new_flight->reg.a = s0, new_flight->reg.b = s1; continue; } * { fail = "flight airports"; goto catch; } ws* @s0 airport @s1 ws* "--" ws* @t0 airport @t1 ws* ";" => flight_pic { new_flight->launch.a = s0, new_flight->launch.b = s1; new_flight->landing.a = t0, new_flight->landing.b = t1; date32_to_string(date, &datestr); fprintf(stderr, "%s: flight <%.*s> at <%.*s>\n", datestr, (int)(new_flight->reg.b - new_flight->reg.a), new_flight->reg.a, (int)(s1 - s0), s0); continue; } * { fail = "flight pic"; goto catch; } ws* @s0 semitext+ (ws+ semitext+)* @s1 /* ws*? */";" => flight_sic { new_flight->pilot.a = s0, new_flight->pilot.b = s1; continue; } * { fail = "flight sic"; goto catch; } ws* ";" => flight_dual { new_flight->copilot.a = new_flight->copilot.b = 0; continue; } ws* @s0 semitext+ (ws+ semitext+)* @s1 ";" => flight_dual { new_flight->copilot.a = s0, new_flight->copilot.b = s1; continue; } * { fail = "flight dual time"; goto catch; } ws* ";" => flight_pilot { new_flight->dual_min = 0; continue; } ws* @s0 zero_natural? @s1 "." @t0 [0-9] @t1 "h" ws* ";" => flight_pilot { if(!pair_hours_to_minutes(s0, s1, t0, t1, &new_flight->dual_min)) { fail = "flight dual time"; goto catch; } continue; } * { fail = "flight pilot time"; goto catch; } ws* ";" => flight_ifrsim { new_flight->pilot_min = 0; continue; } ws* @s0 zero_natural? @s1 "." @t0 [0-9] @t1 "h" ws* ";" => flight_ifrsim { if(!pair_hours_to_minutes(s0, s1, t0, t1, &new_flight->pilot_min)) { fail = "flight pilot time"; goto catch; } continue; } * { fail = "flight simulated ifr time"; goto catch; } ws* ";" => flight_ifr { new_flight->ifrsim_min = 0; continue; } ws* @s0 zero_natural? @s1 "." @t0 [0-9] @t1 "h" ws* ";" => flight_ifr { if(!pair_hours_to_minutes(s0, s1, t0, t1, &new_flight->ifrsim_min)) { fail = "flight simulated ifr time"; goto catch; } continue; } * { fail = "flight ifr time"; goto catch; } ws* ";" => flight_remarks { new_flight->ifr_min = 0; continue; } ws* @s0 zero_natural? @s1 "." @t0 [0-9] @t1 "h" ws* ";" => flight_remarks { if(!pair_hours_to_minutes(s0, s1, t0, t1, &new_flight->ifr_min)) { fail = "flight ifr time"; goto catch; } continue; } * { fail = "flight remarks"; goto catch; } ws* "\n" => line { new_flight->remarks.a = new_flight->remarks.b = 0; new_flight = 0; line++; continue; } ws* @s0 glyph+ (ws+ glyph+)* @s1 "\n" => line { new_flight->remarks.a = s0, new_flight->remarks.b = s1; new_flight = 0; line++; continue; } /* Books in KJV. */ "Genesis" / kjvlookat => book { book = Genesis; continue; } "Exodus" / kjvlookat => book { book = Exodus; continue; } "Leviticus" / kjvlookat => book { book = Leviticus; continue; } "Numbers" / kjvlookat => book { book = Numbers; continue; } "Deuteronomy" / kjvlookat => book { book = Deuteronomy; continue; } "Joshua" / kjvlookat => book { book = Joshua; continue; } "Judges" / kjvlookat => book { book = Judges; continue; } "Ruth" / kjvlookat => book { book = Ruth; continue; } first "Samuel" / kjvlookat => book { book = ISamuel; continue; } second "Samuel" / kjvlookat => book { book = IISamuel; continue; } first "Kings" / kjvlookat => book { book = IKings; continue; } second "Kings" / kjvlookat => book { book = IIKings; continue; } first "Chronicles" / kjvlookat => book { book = IChronicles; continue; } second "Chronicles" / kjvlookat => book { book = IIChronicles; continue; } "Ezra" / kjvlookat => book { book = Ezra; continue; } "Nehemiah" / kjvlookat => book { book = Nehemiah; continue; } "Esther" / kjvlookat => book { book = Esther; continue; } "Job" / kjvlookat => book { book = Job; continue; } "Psalms" / kjvlookat => book { book = Psalms; continue; } "Proverbs" / kjvlookat => book { book = Proverbs; continue; } "Ecclesiastes" / kjvlookat => book { book = Ecclesiastes; continue; } "Song of Solomon" / kjvlookat => book { book = Song_of_Solomon; continue; } "Isaiah" / kjvlookat => book { book = Isaiah; continue; } "Jeremiah" / kjvlookat => book { book = Jeremiah; continue; } "Lamentations" / kjvlookat => book { book = Lamentations; continue; } "Ezekiel" / kjvlookat => book { book = Ezekiel; continue; } "Daniel" / kjvlookat => book { book = Daniel; continue; } "Hosea" / kjvlookat => book { book = Hosea; continue; } "Joel" / kjvlookat => book { book = Joel; continue; } "Amos" / kjvlookat => book { book = Amos; continue; } "Obadiah" / kjvlookat => book { book = Obadiah; continue; } "Jonah" / kjvlookat => book { book = Jonah; continue; } "Micah" / kjvlookat => book { book = Micah; continue; } "Nahum" / kjvlookat => book { book = Nahum; continue; } "Habakkuk" / kjvlookat => book { book = Habakkuk; continue; } "Zephaniah" / kjvlookat => book { book = Zephaniah; continue; } "Haggai" / kjvlookat => book { book = Haggai; continue; } "Zechariah" / kjvlookat => book { book = Zechariah; continue; } "Malachi" / kjvlookat => book { book = Malachi; continue; } "Matthew" / kjvlookat => book { book = Matthew; continue; } "Mark" / kjvlookat => book { book = Mark; continue; } "Luke" / kjvlookat => book { book = Luke; continue; } "John" / kjvlookat => book { book = John; continue; } "Acts" / kjvlookat => book { book = Acts; continue; } "Romans" / kjvlookat => book { book = Romans; continue; } first "Corinthians" / kjvlookat => book { book = ICorinthians; continue; } second "Corinthians" / kjvlookat => book { book = IICorinthians; continue; } "Galatians" / kjvlookat => book { book = Galatians; continue; } "Ephesians" / kjvlookat => book { book = Ephesians; continue; } "Philippians" / kjvlookat => book { book = Philippians; continue; } "Colossians" / kjvlookat => book { book = Colossians; continue; } first "Thessalonians" / kjvlookat => book { book = IThessalonians; continue; } second "Thessalonians" / kjvlookat => book { book = IIThessalonians; continue; } first "Timothy" / kjvlookat => book { book = ITimothy; continue; } second "Timothy" / kjvlookat => book { book = IITimothy; continue; } "Titus" / kjvlookat => book { book = Titus; continue; } "Philemon" / kjvlookat => book { book = Philemon; continue; } "Hebrews" / kjvlookat => book { book = Hebrews; continue; } "James" / kjvlookat => book { book = James; continue; } first "Peter" / kjvlookat => book { book = IPeter; continue; } second "Peter" / kjvlookat => book { book = IIPeter; continue; } first "John" / kjvlookat => book { book = IJohn; continue; } second "John" / kjvlookat => book { book = IIJohn; continue; } third "John" / kjvlookat => book { book = IIIJohn; continue; } "Jude" / kjvlookat => book { book = Jude; continue; } "Revelation" / kjvlookat => book { book = Revelation; continue; } * { fail = "kjv unrecognized"; goto catch; } /* 19:15a, just ignore the a. */ ws+ @s0 natural @s1 ":" @t0 natural @t1 [ab]? { if(chapter || verse || verse_end) { fail = "kjv reference"; goto catch; } if(!pair_to_natural(s0, s1, &chapter) || !pair_to_natural(t0, t1, &verse)) { fail = "kjv reference numerical error"; goto catch; } continue; } "-" @s0 natural @s1 [ab]? { /* Verse range. */ if(!chapter || !verse || verse_end) { fail = "kjv range unrecognized"; goto catch; } if(!pair_to_natural(s0, s1, &verse_end)) { fail = "kjv range numerical error"; goto catch; } continue; } ws+ "--" ws+ => skip { if(!chapter || !verse) { fail = "kjv missing information"; goto catch; } if(verse_end && verse_end <= verse) { fail = "kjv interval error"; goto catch; } const union line64 key = {{ (uint32_t)line, date }}; struct kjvrange *value; switch(kjvline_tree_bulk_assign(&scan->kjvs, key, &value)) { case TREE_PRESENT: fail = "kjv duplicate key"; case TREE_ERROR: goto catch; case TREE_ABSENT: value->start.book = book; value->start.chapter = chapter; value->start.verse = verse; value->verse_end = verse_end; break; } date32_to_string(date, &datestr); fprintf(stderr, "%s: KJV %s %" PRIu32 ":%" PRIu32, datestr, kjv_book_string[book], chapter, verse); if(verse_end) fprintf(stderr, "-%u", verse_end); fprintf(stderr, "\n"); fprintf(stderr, ""); book = Revelation, chapter = 0, verse = 0, verse_end = 0; continue; } */ } assert(0); /* Never gets here. */ catch: if(!errno) errno = EILSEQ; date32_to_string(date, &datestr); fprintf(stderr, "%s line %zu: %s" /*" condition %d"*/ ".\n", datestr, line, fail/*, condition*/); return 0; } void scan_(struct scan *const scan) { if(!scan) return; linemap_tree_(&scan->scores.dates); pair_map_table_(&scan->scores.map); score_array_(&scan->scores.array); } /** @param[jrnl] Must be constant throughout the use of the returned value. */ struct scan scan(struct journal *const jrnl) { struct scan scan = {0}; struct journal_iterator it; union date32 date; const char *text; assert(jrnl); /* Null is the first item for convenience, (TABLE_DEFAULT). */ { struct source *nul; if(!(nul = source_array_new(&scan.sources.array))) goto catch; *nul = (struct source){0}; } { struct score *nul; if(!(nul = score_array_new(&scan.scores.array))) goto catch; *nul = (struct score){0}; } /* Scan all. */ it = journal_iterator(jrnl); while(journal_next(&it, &date, &text)) if(!scan_day(&scan, date, text)) goto catch; /* Scans make trees bulk-loaded; fix to real tree. */ if(!linemap_tree_bulk_finish(&scan.sources.dates) || !linemap_tree_bulk_finish(&scan.scores.dates) || !glider_tree_bulk_finish(&scan.gliders) || !flight_tree_bulk_finish(&scan.flights) || !kjvline_tree_bulk_finish(&scan.kjvs)) goto catch; goto finally; catch: scan_(&scan); finally: return scan; } /** Lookup the source of `x` in `scan`, if any. */ static const struct source *source_lookup(struct scan *const scan, const union line64 x) { struct linemap_tree_iterator it; assert(scan); it = linemap_tree_less(&scan->sources.dates, x); /* If it's before all elements of the journal or is not on the same date as the source, this has no source, which is `array[0]`. */ return scan->sources.array.data + (linemap_tree_has_element(&it) && x.date.u32 == linemap_tree_key(&it).date.u32 ? *linemap_tree_value(&it) : 0); } /** Lookup the source of `x` in `scan`, if any; public function for testing. */ const struct source *scan_source_lookup(struct scan *const scan, const union line64 x) { return source_lookup(scan, x); } void scan_score_graph(struct scan *const scan) { struct linemap_tree_iterator it = linemap_tree_iterator(&scan->scores.dates); union line64 line; struct score *score; assert(scan); /* Set score to zero to verify count with paper journal. */ for(struct score *i = scan->scores.array.data, *const z = i + scan->scores.array.size; i < z; i++) i->score = 0; /* 840 with legend; only useful to me. */ printf("set terminal pngcairo dashed transparent truecolor" " size 840, 480 fontscale 1\n" "set output \"score.png\"\n"); printf("$Data <scores.array.size); score = scan->scores.array.data + *linemap_tree_value(&it); char datestr[12]; date32_to_string(line.date, &datestr); score->score++; printf("%s, %.*s, %u\n", datestr, (int)(score->key.b - score->key.a), score->key.a, score->score); } printf("EOD\n" "# theozh https://stackoverflow.com/a/75466214/2472827\n" "# get a unique list from datablock\n" "addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n" " > 0 ? '' : ' \"'.strcol(col).'\"')\n" "Uniqs = ''\n" "stats $Data u (Uniqs=addToList(Uniqs,2)) nooutput\n" "Uniq(i) = word(Uniqs,i)\n" "getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n" "\n" /*"stats $Data u 3 nooutput\n" "cumsum = STATS_sum\n" "stats $Data u 4 nooutput\n" "setsum = STATS_sum\n" "\n"*/ "myTimeFmt = \"%%Y-%%m-%%d\"\n" "set format x myTimeFmt timedate\n" "set xtics format myTimeFmt rotate by -30\n" "set ylabel \"happiness and satisfaction CDF (days)\"\n" "set grid\n" "set key out reverse Left noautotitle\n" "set style fill solid 0.5\n" "unset border\n" "unset key\n" "set autoscale xfix # max? hack: can't get x to extend further\n" "\n" /*"set label sprintf(\"%%u cumulative words (duplicate verses counted)\"," " cumsum) center at graph 0.5, first cumsum*100/%zu offset 0,0.5\n" "set label sprintf(\"%%u unique KJV verse words memorized\", setsum) " "center at graph 0.5, first setsum*100/%zu offset 0,0.5\n" "\n"*/ "plot \\\n" " total=0 $Data u" " (timecolumn(1,myTimeFmt)):(total=total+1) \\\n" " w steps lc \"black\" dt 1 lw 1, \\\n" " total=0 '' u \\\n" " (timecolumn(1,myTimeFmt)): \\\n" " (total=total+1,total/2.): \\\n" " (43200): \\\n" " (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"); } #include void scan_glider_graph(struct scan *const scan) { assert(scan); fprintf(stderr, "Glider: %s.\n", glider_tree_to_string(&scan->gliders)); printf("set terminal pngcairo dashed transparent truecolor" " size 840, 480 fontscale 1\n" "set output \"glider.png\"\n"); /*printf("set terminal cairolatex standalone pdf size 16cm,10.5cm" " dashed transparent\n" "set output \"flight.tex\"\n");*/ /*printf("set term postscript eps enhanced\n" "set output \"flighthours.eps\"\n");*/ printf("$Data <gliders); while(glider_tree_next(&it)) { const union line64 line = glider_tree_key(&it); const struct glider *glider = glider_tree_value(&it); char datestr[12]; date32_to_string(line.date, &datestr); const struct source *src = source_lookup(scan, line); assert(src); if(!src->name.a) { fprintf(stderr, "Glider has no source at %s; ignoring.\n", datestr); continue; } printf("%s, ", datestr); printf("%.*s, %" PRIu32 ", %" PRIu32, (int)(glider->reg.b - glider->reg.a), glider->reg.a, glider->dual_min, glider->pilot_min + glider->instr_min); /* case POWER: printf("%.*s, %" PRIu32 ", %" PRIu32, (int)(flight->power.reg.b - flight->power.reg.a), flight->power.reg.a, flight->power.dual_min, flight->power.pilot_min); break; */ printf(", %.*s\n", (int)(src->name.b - src->name.a), src->name.a); } printf("EOD\n" "# theozh https://stackoverflow.com/a/75466214/2472827\n" "# get a unique list from datablock\n" "addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n" " > 0 ? '' : ' \"'.strcol(col).'\"')\n" "Uniqs = ''\n" "stats $Data u (Uniqs=addToList(Uniqs,2)) nooutput\n" "Uniq(i) = word(Uniqs,i)\n" "getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n" "\n" "stats $Data u 3 nooutput\n" "sicsum = STATS_sum\n" "stats $Data u 4 nooutput\n" "picsum = STATS_sum\n" "\n" "myTimeFmt = \"%%Y-%%m-%%d\"\n" "set format x myTimeFmt timedate\n" "set xtics format myTimeFmt rotate by -30\n" "set format y \"%%tH:%%tM\" timedate\n" "set grid\n" "set key out reverse Left noautotitle\n" "set style fill solid 0.5\n" "unset border\n" "plot total=0 $Data u" " (timecolumn(1,myTimeFmt)):(dy=($3+$4)*60,total=total+dy)" " w steps lc \"black\" dt 3, \\\n" " total=0 '' u (timecolumn(1,myTimeFmt)):" "(dy=($3+$4)*60,total=total+dy,total/2.): \\\n" " (43200):(total/2.):(getIndex(strcol(2))) w boxxy lc var, \\\n" " for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n" /*"set xrange [*:'2001-09-11']\n"*/ /*"#set style fill solid 0.1 #pattern 5 (better, but restarts)\n" "plot $Data using 1:($6/60) with fillsteps lw 2\n"*/); } void scan_flight_graph(struct scan *const scan) { assert(scan); fprintf(stderr, "Flight: %s.\n", flight_tree_to_string(&scan->flights)); printf("set terminal pngcairo dashed transparent truecolor" " size 840, 480 fontscale 1\n" "set output \"glider.png\"\n"); printf("$Data <flights); while(flight_tree_next(&it)) { const union line64 line = flight_tree_key(&it); const struct flight *flight = flight_tree_value(&it); char datestr[12]; date32_to_string(line.date, &datestr); const struct source *src = source_lookup(scan, line); assert(src); if(!src->name.a) { fprintf(stderr, "Source has no source at %s; ignoring.\n", datestr); continue; } printf("%s, ", datestr); printf("%.*s, %" PRIu32 ", %" PRIu32, (int)(flight->reg.b - flight->reg.a), flight->reg.a, flight->dual_min, flight->pilot_min); printf(", %.*s\n", (int)(src->name.b - src->name.a), src->name.a); } printf("EOD\n" "# theozh https://stackoverflow.com/a/75466214/2472827\n" "# get a unique list from datablock\n" "addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n" " > 0 ? '' : ' \"'.strcol(col).'\"')\n" "Uniqs = ''\n" "stats $Data u (Uniqs=addToList(Uniqs,2)) nooutput\n" "Uniq(i) = word(Uniqs,i)\n" "getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n" "\n" "stats $Data u 3 nooutput\n" "sicsum = STATS_sum\n" "stats $Data u 4 nooutput\n" "picsum = STATS_sum\n" "\n" "myTimeFmt = \"%%Y-%%m-%%d\"\n" "set format x myTimeFmt timedate\n" "set xtics format myTimeFmt rotate by -30\n" "set format y \"%%tH:%%tM\" timedate\n" "set grid\n" "set key out reverse Left noautotitle\n" "set style fill solid 0.5\n" "unset border\n" "plot total=0 $Data u" " (timecolumn(1,myTimeFmt)):(dy=($3+$4)*60,total=total+dy)" " w steps lc \"black\" dt 3, \\\n" " total=0 '' u (timecolumn(1,myTimeFmt)):" "(dy=($3+$4)*60,total=total+dy,total/2.): \\\n" " (43200):(total/2.):(getIndex(strcol(2))) w boxxy lc var, \\\n" " for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n"); } void scan_kjv_graph(struct scan *const scan) { 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; } fprintf(stderr, "KJV lines: %s.\n", kjvline_tree_to_string(&scan->kjvs)); struct kjvline_tree_iterator it = kjvline_tree_iterator(&scan->kjvs); /* https://stackoverflow.com/a/12601553 */ printf("set terminal pngcairo dashed transparent truecolor" " size 840, 480 fontscale 1\n" "set output \"kjv.png\"\n"); printf("$Data <name.a) { errno = EDOM; goto catch; } date32_to_string(line.date, &datestr); /* Date. */ kjvcite_to_string(range->start, &citestr); /* KJV cite. */ for(union kjvcite c = range->start; ; c.verse++) { size_t w = kjv_count_get(&count, c); words += w; switch (kjv_set_add(&set, c)) { case TABLE_ERROR: goto catch; case TABLE_ABSENT: newwords += w; case TABLE_PRESENT: break; } /* while(); */ if(!range->verse_end || range->verse_end <= c.verse) break; } printf("%s, %s", datestr, citestr); if(range->verse_end) printf("-%" PRIu32, range->verse_end); printf(", %zu, %zu, %.*s\n", words, newwords, (int)(src->name.b - src->name.a), src->name.a); } printf("EOD\n" "# theozh https://stackoverflow.com/a/75466214/2472827\n" "# get a unique list from datablock\n" "addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n" " > 0 ? '' : ' \"'.strcol(col).'\"')\n" "Uniqs = ''\n" "stats $Data u (Uniqs=addToList(Uniqs,5)) nooutput\n" "Uniq(i) = word(Uniqs,i)\n" "getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n" "\n" "stats $Data u 3 nooutput\n" "cumsum = STATS_sum\n" "stats $Data u 4 nooutput\n" "setsum = STATS_sum\n" "\n" "myTimeFmt = \"%%Y-%%m-%%d\"\n" "set format x myTimeFmt timedate\n" "set xtics format myTimeFmt rotate by -30\n" "set format y \"%%g%%%%\"\n" "set grid\n" "set key out reverse Left noautotitle\n" "set style fill solid 0.5\n" "unset border\n" "set autoscale xfix # max? hack: can't get x to extend further\n" "\n" "set label sprintf(\"%%u cumulative words (duplicate verses counted)\"," " cumsum) center at graph 0.5, first cumsum*100/%zu offset 0,0.5\n" "set label sprintf(\"%%u unique KJV verse words memorized\", setsum) " "center at graph 0.5, first setsum*100/%zu offset 0,0.5\n" "\n" "plot \\\n" " cumsum*100/%zu w l lc \"grey\" dt 2 lw 1, \\\n" " total=0 $Data u" " (timecolumn(1,myTimeFmt)):(dy=$3*100/%zu,total=total+dy) \\\n" " w steps lc \"grey\" dt 2 lw 1, \\\n" /* `pngcairo` has trouble :[. */ " total=0 $Data u" " (timecolumn(1,myTimeFmt)):(dy=$4*100/%zu,total=total+dy) \\\n" " w steps lc \"black\" dt 1 lw 1, \\\n" " setsum*100/%zu w l lc \"black\" dt 1 lw 1, \\\n" " total=0 '' u \\\n" " (timecolumn(1,myTimeFmt)): \\\n" " (dy=$4*100/%zu,total=total+dy,total/2.): \\\n" " (43200): \\\n" " (total/2.): \\\n" " (getIndex(strcol(5))) w boxxy lc var lw 1, \\\n" " 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_count_(&count); }