]> 4ch.mooo.com Git - 16.git/blob - src/lib/jsmn/example/maptest2.c
new file: 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         if (t->type == JSMN_PRIMITIVE) {
18                 printf("%.*s", t->end - t->start, js+t->start);
19                 return 1;
20         } else if (t->type == JSMN_STRING) {
21                 printf("'%.*s'", t->end - t->start, js+t->start);
22                 return 1;
23         } else if (t->type == JSMN_OBJECT) {
24                 printf("\n");
25                 j = 0;
26                 for (i = 0; i < t->size; i++) {
27                         for (k = 0; k < indent; k++) printf("  ");
28                         j += dump(js, t+1+j, count-j, indent+1);
29                         printf(": ");
30                         j += dump(js, t+1+j, count-j, indent+1);
31                         printf("\n");
32                 }
33                 return j+1;
34         } else if (t->type == JSMN_ARRAY) {
35                 j = 0;
36                 printf("\n");
37                 for (i = 0; i < t->size; i++) {
38                         for (k = 0; k < indent-1; k++) printf("  ");
39                         printf("   - ");
40                         j += dump(js, t+1+j, count-j, indent+1);
41                         printf("\n");
42                 }
43                 return j+1;
44         }
45         return 0;
46 }
47
48 int main() {
49         FILE *fh = fopen("../../../../data/test.map", "r");
50         int r;
51         int eof_expected = 0;
52         char *js = NULL;
53         size_t jslen = 0;
54         char buf[BUFSIZ];
55
56         jsmn_parser p;
57         jsmntok_t *tok;
58         size_t tokcount = 2;
59
60         /* Prepare parser */
61         jsmn_init(&p);
62
63         /* Allocate some tokens as a start */
64         tok = malloc(sizeof(*tok) * tokcount);
65         if (tok == NULL) {
66                 fprintf(stderr, "malloc(): errno=%d\n", errno);
67                 return 3;
68         }
69
70         for (;;) {
71                 /* Read another chunk */
72                 r = fread(buf, 1, sizeof(buf), fh);
73                 if (r < 0) {
74                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
75                         return 1;
76                 }
77                 if (r == 0) {
78                         if (eof_expected != 0) {
79                                 return 0;
80                         } else {
81                                 fprintf(stderr, "fread(): unexpected EOF\n");
82                                 return 2;
83                         }
84                 }
85
86                 js = realloc(js, jslen + r + 1);
87                 if (js == NULL) {
88                         fprintf(stderr, "realloc(): errno=%d\n", errno);
89                         return 3;
90                 }
91                 strncpy(js + jslen, buf, r);
92                 jslen = jslen + r;
93
94 again:
95                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
96                 if (r < 0) {
97                         if (r == JSMN_ERROR_NOMEM) {
98                                 tokcount = tokcount * 2;
99                                 tok = realloc(tok, sizeof(*tok) * tokcount);
100                                 if (tok == NULL) {
101                                         fprintf(stderr, "realloc(): errno=%d\n", errno);
102                                         return 3;
103                                 }
104                                 goto again;
105                         }
106                 } else {
107                         dump(js, tok, p.toknext, 0);
108                         eof_expected = 1;
109                 }
110         }
111
112         return 0;
113 }