/** @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 "../src/hash.h" /* djb2 */ #include "../src/pair.h" #include #include #include #include /* fixme: ...wait, why would you copy? modify the table to use substring. */ /* This is a lookup table for source strings ("2000glider") to substring the first description ("Glider pilot log book"). */ static int lookup_is_equal(const char *const x, const char *const y) { return !strcmp(x, y); } static uint32_t lookup_hash(const char *const x) { return djb2(x); } static void lookup_to_string(const char *x, const struct pair desc, char (*const a)[12]) { (void)desc; sprintf(*a, "%.11s", x); } static struct pair lookup_default = { 0, 0 }; #define TABLE_NAME lookup #define TABLE_KEY char * #define TABLE_UINT uint32_t #define TABLE_VALUE struct pair #define TABLE_DEFAULT lookup_default #define TABLE_TO_STRING #include "../src/table.h" static void source_to_string(const union line64 line, char *const*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 char * #define TREE_COMPARE #define TREE_TO_STRING #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, *t0, *t1; 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 { struct pair p = pair(s0, s1); const char *label; printf("extracted <%.*s>\n", (int)(s1 - s0), s0); continue; } /* This is lazy and will pickup trailing spaces. */ @s0 keyword @s1 ":" [^\x00\n]+ / "\n" => skip { printf("new keyword <%.*s>\n", (int)(s1 - s0), s0); continue; } */ } 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, will be false. */ void sources_(struct sources *const s) { if(!s) return; source_tree_(&s->_); } struct sources sources(struct journal *const j) { struct sources s; struct journal_iterator it; union date32 k; const char *v; assert(j); s._ = source_tree(); it = journal_begin(j); while(journal_next(&it, &k, &v)) if(!scan(k, v, &s)) goto catch; goto finally; catch: sources_(&s); finally: //char_array_(&temp); /* Erase the temporary buffer. */ return s; }