]> 4ch.mooo.com Git - 16.git/blob - src/lib/jsmn/example/maptest2.c
modified: src/lib/jsmn/example/MAPTEST.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 jsoneq(const char *json, jsmntok_t *tok, const char *s) {
13         if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
14                         strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
15                 return 0;
16         }
17         return -1;
18 }
19
20 static int dump(const char *js, jsmntok_t *t, size_t count, int indent) {
21         int i, j, k;
22         if (count == 0) {
23                 return 0;
24         }
25         if (t->type == JSMN_PRIMITIVE) {
26                 printf("%.*s", t->end - t->start, js+t->start);
27                 return 1;
28         } else if (t->type == JSMN_STRING) {
29                 printf("'%.*s'", t->end - t->start, js+t->start);
30                 /*if (jsoneq(js, t, "image") == 0) {
31                         printf("- image: %.*s\n", t->end-t->start,
32                                         js + t->start);
33                 }*/
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("\t");
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("\t");
51                         printf("\t-");
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         FILE *fh = fopen("../../../../data/test.map", "r");
72
73         /* Prepare parser */
74         jsmn_init(&p);
75
76         /* Allocate some tokens as a start */
77         tok = malloc(sizeof(*tok) * tokcount);
78         if (tok == NULL) {
79                 fprintf(stderr, "malloc(): errno=%d\n", errno);
80                 return 3;
81         }
82
83         for (;;) {
84                 /* Read another chunk */
85                 r = fread(buf, 1, sizeof(buf), fh);
86                 if (r < 0) {
87                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
88                         return 1;
89                 }
90                 if (r == 0) {
91                         if (eof_expected != 0) {
92                                 return 0;
93                         } else {
94                                 fprintf(stderr, "fread(): unexpected EOF\n");
95                                 return 2;
96                         }
97                 }
98
99                 js = realloc(js, jslen + r + 1);
100                 if (js == NULL) {
101                         fprintf(stderr, "realloc(): errno=%d\n", errno);
102                         return 3;
103                 }
104                 strncpy(js + jslen, buf, r);
105                 jslen = jslen + r;
106
107 again:
108                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
109                 if (r < 0) {
110                         if (r == JSMN_ERROR_NOMEM) {
111                                 tokcount = tokcount * 2;
112                                 tok = realloc(tok, sizeof(*tok) * tokcount);
113                                 if (tok == NULL) {
114                                         fprintf(stderr, "realloc(): errno=%d\n", errno);
115                                         return 3;
116                                 }
117                                 goto again;
118                         }
119                 } else {
120                         dump(js, tok, p.toknext, 0);
121                         //fprintf(stdout, "[[[[%d]]]]\n", sizeof(tok));
122                         printf("[\n%d\n]", jslen);
123                         eof_expected = 1;
124                 }
125         }
126
127         return 0;
128 }