aboutsummaryrefslogtreecommitdiff
path: root/json.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2020-12-14 22:13:47 -0800
committerBruce Hill <bruce@bruce-hill.com>2020-12-14 22:13:47 -0800
commit3acf397e6d772778490d7da9f33f3a8f939c9efd (patch)
tree28b8ba262bb1b40670f87e55b183acf2f3bda6bd /json.c
parent2fb68fa97f096e5bd0f3ff81d31c3bc61719ae8a (diff)
Imports cleanup and removing FILE* parameter from json
Diffstat (limited to 'json.c')
-rw-r--r--json.c31
1 files changed, 17 insertions, 14 deletions
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 <stdio.h>
+
#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);
}