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