/** @license 2023 Neil Edelman, distributed under the terms of the [MIT License](https://opensource.org/licenses/MIT). @std C11 */ #define BASE #include "../src/source.h" /* base */ #include "../src/journal.h" #include #include #include #include /* `sourcelist` is an array of all the sources. */ static void sourcelist_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 sourcelist #define ARRAY_TYPE struct source #define ARRAY_TO_STRING #include "../src/array.h" /* `sourcemap` maps from substring keywords to indices in `sourcelist`. */ static void sourcemap_to_string(const struct pair key, const size_t i, char (*const a)[12]) { (void)key; sprintf(*a, "%zu", i); } static int sourcemap_is_equal(const struct pair a, const struct pair b) { return pair_is_equal(a, b); } static uint32_t sourcemap_hash(const struct pair p) { return pair_djb2(p); } #define TABLE_NAME sourcemap #define TABLE_KEY struct pair #define TABLE_UINT uint32_t #define TABLE_VALUE size_t /* Index into source list. */ #define TABLE_DEFAULT 0 #define TABLE_TO_STRING #include "../src/table.h" /* `source` is a tree mapping from date-line to indices in `sourcelist`. */ static void source_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 source_compare(const union line64 a, const union line64 b) { return a.u64 > b.u64; } #define TREE_NAME source #define TREE_KEY union line64 #define TREE_VALUE size_t /* Index into source list. */ #define TREE_COMPARE #define TREE_TO_STRING #define TREE_DEFAULT 0 #include "../src/tree.h" #define PROTO #include "../src/source.h" /* proto */ /*!conditions:re2c*/ static int scan(union date32 date, const char *const buffer, struct sources *const s) { const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *s0, *s1; enum YYCONDTYPE condition = yycline; size_t line = 1; char datestr[12] = {0}; const char *why = "unexpected"; assert(buffer && s); 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; unix_control = [\x01-\x08\x0b-\x1f\x7f]; ws = [ \t]; glyph = [^] \ ("\x00" | "\n" | unix_control | ws); keyword = [A-Za-z0-9][A-Za-z0-9_-]*; */ for( ; ; ) { /*!re2c /**/ /* Default ignore. */ [^\n\x00] { continue; } "\x00" { why = "no newline at end of file"; goto catch; } "\x00" { return 1; } "\n" => line { line++; continue; } * :=> skip "--" / [^-] :=> source * { why = "default source unrecognized"; goto catch; } @s0 keyword @s1 / "\n" => skip { also_add_to_tree: { 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; why = "too many lines of text"; goto catch; } if(!(i = sourcemap_table_get(&s->map, keyword))) { why = "keyword not introduced"; goto catch; } switch(source_tree_try(&s->dates, key, &pi)) { case TREE_PRESENT: why = "duplicate key"; /* _Sic_. */ case TREE_ERROR: goto catch; case TREE_ABSENT: *pi = i; break; } /*date32_to_string(date, &datestr); printf("%s: <%.*s>\n", datestr, (int)(s1 - s0), s0);*/ continue; } } /* This is lazy and will pickup trailing spaces. */ @s0 keyword @s1 ":" [^\x00\n]+ / "\n" => skip { struct pair keyword = pair(s0, s1); size_t *idx; struct source *source; switch(sourcemap_table_assign(&s->map, keyword, &idx)) { case TABLE_PRESENT: errno = EDOM; why = "new keyword already used"; case TABLE_ERROR: goto catch; /* /\ _Sic_. */ case TABLE_ABSENT: *idx = 0; break; /* Good. */ } if(!(source = sourcelist_array_new(&s->list))) goto catch; *idx = (size_t)(source - s->list.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_add_to_tree; } */ } assert(0); /* Never gets here. */ catch: if(!errno) errno = EILSEQ; date32_to_string(date, &datestr); fprintf(stderr, "%s\n" "%s line %zu: %s.\n", buffer, datestr, line, why); return 0; } /** Dynamic memory allocation for `s` will be zero. */ void sources_(struct sources *const s) { if(!s) return; source_tree_(&s->dates); sourcemap_table_(&s->map); sourcelist_array_(&s->list); } struct sources sources(struct journal *const j) { struct sources s = { sourcelist_array(), sourcemap_table(), source_tree() }; struct journal_iterator it; union date32 k; const char *v; assert(j); { /* Null is the first item for convenience, (TABLE_DEFAULT). */ struct source *nul; if(!(nul = sourcelist_array_new(&s.list))) goto catch; nul->name.a = nul->name.b = nul->desc.a = nul->desc.b = 0; } it = journal_iterator(j); while(journal_next(&it, &k, &v)) if(!scan(k, v, &s)) goto catch; /*fprintf(stderr, "List of sources: %s.\n" "Mapped to indices: %s.\n" "Date-line tree: %s.\n", sourcelist_array_to_string(&s.list), sourcemap_table_to_string(&s.map), source_tree_to_string(&s.dates));*/ goto finally; catch: sources_(&s); finally: return s; } /** Lookup the last source in `range` in sources `s`. They are invalidated on adding a source, (probably fine.) */ const struct source *source_lookup(struct sources *const s, const union line64 x) { struct source_tree_iterator it; assert(s); it = source_tree_less(&s->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 `list[0]` by . */ return s->list.data + (source_tree_has_element(&it) && x.date.u32 == source_tree_key(&it).date.u32 ? *source_tree_value(&it) : 0); }