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 //this function is quite messy ^^; sorry! it is a quick and dirty fix~
12 static int dump(const char *js, jsmntok_t *t, size_t count, int indent, /*char *js_sv,*/ map_t *map) {
17 /* We may want to do strtol() here to get numeric value */
18 if (t->type == JSMN_PRIMITIVE) {
21 map->height = (int)strtol(js+t->start, (char **)js+t->end, 10);
22 //printf("h:[%d]\n", map->height);
23 }else if(js_sv == "width")
25 map->width = (int)strtol(js+t->start, (char **)js+t->end, 10);
26 //printf("w:[%d]\n", map->width);
29 /* We may use strndup() to fetch string value */
30 } else if (t->type == JSMN_STRING) {
31 //printf("'%.*s'", t->end - t->start, js+t->start);
32 if (jsoneq(js, t, "height") == 0 && indent==1)
34 js_sv="height";//strdup(js+t->start);//, t->end - t->start);
35 //printf("%s\n", js_sv);
36 }else if (jsoneq(js, t, "width") == 0 && indent==1)
38 js_sv="width";//strdup(js+t->start);//, t->end - t->start);
39 //printf("%s\n", js_sv);
42 } else if (t->type == JSMN_OBJECT) {
45 for (i = 0; i < t->size; i++) {
46 //for (k = 0; k < indent; k++) printf("\t");
47 j += dump(js, t+1+j, count-j, indent+1, map);
49 j += dump(js, t+1+j, count-j, indent+1, map);
53 } else if (t->type == JSMN_ARRAY) {
56 for (i = 0; i < t->size; i++) {
57 //for (k = 0; k < indent-1; k++) printf("\t");
59 j += dump(js, t+1+j, count-j, indent+1, map);
67 int loadmap(char *mn, map_t *map)
79 FILE *fh = fopen(mn, "r");
84 /* Allocate some tokens as a start */
85 tok = malloc(sizeof(*tok) * tokcount);
87 fprintf(stderr, "malloc(): errno=%d\n", errno);
92 /* Read another chunk */
93 r = fread(buf, 1, sizeof(buf), fh);
95 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
99 if (eof_expected != 0) {
102 fprintf(stderr, "fread(): unexpected EOF\n");
107 js = realloc(js, jslen + r + 1);
109 fprintf(stderr, "realloc(): errno=%d\n", errno);
112 strncpy(js + jslen, buf, r);
116 r = jsmn_parse(&p, js, jslen, tok, tokcount);
118 if (r == JSMN_ERROR_NOMEM) {
119 tokcount = tokcount * 2;
120 tok = realloc(tok, sizeof(*tok) * tokcount);
122 fprintf(stderr, "realloc(): errno=%d\n", errno);
128 dump(js, tok, p.toknext, 0, map);
129 //fprintf(stdout, "[[[[%d]]]]\n", sizeof(tok));
130 //printf("[\n%d\n]", jslen);
141 loadmap("../../../../data/test.map", &map);