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, int q/*, word w*/) {
17 /* We may want to do strtol() here to get numeric value */
18 if (t->type == JSMN_PRIMITIVE) {
22 //bgdata[q] = (byte)strtol(js+t->start, (char **)js+t->end, 10);
23 if(strtol(js+t->start, (char **)js+t->end, 10)==0){ fprintf(stderr, "FACK! %d\n", errno); exit(-1); }
24 map->tiles->data->data[q] = (byte)strtol(js+t->start, (char **)js+t->end, 10);
25 printf("%d[%d]", q, map->tiles->data->data[q]);
26 //printf("%d[%d]", q, bgdata[q]);
31 map->height = (int)strtol(js+t->start, (char **)js+t->end, 10);
32 printf("h:[%d]\n", map->height);
33 }else if(js_sv == "width")
35 map->width = (int)strtol(js+t->start, (char **)js+t->end, 10);
36 printf("w:[%d]\n", map->width);
39 /* We may use strndup() to fetch string value */
40 } else if (t->type == JSMN_STRING) {
41 //printf("'%.*s'", t->end - t->start, js+t->start);
43 if(jsoneq(js, t, "data") == 0 )
45 js_sv="data";//strdup(js+t->start);//, t->end - t->start);
46 //printf("%s\n", js_sv);
49 if (jsoneq(js, t, "height") == 0 && indent==1)
51 js_sv="height";//strdup(js+t->start);//, t->end - t->start);
52 //printf("%s\n", js_sv);
53 }else if (jsoneq(js, t, "width") == 0 && indent==1)
55 js_sv="width";//strdup(js+t->start);//, t->end - t->start);
56 //printf("%s\n", js_sv);
59 } else if (t->type == JSMN_OBJECT) {
62 for (i = 0; i < t->size; i++) {
63 //for (k = 0; k < indent; k++) printf("\t");
64 j += dump(js, t+1+j, count-j, indent+1, map, i/*, w*/);
66 j += dump(js, t+1+j, count-j, indent+1, map, i/*, w*/);
70 } else if (t->type == JSMN_ARRAY) {
73 for (i = 0; i < t->size; i++) {
74 //if(bgdata==NULL) bgdata=malloc(sizeof(char)*t->size);
75 //for (k = 0; k < indent-1; k++) printf("\t");
77 j += dump(js, t+1+j, count-j, indent+1, map, i/*, w*/);
85 static int loadmap(char *mn, map_t *map/*, word w*/)
97 FILE *fh = fopen(mn, "r");
102 /* Allocate some tokens as a start */
103 tok = malloc(sizeof(*tok) * tokcount);
105 fprintf(stderr, "malloc(): errno=%d\n", errno);
110 /* Read another chunk */
111 r = fread(buf, 1, sizeof(buf), fh);
113 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
117 if (eof_expected != 0) {
120 fprintf(stderr, "fread(): unexpected EOF\n");
125 js = realloc(js, jslen + r + 1);
127 fprintf(stderr, "realloc(): errno = %d\n", errno);
130 strncpy(js + jslen, buf, r);
134 r = jsmn_parse(&p, js, jslen, tok, tokcount);
136 if (r == JSMN_ERROR_NOMEM) {
137 tokcount = tokcount * 2;
138 tok = realloc(tok, sizeof(*tok) * tokcount);
140 fprintf(stderr, "realloc(): errno=%d\n", errno);
146 dump(js, tok, p.toknext, 0, map, 0/*, w*/);
147 //fprintf(stdout, "[[[[%d]]]]\n", sizeof(tok));
148 //printf("[\n%d\n]", jslen);
163 loadmap("../../../../data/test.map", &map);