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/*, int w*/) {
17 /* We may want to do strtol() here to get numeric value */
18 if (t->type == JSMN_PRIMITIVE) {
23 here we should recursivly call dump again here to skip over the array until we get the facking width of the map.
24 so we can initiate the map which allocates the facking map->tiles->data->data properly and THEN we can return
25 here to read the data.... That is my design for this... wwww
27 FUCK well i am stuck.... wwww
29 //dump(js, t+1, count, indent, map, i);
31 //bgdata[q] = strtol(js+t->start, (char **)js+t->end, 10);
32 //if(strtol(js+t->start, (char **)js+t->end, 10)==0){ /*printf("%d\n", sizeof(map->tiles->data->data));*/ /*fprintf(stderr, "\nFACK! %d\n", errno);*/ return 1; /*exit(-1); */}
33 map->tiles->data->data[q] = (byte)strtol(js+t->start, (char **)js+t->end, 10);
34 printf("%d[%d]", q, map->tiles->data->data[q]);
35 //printf("%d[%d]", q, bgdata[q]);
40 map->height = (int)strtol(js+t->start, (char **)js+t->end, 10);
41 printf("h:[%d]\n", map->height);
42 }else if(js_sv == "width")
44 map->width = (int)strtol(js+t->start, (char **)js+t->end, 10);
45 printf("w:[%d]\n", map->width);
48 /* We may use strndup() to fetch string value */
49 } else if (t->type == JSMN_STRING) {
50 //printf("'%.*s'", t->end - t->start, js+t->start);
52 if(jsoneq(js, t, "data") == 0 )
55 //map->tiles->data->data = malloc((16*2)*16);
56 js_sv="data";//strdup(js+t->start);//, t->end - t->start);
57 //printf("%s\n", js_sv);
60 if (jsoneq(js, t, "height") == 0 && indent==1)
62 js_sv="height";//strdup(js+t->start);//, t->end - t->start);
63 //printf("%s\n", js_sv);
64 }else if (jsoneq(js, t, "width") == 0 && indent==1)
66 js_sv="width";//strdup(js+t->start);//, t->end - t->start);
67 //printf("%s\n", js_sv);
70 } else if (t->type == JSMN_OBJECT) {
73 for (i = 0; i < t->size; i++) {
74 //for (k = 0; k < indent; k++) printf("\t");
75 j += dump(js, t+1+j, count-j, indent+1, map, i/*, w*/);
77 j += dump(js, t+1+j, count-j, indent+1, map, i/*, w*/);
81 } else if (t->type == JSMN_ARRAY) {
84 for (i = 0; i < t->size; i++) {
85 //printf("[[[[%d]]]]\n", t->size);
86 //if(bgdata==NULL) bgdata=malloc(sizeof(char)*t->size);
87 //for (k = 0; k < indent-1; k++) printf("\t");
89 j += dump(js, t+1+j, count-j, indent+1, map, i/*, t->size*/);
97 static int loadmap(char *mn, map_t *map/*, word w*/)
100 int eof_expected = 0;
109 FILE *fh = fopen(mn, "r");
114 /* Allocate some tokens as a start */
115 tok = malloc(sizeof(*tok) * tokcount);
117 fprintf(stderr, "malloc(): errno=%d\n", errno);
122 /* Read another chunk */
123 r = fread(buf, 1, sizeof(buf), fh);
125 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
129 if (eof_expected != 0) {
132 fprintf(stderr, "fread(): unexpected EOF\n");
137 js = realloc(js, jslen + r + 1);
139 fprintf(stderr, "realloc(): errno = %d\n", errno);
142 strncpy(js + jslen, buf, r);
146 r = jsmn_parse(&p, js, jslen, tok, tokcount);
148 if (r == JSMN_ERROR_NOMEM) {
149 tokcount = tokcount * 2;
150 tok = realloc(tok, sizeof(*tok) * tokcount);
152 fprintf(stderr, "realloc(): errno=%d\n", errno);
158 dump(js, tok, p.toknext, 0, map, 0/*, w*/);
159 //fprintf(stdout, "[[[[%d]]]]\n", sizeof(tok));
160 //printf("[\n%d\n]", jslen);
175 loadmap("../../../../data/test.map", &map);