]> pd.if.org Git - json/blob - filter.c
initial commit
[json] / filter.c
1 #include <stdio.h>
2
3 #include "scan.h"
4 #include "json.h"
5 #include "filter.h"
6
7 void *jsonp_alloc(void *(*mallocProc)(size_t));
8 void jsonp_parse(void *parser, int token, struct token tokentype, struct parser *);
9 void jsonp_free(void *p, void (*freeProc)(void*));
10 void jsonp_trace(FILE *out, char *prompt);
11
12 int main(void) {
13         char line[1024];
14         char *in = 0;
15         struct token t = {0};
16         struct parser state = {0};
17         void *jp = 0;
18         int ttype;
19
20         jp = jsonp_alloc(malloc);
21
22         line[0] = 0;
23
24         //jsonp_trace(stdout, "trace: ");
25         while (fgets(line, sizeof line, stdin)) {
26                 state.line = line;
27                 for (in = line; *in; in++) {
28                         t.ch = *in;
29                         if (*in == '\n') {
30                                 state.line++;
31                                 state.pos = 0;
32                         }
33                         state.pos++;
34                         while (t.ch) {
35                                 ttype = scan_json_ch(*in, &t);
36                                 if (t.error) {
37                                         printf("scanning error\n");
38                                         exit(EXIT_FAILURE);
39                                 }
40                                 if (ttype) {
41                                         t.str[t.len] = 0;
42                                         //printf("feeding %d\n", ttype);
43                                         jsonp_parse(jp, ttype, t, &state);
44                                         t.type = 0;
45                                 }
46                         }
47                 }
48         }
49         ttype = scan_json_ch(0, &t);
50         if (ttype) {
51                 t.str[t.len] = 0;
52                 jsonp_parse(jp, ttype, t, &state);
53                 t.type = 0;
54         }
55         jsonp_parse(jp, 0, t, &state);
56         jsonp_free(jp, free);
57         return 0;
58 }