From 3acf397e6d772778490d7da9f33f3a8f939c9efd Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 14 Dec 2020 22:13:47 -0800 Subject: Imports cleanup and removing FILE* parameter from json --- json.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'json.c') diff --git a/json.c b/json.c index d45e79c..2c9b50a 100644 --- a/json.c +++ b/json.c @@ -1,44 +1,47 @@ /* * json.c - Code for printing JSON output of matches. */ + +#include + #include "types.h" /* * Print a match as JSON */ -static int _json_match(FILE *f, const char *text, match_t *m, int comma, int verbose) +static int _json_match(const char *text, match_t *m, int comma, int verbose) { if (!verbose) { if (m->op->op != VM_REF) { for (match_t *child = m->child; child; child = child->nextsibling) { - comma |= _json_match(f, text, child, comma, verbose); + comma |= _json_match(text, child, comma, verbose); } return comma; } } - if (comma) fprintf(f, ",\n"); + if (comma) printf(",\n"); comma = 0; - fprintf(f, "{\"rule\":\""); + printf("{\"rule\":\""); for (const char *c = m->op->start; c < m->op->end; c++) { switch (*c) { - case '"': fprintf(f, "\\\""); break; - case '\\': fprintf(f, "\\\\"); break; - case '\t': fprintf(f, "\\t"); break; - case '\n': fprintf(f, "↵"); break; - default: fprintf(f, "%c", *c); break; + case '"': printf("\\\""); break; + case '\\': printf("\\\\"); break; + case '\t': printf("\\t"); break; + case '\n': printf("↵"); break; + default: printf("%c", *c); break; } } - fprintf(f, "\",\"start\":%ld,\"end\":%ld,\"children\":[", + printf("\",\"start\":%ld,\"end\":%ld,\"children\":[", m->start - text, m->end - text); for (match_t *child = m->child; child; child = child->nextsibling) { - comma |= _json_match(f, text, child, comma, verbose); + comma |= _json_match(text, child, comma, verbose); } - fprintf(f, "]}"); + printf("]}"); return 1; } -void json_match(FILE *f, const char *text, match_t *m, int verbose) +void json_match(const char *text, match_t *m, int verbose) { - _json_match(f, text, m, 0, verbose); + _json_match(text, m, 0, verbose); } -- cgit v1.2.3