]> 4ch.mooo.com Git - 16.git/blob - src/lib/00jsmn00/example/maptest2.c
017fbf8520111b28161a7dbdfb252496ae9beed1
[16.git] / src / lib / 00jsmn00 / example / maptest2.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "../jsmn.c"
6 #include "../../../lib/types.h"
7
8 /*
9  * An example of reading JSON from stdin and printing its content to stdout.
10  * The output looks like YAML, but I'm not sure if it's really compatible.
11  */\r
12
13 char *js_sv;
14
15 typedef struct {\r
16         //bitmap_t *data;
17         byte *data;\r
18         word tileHeight;\r
19         word tileWidth;\r
20         unsigned int rows;\r
21         unsigned int cols;\r
22 } tiles_t;\r
23 \r
24 typedef struct {\r
25         byte    *data;\r
26         tiles_t *tiles;\r
27         int width;\r
28         int height;\r
29 } map_t;
30
31 static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
32         if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
33                         strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
34                 return 0;
35         }
36         return -1;
37 }
38
39 static int dump(const char *js, jsmntok_t *t, size_t count, int indent, /*char *js_sv,*/ map_t *map) {
40         int i, j, k;
41         if (count == 0) {
42                 return 0;
43         }
44         /* We may want to do strtol() here to get numeric value */
45         if (t->type == JSMN_PRIMITIVE) {
46                 if(js_sv == "height")
47                 {
48                         map->height = (int)strtol(js+t->start, (char **)js+t->end, 10);
49                         printf("h:[%d]\n", map->height);
50                 }else if(js_sv == "width")
51                 {
52                         map->width = (int)strtol(js+t->start, (char **)js+t->end, 10);
53                         printf("w:[%d]\n", map->width);
54                 }
55                 return 1;
56                 /* We may use strndup() to fetch string value */
57         } else if (t->type == JSMN_STRING) {
58                 //printf("'%.*s'", t->end - t->start, js+t->start);
59                 if (jsoneq(js, t, "height") == 0 && indent==1)
60                 {
61                         js_sv="height";//strdup(js+t->start);//, t->end - t->start);
62                         printf("%s\n", js_sv);
63                 }else if (jsoneq(js, t, "width") == 0 && indent==1)
64                 {
65                         js_sv="width";//strdup(js+t->start);//, t->end - t->start);
66                         printf("%s\n", js_sv);
67                 }else js_sv=NULL;
68                 return 1;
69         } else if (t->type == JSMN_OBJECT) {
70                 //printf("\n");
71                 j = 0;
72                 for (i = 0; i < t->size; i++) {
73                         //for (k = 0; k < indent; k++) printf("\t");
74                         j += dump(js, t+1+j, count-j, indent+1, map);
75                         //printf(": ");
76                         j += dump(js, t+1+j, count-j, indent+1, map);
77                         //printf("\n");
78                 }
79                 return j+1;
80         } else if (t->type == JSMN_ARRAY) {
81                 j = 0;
82                 //printf("==\n");
83                 for (i = 0; i < t->size; i++) {
84                         //for (k = 0; k < indent-1; k++) printf("\t");
85                         //printf("\t-");
86                         j += dump(js, t+1+j, count-j, indent+1, map);
87                         //printf("==\n");
88                 }
89                 return j+1;
90         }
91         return 0;
92 }
93
94 int loadmap(char *mn, map_t *map)
95 {
96         int r;
97         int eof_expected = 0;
98         char *js = NULL;
99         size_t jslen = 0;
100         char buf[BUFSIZ];
101
102         jsmn_parser p;
103         jsmntok_t *tok;
104         size_t tokcount = 2;
105
106         FILE *fh = fopen(mn, "r");
107
108         /* Prepare parser */
109         jsmn_init(&p);
110
111         /* Allocate some tokens as a start */
112         tok = malloc(sizeof(*tok) * tokcount);
113         if (tok == NULL) {
114                 fprintf(stderr, "malloc(): errno=%d\n", errno);
115                 return 3;
116         }
117
118         for (;;) {
119                 /* Read another chunk */
120                 r = fread(buf, 1, sizeof(buf), fh);
121                 if (r < 0) {
122                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
123                         return 1;
124                 }
125                 if (r == 0) {
126                         if (eof_expected != 0) {
127                                 return 0;
128                         } else {
129                                 fprintf(stderr, "fread(): unexpected EOF\n");
130                                 return 2;
131                         }
132                 }
133
134                 js = realloc(js, jslen + r + 1);
135                 if (js == NULL) {
136                         fprintf(stderr, "realloc(): errno=%d\n", errno);
137                         return 3;
138                 }
139                 strncpy(js + jslen, buf, r);
140                 jslen = jslen + r;
141
142 again:
143                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
144                 if (r < 0) {
145                         if (r == JSMN_ERROR_NOMEM) {
146                                 tokcount = tokcount * 2;
147                                 tok = realloc(tok, sizeof(*tok) * tokcount);
148                                 if (tok == NULL) {
149                                         fprintf(stderr, "realloc(): errno=%d\n", errno);
150                                         return 3;
151                                 }
152                                 goto again;
153                         }
154                 } else {
155                         dump(js, tok, p.toknext, 0, map);
156                         //fprintf(stdout, "[[[[%d]]]]\n", sizeof(tok));
157                         //printf("[\n%d\n]", jslen);
158                         eof_expected = 1;
159                 }
160         }
161
162         return 0;
163 }
164
165 int main()
166 {
167         map_t map;
168         loadmap("../../../../data/test.map", &map);
169         return 0;
170 }