From 433ce0f6795a4db89921b26274dd0b18fcccbb21 Mon Sep 17 00:00:00 2001 From: gramanas Date: Tue, 6 Sep 2022 10:42:48 +0300 Subject: Small parser refactor --- src/parser.c | 25 +++++++++++++++++-------- src/types.c | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/parser.c b/src/parser.c index 9206f42..e90b200 100644 --- a/src/parser.c +++ b/src/parser.c @@ -217,12 +217,11 @@ next_escaped_line(FILE *f, char * line, int * lino, char * error) { return 0; } -recipe * -parse(char * path, const char * prev) +FILE * +file_from_path(const char * path) { - fdebug("Parsing: %s\n", path); - FILE *f; + if (!strcmp(path, "-")) { f = stdin; } else { @@ -234,10 +233,20 @@ parse(char * path, const char * prev) strcpy(tmp, path); } f = fopen(tmp, "r"); - if (!f) { - fprintf(stderr, "Can't open %s\n", path); - return NULL; - } + } + + return f; +} + +recipe * +parse(char * path, const char * prev) +{ + fdebug("Parsing: %s\n", path); + + FILE *f = file_from_path(path); + if (!f) { + fprintf(stderr, "Can't open %s\n", path); + return NULL; } recipe * r = new_recipe(); diff --git a/src/types.c b/src/types.c index 9fbb59b..5ce0203 100644 --- a/src/types.c +++ b/src/types.c @@ -163,7 +163,7 @@ void tojson(recipe * r) { printf("{\"filename\":\"%s\",", r->filename); - printf("\"dirname\":\"%s\",", r->path); + printf("\"path\":\"%s\",", r->path); printf("\"title\":\"%s\",", r->title); printf("\"n\":\"%d\",", r->n); printf("\"sha1_short\":\"%.*s\",", 8, r->sha1); -- cgit v1.2.3