From 21729f867980c20a8b9c264ed334ab0a48f93e4e Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 1 Feb 2023 23:18:20 -0800 Subject: [PATCH] compiled flighthours --- Makefile | 6 +-- src/flight.h | 43 +++++++++------- src/flight.re.c | 113 +++++++++++++++++++++++++++++++++++++++++++ src/flighthours.c | 43 ++++++++++++++++ src/scan_flight.h | 2 +- src/scan_flight.re.c | 2 +- src/scan_kjv.h | 1 - src/scan_kjv.re.c | 13 +++-- 8 files changed, 193 insertions(+), 30 deletions(-) create mode 100644 src/flight.re.c create mode 100644 src/flighthours.c delete mode 100644 src/scan_kjv.h diff --git a/Makefile b/Makefile index db6c063..1e2b6b1 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ else CF += -g endif -projects := bin/test-text bin/test-kjv bin/test-journal bin/kjv bin/flight +projects := bin/test-text bin/test-journal bin/test-kjv bin/kjv bin/flight #docs := $(patsubst test/test_%.c, doc/%.md, $(wildcard test/test_*.c)) default: $(projects) @@ -39,7 +39,7 @@ bin/test-text: build/text.o build/test_text.o bin/test-journal: build/text.o build/journal.o build/test_journal.o bin/test-kjv: build/text.o build/kjv.o build/test_kjv.o bin/kjv: build/text.o build/journal.o build/kjv.o build/scan_kjv.o -bin/flight: build/text.o build/journal.o build/flight.o build/driver_flighthours.o +bin/flight: build/text.o build/journal.o build/flight.o build/flighthours.o bin/%: @echo "\033[1;36mlinking $@\033[0m" @@ -69,7 +69,7 @@ build/%.c: src/%.re.c # # https://github.com/neil-edelman/cdoc documentation # -cdoc -o $@ $< -.SECONDARY: build/kjv.c build/journal.c build/scan_kjv.c +.SECONDARY: build/kjv.c build/journal.c build/scan_kjv.c build/flight.c .PHONY: clean release test test: $(projects) diff --git a/src/flight.h b/src/flight.h index 534541f..70bfd34 100644 --- a/src/flight.h +++ b/src/flight.h @@ -1,4 +1,5 @@ -#ifndef OMIT_BASE /* */ +#endif /* base --> */ -#ifndef OMIT_INTERNAL /* */ +#endif /* generic --> */ -#ifndef OMIT_PROTO /* */ +const char *flights_to_string(const struct flights *); +#endif /* proto --> */ + +#ifdef BASE +#undef BASE +#endif +#ifdef GENERIC +#undef GENERIC +#endif +#ifdef PROTO +#undef PROTO +#endif diff --git a/src/flight.re.c b/src/flight.re.c new file mode 100644 index 0000000..9362619 --- /dev/null +++ b/src/flight.re.c @@ -0,0 +1,113 @@ +/** @license 2023 Neil Edelman, distributed under the terms of the + [MIT License](https://opensource.org/licenses/MIT). + @std C11 */ +#define BASE +#include "../src/flight.h" /* base */ +#include "../src/journal.h" +#include +#include +#include + + +static void flight_to_string(const union line64 line, const struct flight *u, + char (*const a)[12]) { (void)u; 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 +#include "../src/tree.h" + + +#define PROTO +#include "../src/flight.h" /* proto */ + + +/*!conditions:re2c*/ + +static int scan(union date32 date, const char *const buffer, + struct flights *const f) { + const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *yyt3, *s0, *s1, *t0, *t1; + enum flight_type type = Glider; + enum YYCONDTYPE condition = yycline; + size_t line = 1; + char datestr[12] = {0}; + const char *why = "unexpected"; + assert(buffer && f); + 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); + natural = [1-9][0-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 + + /* Except these two. */ + "[glider]" :=> glider + "[flight]" :=> flight + + /* "M" - Motor Car Tow + "W" - Winch + "A" - Aero Tow */ + /* type; registration; launch -- landing; pic; sic; + single engine day dual; pilot; instrument simulated; actual; remarks */ + * { why = "default unrecognized"; goto catch; } + */ } + 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 `f` will be zero, will + be false. */ +void flights_(struct flights *const f) { + if(!f) return; + flight_tree_(&f->_); +} + +struct flights flights(/*const*/ struct journal *const j) { + struct flights f; + struct journal_iterator it; + union date32 k; + const char *v; + assert(j); + f._ = flight_tree(); + it = journal_begin(j); + while(journal_next(&it, &k, &v)) if(!scan(k, v, &f)) goto catch; + goto finally; +catch: + flights_(&f); +finally: + return f; +} + +struct flight *flights_add(struct flights *const flights, + const union line64 line) { + struct flight *flight; + assert(flights); + /* We assert that the flights are unique. */ + switch(flight_tree_try(&flights->_, line, &flight)) { + case TREE_PRESENT: errno = EDOM; + case TREE_ERROR: return 0; + case TREE_ABSENT: return flight; + } +} diff --git a/src/flighthours.c b/src/flighthours.c new file mode 100644 index 0000000..9512857 --- /dev/null +++ b/src/flighthours.c @@ -0,0 +1,43 @@ +/** @license 2023 Neil Edelman, distributed under the terms of the + [MIT License](https://opensource.org/licenses/MIT). + + Date _vs_ hours flown. */ + +#include "journal.h" +#include "flight.h" +#include +#include +#include +#include + +int main(void) { + int success = EXIT_SUCCESS; + struct journal j = journal(); + union date32 k; + struct flights f = flights(&j); + if(!journal_is_valid(&j)) goto catch; + fprintf(stderr, "Journal: %s.\n", journal_to_string(&j)); + printf("set term postscript eps enhanced\n" + "set output \"flighthours.eps\"\n" + "$Data <text, &kj)) goto catch; + "# date\tverse\tset\tcumulative / %zu\n", bible.words.total); + it = journal_begin(&j); + while(journal_next(&it, &k, &v)) if(!scan(k, v, &bible)) goto catch; printf("EOD\n" "set monochrome\n" "set xdata time\n" @@ -211,7 +210,7 @@ int main(void) { "#set style fill solid 0.1 #pattern 5 (better, but restarts)\n" "plot $Data using 1:($3)*100/%zu with fillsteps lw 2 title \"set\", \\\n" "$Data using 1:($4)*100/%zu with steps lw 1 title \"cumulative\"\n", - kj.words.total, kj.words.total); + bible.words.total, bible.words.total); goto finally; catch: success = EXIT_FAILURE;