]> 4ch.mooo.com Git - 16.git/blob - src/lib/jsmn/example/maptest2.c
modified: data/test0.map
[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         int r;
50         int eof_expected = 0;
51         char *js = NULL;
52         size_t jslen = 0;
53         char buf[BUFSIZ];
54
55         jsmn_parser p;
56         jsmntok_t *tok;
57         size_t tokcount = 2;
58
59         FILE *fh = fopen("../../../../data/test.map", "r");
60
61         /* Prepare parser */
62         jsmn_init(&p);
63
64         /* Allocate some tokens as a start */
65         tok = malloc(sizeof(*tok) * tokcount);
66         if (tok == NULL) {
67                 fprintf(stderr, "malloc(): errno=%d\n", errno);
68                 return 3;
69         }
70
71         for (;;) {
72                 /* Read another chunk */
73                 r = fread(buf, 1, sizeof(buf), fh);
74                 if (r < 0) {
75                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
76                         return 1;
77                 }
78                 if (r == 0) {
79                         if (eof_expected != 0) {
80                                 return 0;
81                         } else {
82                                 fprintf(stderr, "fread(): unexpected EOF\n");
83                                 return 2;
84                         }
85                 }
86
87                 js = realloc(js, jslen + r + 1);
88                 if (js == NULL) {
89                         fprintf(stderr, "realloc(): errno=%d\n", errno);
90                         return 3;
91                 }
92                 strncpy(js + jslen, buf, r);
93                 jslen = jslen + r;
94
95 again:
96                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
97                 if (r < 0) {
98                         if (r == JSMN_ERROR_NOMEM) {
99                                 tokcount = tokcount * 2;
100                                 tok = realloc(tok, sizeof(*tok) * tokcount);
101                                 if (tok == NULL) {
102                                         fprintf(stderr, "realloc(): errno=%d\n", errno);
103                                         return 3;
104                                 }
105                                 goto again;
106                         }
107                 } else {
108                         dump(js, tok, p.toknext, 0);
109                         printf("[\n%d\n]", jslen);
110                         eof_expected = 1;
111                 }
112         }
113
114         return 0;
115 }