1 #include "src/lib/mapread.h"
3 static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
4 if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
5 strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
11 static int dump(const char *js, jsmntok_t *t, size_t count, int indent, /*char *js_sv,*/ map_t *map) {
16 /* We may want to do strtol() here to get numeric value */
17 if (t->type == JSMN_PRIMITIVE) {
20 map->height = (int)strtol(js+t->start, (char **)js+t->end, 10);
21 printf("h:[%d]\n", map->height);
22 }else if(js_sv == "width")
24 map->width = (int)strtol(js+t->start, (char **)js+t->end, 10);
25 printf("w:[%d]\n", map->width);
28 /* We may use strndup() to fetch string value */
29 } else if (t->type == JSMN_STRING) {
30 //printf("'%.*s'", t->end - t->start, js+t->start);
31 if (jsoneq(js, t, "height") == 0 && indent==1)
33 js_sv="height";//strdup(js+t->start);//, t->end - t->start);
34 printf("%s\n", js_sv);
35 }else if (jsoneq(js, t, "width") == 0 && indent==1)
37 js_sv="width";//strdup(js+t->start);//, t->end - t->start);
38 printf("%s\n", js_sv);
41 } else if (t->type == JSMN_OBJECT) {
44 for (i = 0; i < t->size; i++) {
45 //for (k = 0; k < indent; k++) printf("\t");
46 j += dump(js, t+1+j, count-j, indent+1, map);
48 j += dump(js, t+1+j, count-j, indent+1, map);
52 } else if (t->type == JSMN_ARRAY) {
55 for (i = 0; i < t->size; i++) {
56 //for (k = 0; k < indent-1; k++) printf("\t");
58 j += dump(js, t+1+j, count-j, indent+1, map);
66 int loadmap(char *mn, map_t *map)
78 FILE *fh = fopen(mn, "r");
83 /* Allocate some tokens as a start */
84 tok = malloc(sizeof(*tok) * tokcount);
86 fprintf(stderr, "malloc(): errno=%d\n", errno);
91 /* Read another chunk */
92 r = fread(buf, 1, sizeof(buf), fh);
94 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
98 if (eof_expected != 0) {
101 fprintf(stderr, "fread(): unexpected EOF\n");
106 js = realloc(js, jslen + r + 1);
108 fprintf(stderr, "realloc(): errno=%d\n", errno);
111 strncpy(js + jslen, buf, r);
115 r = jsmn_parse(&p, js, jslen, tok, tokcount);
117 if (r == JSMN_ERROR_NOMEM) {
118 tokcount = tokcount * 2;
119 tok = realloc(tok, sizeof(*tok) * tokcount);
121 fprintf(stderr, "realloc(): errno=%d\n", errno);
127 dump(js, tok, p.toknext, 0, map);
128 //fprintf(stdout, "[[[[%d]]]]\n", sizeof(tok));
129 //printf("[\n%d\n]", jslen);
140 loadmap("../../../../data/test.map", &map);