From 28ac6d83313d4b9f3298373e69fdd0c1becbae0d Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Tue, 21 Dec 2021 09:58:33 -0600 Subject: [PATCH] Use an input file --- cyoa.leg | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/cyoa.leg b/cyoa.leg index 2d94070..8dafaec 100644 --- a/cyoa.leg +++ b/cyoa.leg @@ -5,6 +5,13 @@ #include #include +FILE* yyin = stdin; + +#define YY_INPUT(buf, result, max_size) { \ + int yyc = getc(yyin); \ + result = (EOF == yyc) ? 0 : (*(buf) = yyc, 1); \ + } + #define STATCHECK_GT 1 #define STATCHECK_LT 2 @@ -217,14 +224,21 @@ void print_page(Page *page) { } } -int main() { - if (!yyparse()) { - printf("Parsing Error!\n"); +std::vector CyoaParse(FILE *file) { + yyin = file; + yyparse(); + yyin = stdin; + return pages; +} + +int main(int argc, char **argv) { + if (argc < 2) { return 1; - } else { - for (Page* page : pages) { - print_page(page); - } + } + FILE* fin = fopen(argv[1], "r"); + auto mypages = CyoaParse(fin); + for (Page* page : pages) { + print_page(page); } return 0; }