factored out date32_to_string

This commit is contained in:
Neil 2023-04-30 11:06:27 -07:00
parent a07c9c5811
commit bfd8d797f9
2 changed files with 43 additions and 45 deletions

View File

@ -15,14 +15,12 @@ struct source { struct pair name, desc; };
#define ARRAY_HEAD #define ARRAY_HEAD
#include "../src/array.h" #include "../src/array.h"
/* Place table. */ /* Place array. */
struct place { double x, y; }; struct place { struct pair name; double x, y; };
#define TABLE_NAME place #define ARRAY_NAME place
#define TABLE_KEY struct pair #define ARRAY_TYPE struct place
#define TABLE_VALUE struct place #define ARRAY_HEAD
#define TABLE_UINT uint32_t #include "../src/array.h"
#define TABLE_HEAD
#include "../src/table.h"
/* Score array. */ /* Score array. */
struct score { struct score {
@ -88,12 +86,16 @@ struct scan {
struct pairmap_table map; struct pairmap_table map;
struct linemap_tree dates; struct linemap_tree dates;
} sources; } sources;
struct {
struct place_array array;
struct pairmap_table map;
struct linemap_tree dates;
} places;
struct { struct {
struct score_array array; struct score_array array;
struct pairmap_table map; struct pairmap_table map;
struct linemap_tree dates; struct linemap_tree dates;
} scores; } scores;
struct place_table places;
struct glider_tree gliders; struct glider_tree gliders;
struct flight_tree flights; struct flight_tree flights;
struct kjv_tree kjvs; struct kjv_tree kjvs;

View File

@ -23,42 +23,37 @@ static int linemap_compare(const union line64 a, const union line64 b)
#define TREE_BODY #define TREE_BODY
#include "../src/tree.h" #include "../src/tree.h"
/* Looking up places with labels. */ /** `p` to `z`. */
static int place_is_equal(const struct pair a, const struct pair b) static void pair_to_string(const struct pair *const p,
{ return pair_is_equal(a, b); }
static uint32_t place_hash(const struct pair a)
{ return pair_djb2(a); }
#define TABLE_NAME place
#define TABLE_KEY struct pair
#define TABLE_VALUE struct place
#define TABLE_UINT uint32_t
#define TABLE_BODY
#include "../src/table.h"
/* Array of sources. */
static void source_to_string(const struct source *const s,
char (*const z)[12]) { char (*const z)[12]) {
const char *a = s->name.a, *b; const char *a = p->a, *b;
char *y = *z; char *y = *z;
b = s->name.b <= a + 11 ? s->name.b : a + 11; b = p->b <= a + 11 ? p->b : a + 11;
while(a < b) *(y++) = *(a++); while(a < b) *(y++) = *(a++);
*y = '\0'; *y = '\0';
} }
/* Array of sources. */
static void source_to_string(const struct source *const s,
char (*const z)[12]) { pair_to_string(&s->name, z); }
#define ARRAY_NAME source #define ARRAY_NAME source
#define ARRAY_TYPE struct source #define ARRAY_TYPE struct source
#define ARRAY_TO_STRING #define ARRAY_TO_STRING
#define ARRAY_BODY #define ARRAY_BODY
#include "../src/array.h" #include "../src/array.h"
/* Array of places. */
static void place_to_string(const struct place *const p,
char (*const z)[12]) { pair_to_string(&p->name, z); }
#define ARRAY_NAME place
#define ARRAY_TYPE struct place
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"
/* Array of scores. */ /* Array of scores. */
static void score_to_string(const struct score *const s, static void score_to_string(const struct score *const s,
char (*const z)[12]) { char (*const z)[12]) { pair_to_string(&s->name, z); }
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_NAME score
#define ARRAY_TYPE struct score #define ARRAY_TYPE struct score
#define ARRAY_TO_STRING #define ARRAY_TO_STRING
@ -122,6 +117,8 @@ static int scan_day(struct scan *const scan, union date32 date,
assert(scan && date.u32 && buffer); assert(scan && date.u32 && buffer);
YYCURSOR = YYMARKER = yyt1 = buffer; YYCURSOR = YYMARKER = yyt1 = buffer;
date32_to_string(date, &datestr);
/*!re2c /**/ /*!re2c /**/
re2c:define:YYCTYPE = char; re2c:define:YYCTYPE = char;
re2c:yyfill:enable = 0; re2c:yyfill:enable = 0;
@ -162,12 +159,12 @@ static int scan_day(struct scan *const scan, union date32 date,
<place> * { fail = "place unrecognized"; goto catch; } <place> * { fail = "place unrecognized"; goto catch; }
<place> @t0 "(" decimal "," decimal ")" @t1 ws+ @s0 label @s1 / "\n" <place> "(" @t0 decimal "," @t1 decimal ")"
=> skip { ws+ @s0 label @s1 / "\n" => skip {
const struct pair keyword = pair(s0, s1); const struct pair keyword = pair(s0, s1);
const struct pair coords = pair(t0, t1); const double x = strtod(t0, 0), y = strtod(t1, 0); /* Safe? */
fprintf(stderr, "place: %.*s <<%.*s>>\n", fprintf(stderr, "%s: new place: (%.*s)(%f,%f) <<%.*s>>\n",
(int)(t1-t0), t0, (int)(s1-s0), s0); datestr, (int)(s0 - t0), t0 - 1, x, y, (int)(s1-s0), s0);
continue; continue;
} }
<place> @s0 label @s1 / "\n" => skip { <place> @s0 label @s1 / "\n" => skip {
@ -189,7 +186,6 @@ static int scan_day(struct scan *const scan, union date32 date,
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch; case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: *pi = i; break; case TREE_ABSENT: *pi = i; break;
} }
date32_to_string(date, &datestr);
fprintf(stderr, "%s: source <<%.*s>>\n", fprintf(stderr, "%s: source <<%.*s>>\n",
datestr, (int)(s1 - s0), s0); datestr, (int)(s1 - s0), s0);
} continue; } } continue; }
@ -207,7 +203,6 @@ static int scan_day(struct scan *const scan, union date32 date,
*idx = (size_t)(source - scan->sources.array.data); *idx = (size_t)(source - scan->sources.array.data);
source->name.a = s0, source->name.b = s1; source->name.a = s0, source->name.b = s1;
source->desc.a = 0, source->desc.b = 0; 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", fprintf(stderr, "%s: new source <<%.*s>> stored in list at %zu.\n",
datestr, (int)(s1 - s0), s0, *idx); datestr, (int)(s1 - s0), s0, *idx);
goto also_source; goto also_source;
@ -233,7 +228,6 @@ static int scan_day(struct scan *const scan, union date32 date,
case TREE_ERROR: goto catch; case TREE_ERROR: goto catch;
case TREE_ABSENT: *pidx = idx; break; case TREE_ABSENT: *pidx = idx; break;
} }
date32_to_string(date, &datestr);
fprintf(stderr, "%s: score <%.*s>\n", datestr, (int)(s1 - s0), s0); fprintf(stderr, "%s: score <%.*s>\n", datestr, (int)(s1 - s0), s0);
} continue; } } continue; }
/* New score. */ /* New score. */
@ -255,7 +249,6 @@ static int scan_day(struct scan *const scan, union date32 date,
new_score->name.a = 0, new_score->name.b = 0; new_score->name.a = 0, new_score->name.b = 0;
new_score->date.u32 = new_score->last.u32 = 0; new_score->date.u32 = new_score->last.u32 = 0;
new_score->edges = 0, new_score->score = 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", fprintf(stderr, "%s: new score <%.*s> stored in list at %zu.\n",
datestr, (int)(s1 - s0), s0, *idx); datestr, (int)(s1 - s0), s0, *idx);
goto new_score; goto new_score;
@ -302,7 +295,6 @@ static int scan_day(struct scan *const scan, union date32 date,
<glider_launch> * { fail = "glider launch"; goto catch; } <glider_launch> * { fail = "glider launch"; goto catch; }
<glider_launch> ws* @s0 airport @s1 ws* ";" => glider_how { <glider_launch> ws* @s0 airport @s1 ws* ";" => glider_how {
new_glider->launch.a = s0, new_glider->launch.b = s1; new_glider->launch.a = s0, new_glider->launch.b = s1;
date32_to_string(date, &datestr);
fprintf(stderr, "%s: glider <%.*s> at <%.*s>\n", datestr, fprintf(stderr, "%s: glider <%.*s> at <%.*s>\n", datestr,
(int)(new_glider->reg.b - new_glider->reg.a), (int)(new_glider->reg.b - new_glider->reg.a),
new_glider->reg.a, new_glider->reg.a,
@ -378,7 +370,6 @@ static int scan_day(struct scan *const scan, union date32 date,
ws* @t0 airport @t1 ws* ";" => flight_pic { ws* @t0 airport @t1 ws* ";" => flight_pic {
new_flight->launch.a = s0, new_flight->launch.b = s1; new_flight->launch.a = s0, new_flight->launch.b = s1;
new_flight->landing.a = t0, new_flight->landing.b = t1; new_flight->landing.a = t0, new_flight->landing.b = t1;
date32_to_string(date, &datestr);
fprintf(stderr, "%s: flight <%.*s> at <%.*s>\n", datestr, fprintf(stderr, "%s: flight <%.*s> at <%.*s>\n", datestr,
(int)(new_flight->reg.b - new_flight->reg.a), (int)(new_flight->reg.b - new_flight->reg.a),
new_flight->reg.a, new_flight->reg.a,
@ -546,7 +537,6 @@ static int scan_day(struct scan *const scan, union date32 date,
value->verse_end = verse_end; value->verse_end = verse_end;
break; break;
} }
date32_to_string(date, &datestr);
fprintf(stderr, "%s: KJV %s %" PRIu32 ":%" PRIu32, fprintf(stderr, "%s: KJV %s %" PRIu32 ":%" PRIu32,
datestr, kjv_book_string[book], chapter, verse); datestr, kjv_book_string[book], chapter, verse);
if(verse_end) fprintf(stderr, "-%u", verse_end); if(verse_end) fprintf(stderr, "-%u", verse_end);
@ -572,10 +562,15 @@ void scan_(struct scan *const scan) {
kjv_tree_(&scan->kjvs); kjv_tree_(&scan->kjvs);
flight_tree_(&scan->flights); flight_tree_(&scan->flights);
glider_tree_(&scan->gliders); glider_tree_(&scan->gliders);
place_table_(&scan->places);
linemap_tree_(&scan->scores.dates); linemap_tree_(&scan->scores.dates);
pair_map_table_(&scan->scores.map); pair_map_table_(&scan->scores.map);
score_array_(&scan->scores.array); score_array_(&scan->scores.array);
linemap_tree_(&scan->places.dates);
pair_map_table_(&scan->places.map);
place_array_(&scan->places.array);
linemap_tree_(&scan->sources.dates); linemap_tree_(&scan->sources.dates);
pair_map_table_(&scan->sources.map); pair_map_table_(&scan->sources.map);
source_array_(&scan->sources.array); source_array_(&scan->sources.array);
@ -608,6 +603,7 @@ struct scan scan(struct journal *const jrnl) {
/* Scans make trees bulk-loaded; fix to real tree. */ /* Scans make trees bulk-loaded; fix to real tree. */
if(!linemap_tree_bulk_finish(&scan.sources.dates) if(!linemap_tree_bulk_finish(&scan.sources.dates)
|| !linemap_tree_bulk_finish(&scan.places.dates)
|| !linemap_tree_bulk_finish(&scan.scores.dates) || !linemap_tree_bulk_finish(&scan.scores.dates)
|| !glider_tree_bulk_finish(&scan.gliders) || !glider_tree_bulk_finish(&scan.gliders)
|| !flight_tree_bulk_finish(&scan.flights) || !flight_tree_bulk_finish(&scan.flights)