]> 4ch.mooo.com Git - 16.git/blob - src/lib/jsmn/example/maptest2.c
modified: src/lib/jsmn/example/MAPTEST2.EXE
[16.git] / src / lib / jsmn / example / maptest2.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "../jsmn.c"
6
7 /*
8  * An example of reading JSON from stdin and printing its content to stdout.
9  * The output looks like YAML, but I'm not sure if it's really compatible.
10  */
11
12 static int dump(const char *js, jsmntok_t *t, size_t count, int indent) {
13         int i, j, k;
14         if (count == 0) {
15                 return 0;
16         }
17         //what the fuck is going on here?
18         if (t->type == JSMN_PRIMITIVE) {
19                 printf("%.*s", t->end - t->start, js+t->start);
20                 printf("%s\n", js+t->start);
21                 return 1;
22         } else if (t->type == JSMN_STRING) {
23                 printf("'%.*s'", t->end - t->start, js+t->start);
24                 return 1;
25         } else if (t->type == JSMN_OBJECT) {
26                 printf("\n");
27                 j = 0;
28                 for (i = 0; i < t->size; i++) {
29                         for (k = 0; k < indent; k++) printf("  ");
30                         j += dump(js, t+1+j, count-j, indent+1);
31                         printf(": ");
32                         j += dump(js, t+1+j, count-j, indent+1);
33                         printf("\n");
34                 }
35                 return j+1;
36         } else if (t->type == JSMN_ARRAY) {
37                 j = 0;
38                 printf("\n");
39                 for (i = 0; i < t->size; i++) {
40                         for (k = 0; k < indent-1; k++) printf("  ");
41                         printf("   - ");
42                         j += dump(js, t+1+j, count-j, indent+1);
43                         printf("\n");
44                 }
45                 return j+1;
46         }
47         return 0;
48 }
49
50 static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
51         if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
52                         strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
53                 return 0;
54         }
55         return -1;
56 }
57
58 int main() {
59         FILE *fh = fopen("../../../../data/test.map", "r");
60         int r;
61         int eof_expected = 0;
62         char *js = NULL;
63         size_t jslen = 0;
64         char buf[BUFSIZ];
65
66         jsmn_parser p;
67         jsmntok_t *tok;
68         size_t tokcount = 2;
69
70         /* Prepare parser */
71         jsmn_init(&p);
72
73         /* Allocate some tokens as a start */
74         tok = malloc(sizeof(*tok) * tokcount);
75         if (tok == NULL) {
76                 fprintf(stderr, "malloc(): errno=%d\n", errno);
77                 return 3;
78         }
79
80         for (;;) {
81                 /* Read another chunk */
82                 r = fread(buf, 1, sizeof(buf), fh);
83                 if (r < 0) {
84                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
85                         return 1;
86                 }
87                 if (r == 0) {
88                         if (eof_expected != 0) {
89                                 return 0;
90                         } else {
91                                 fprintf(stderr, "fread(): unexpected EOF\n");
92                                 return 2;
93                         }
94                 }
95
96                 js = realloc(js, jslen + r + 1);
97                 if (js == NULL) {
98                         fprintf(stderr, "realloc(): errno=%d\n", errno);
99                         return 3;
100                 }
101                 strncpy(js + jslen, buf, r);
102                 jslen = jslen + r;
103
104 again:
105                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
106                 if (r < 0) {
107                         if (r == JSMN_ERROR_NOMEM) {
108                                 tokcount = tokcount * 2;
109                                 tok = realloc(tok, sizeof(*tok) * tokcount);
110                                 if (tok == NULL) {
111                                         fprintf(stderr, "realloc(): errno=%d\n", errno);
112                                         return 3;
113                                 }
114                                 goto again;
115                         }
116                 } else {
117                         dump(js, tok, p.toknext, 0);
118                         eof_expected = 1;
119                 }
120         }
121
122         return 0;
123 }