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) {
17 /* We may want to do strtol() here to get numeric value */
18 if (t->type == JSMN_PRIMITIVE) {
21 //bgdata[q] = (byte)strtol(js+t->start, (char **)js+t->end, 10);
22 if(strtol(js+t->start, (char **)js+t->end, 10)==0){ fprintf(stderr, "FACK! %d\n", errno); exit(-1); }
23 map->tiles->data->data[q] = (byte)strtol(js+t->start, (char **)js+t->end, 10);
24 printf("%d[%d]", q, map->tiles->data->data[q]);
25 //printf("%d[%d]", q, bgdata[q]);
26 }else if(js_sv == "height")
28 map->height = (int)strtol(js+t->start, (char **)js+t->end, 10);
29 //printf("h:[%d]\n", map->height);
30 }else if(js_sv == "width")
32 map->width = (int)strtol(js+t->start, (char **)js+t->end, 10);
33 //printf("w:[%d]\n", map->width);
36 /* We may use strndup() to fetch string value */
37 } else if (t->type == JSMN_STRING) {
38 //printf("'%.*s'", t->end - t->start, js+t->start);
39 if (jsoneq(js, t, "data") == 0)
41 js_sv="data";//strdup(js+t->start);//, t->end - t->start);
42 //printf("%s\n", js_sv);
43 }else if (jsoneq(js, t, "height") == 0 && indent==1)
45 js_sv="height";//strdup(js+t->start);//, t->end - t->start);
46 //printf("%s\n", js_sv);
47 }else if (jsoneq(js, t, "width") == 0 && indent==1)
49 js_sv="width";//strdup(js+t->start);//, t->end - t->start);
50 //printf("%s\n", js_sv);
53 } else if (t->type == JSMN_OBJECT) {
56 for (i = 0; i < t->size; i++) {
57 //for (k = 0; k < indent; k++) printf("\t");
58 j += dump(js, t+1+j, count-j, indent+1, map, i);
60 j += dump(js, t+1+j, count-j, indent+1, map, i);
64 } else if (t->type == JSMN_ARRAY) {
67 for (i = 0; i < t->size; i++) {
68 //if(bgdata==NULL) bgdata=malloc(sizeof(char)*t->size);
69 //for (k = 0; k < indent-1; k++) printf("\t");
71 j += dump(js, t+1+j, count-j, indent+1, map, i);
79 int loadmap(char *mn, map_t *map)
91 FILE *fh = fopen(mn, "r");
96 /* Allocate some tokens as a start */
97 tok = malloc(sizeof(*tok) * tokcount);
99 fprintf(stderr, "malloc(): errno=%d\n", errno);
104 /* Read another chunk */
105 r = fread(buf, 1, sizeof(buf), fh);
107 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
111 if (eof_expected != 0) {
114 fprintf(stderr, "fread(): unexpected EOF\n");
119 js = realloc(js, jslen + r + 1);
121 fprintf(stderr, "realloc(): errno=%d\n", errno);
124 strncpy(js + jslen, buf, r);
128 r = jsmn_parse(&p, js, jslen, tok, tokcount);
130 if (r == JSMN_ERROR_NOMEM) {
131 tokcount = tokcount * 2;
132 tok = realloc(tok, sizeof(*tok) * tokcount);
134 fprintf(stderr, "realloc(): errno=%d\n", errno);
140 dump(js, tok, p.toknext, 0, map, 0);
141 //fprintf(stdout, "[[[[%d]]]]\n", sizeof(tok));
142 //printf("[\n%d\n]", jslen);
153 loadmap("../../../../data/test.map", &map);