1 #include "src/lib/mapread.h"
3 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 word dump(const char *js, jsmntok_t *t, size_t count, word indent, char *js_sv, map_t *map, dword q) {
19 fprintf(stdout, "%s\n", js);
20 fprintf(stdout, "\n");
24 fprintf(stdout, "t->size=[%d] ", t->size);
25 fprintf(stdout, "q=[%d] ", q);
26 fprintf(stdout, "indent= [%d] ", indent);
27 fprintf(stdout, "js_sv= [%s]\n", js_sv);
32 /* We may want to do strtol() here to get numeric value */
33 //0000fprintf(stderr, "t->type=%d\n", t->type);
34 if (t->type == JSMN_PRIMITIVE) {
35 if(strstr(js_sv, "data"))
38 here we should recursivly call dump again here to skip over the array until we get the facking width of the map.
39 so we can initiate the map which allocates the facking map->tiles->data->data properly and THEN we can return
40 here to read the data.... That is my design for this... wwww
42 FUCK well i am stuck.... wwww
44 map->data[q] = (byte)atoi(js+t->start);
46 fprintf(stdout, "%d[%d]", q, map->data[q]);
50 if(strstr(js_sv, "height"))
52 map->height = atoi(js+t->start);
54 fprintf(stdout, "indent= [%d] ", indent);
55 fprintf(stdout, "h:[%d]\n", map->height);
57 }else if(strstr(js_sv, "width"))
59 map->width = atoi(js+t->start);
61 fprintf(stdout, "indent= [%d] ", indent);
62 fprintf(stdout, "w:[%d]\n", map->width);
66 /* We may use strndup() to fetch string value */
67 } else if (t->type == JSMN_STRING) {
68 if(jsoneq(js, t, "data") == 0)
70 // fprintf(stdout, "[[[[%d|%d]]]]\n", &(t+1)->size, (t+1)->size);
71 // fprintf(stdout, "\n%.*s[xx[%d|%d]xx]\n", (t+1)->end - (t+1)->start, js+(t+1)->start, &(t+1)->size, (t+1)->size);
72 map->data = malloc(sizeof(byte) * (t+1)->size);
73 map->tiles = malloc(sizeof(tiles_t));
74 map->tiles->btdata = malloc(sizeof(bitmap_t));
76 bp = bitmapLoadPcx("data/ed.pcx");
77 // bp = bitmapLoadPcx("data/koishi^^.pcx");
78 map->tiles->btdata = &bp;
79 map->tiles->data = planar_buf_from_bitmap(&bp);
80 //map->tiles->data->data = malloc((16/**2*/)*16);
81 //map->tiles->data->width = (16/**2*/);
82 //map->tiles->data->height= 16;
83 map->tiles->tileHeight = 16;
84 map->tiles->tileWidth = 16;
87 map->tiles->debug_text=false;
88 strcpy(js_sv, "data");//strdup(js+t->start);//, t->end - t->start);
91 if (jsoneq(js, t, "height") == 0 && indent<=1)
93 strcpy(js_sv, "height");//strdup(js+t->start);//, t->end - t->start);
95 if(jsoneq(js, t, "width") == 0 && indent<=1)
97 strcpy(js_sv, "width");//strdup(js+t->start);//, t->end - t->start);
98 }else strcpy(js_sv, "\0");
100 } else if (t->type == JSMN_OBJECT) {
101 //fprintf(stdout, "\n");
103 for (i = 0; i < t->size; i++) {
104 //for (k = 0; k < indent; k++) fprintf(stdout, "\t");
105 j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
106 //fprintf(stdout, ": ");
107 j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
108 //fprintf(stdout, "\n");
111 } else if (t->type == JSMN_ARRAY) {
113 //fprintf(stdout, "==\n");
114 for (i = 0; i < t->size; i++) {
115 //for (k = 0; k < indent-1; k++) fprintf(stdout, "\t");
116 //fprintf(stdout, "\t-");
117 j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
118 //fprintf(stdout, "==\n");
125 int loadmap(char *mn, map_t *map)
129 int eof_expected = 0;
133 static char js_ss[16];
139 FILE *fh = fopen(mn, "r");
144 /* Allocate some tokens as a start */
145 //0000fprintf(stderr, "tok malloc\n");
146 tok = malloc(sizeof(*tok) * tokcount);
148 fprintf(stderr, "malloc(): errno=%d\n", errno);
153 /* Read another chunk */
154 //0000fprintf(stderr, "read\n");
155 r = fread(buf, 1, sizeof(buf), fh);
157 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
161 if (eof_expected != 0) {
164 fprintf(stderr, "fread(): unexpected EOF\n");
168 //0000fprintf(stdout, "r= [%d] BUFSIZ=%d\n", r, BUFSIZ);
169 //0000fprintf(stderr, "js alloc~\n");
170 js = realloc(js, jslen + r + 1);
172 fprintf(stderr, "*js=%Fp\n", *js);
173 fprintf(stderr, "realloc(): errno = %d\n", errno);
176 strncpy(js + jslen, buf, r);
180 //0000fprintf(stdout, " parse~ tok=%zu jslen=%zu r=%d _memavl()=%u BUFSIZ=%d~\n", tokcount, jslen, r, _memavl(), BUFSIZ);
181 //0000fprintf(stdout, "p=[%u] [%u] [%d]\n", p.pos, p.toknext, p.toksuper);
183 I think it crashes on the line below when it tries to parse the data of huge maps... wwww this is a jsmn problem wwww
185 r = jsmn_parse(&p, js, jslen, tok, tokcount);
186 //0000fprintf(stdout, "r= [%d]\n", r);
188 if (r == JSMN_ERROR_NOMEM) {
189 tokcount = tokcount * 2;
190 //0000fprintf(stderr, "tok realloc~ %zu\n", tokcount);
191 tok = realloc(tok, sizeof(*tok) * tokcount);
193 fprintf(stderr, "realloc(): errno=%d\n", errno);
199 //printf("js=%Fp\n", (js));
200 //printf("*js=%Fp\n", (*(js)));
201 //printf("&*js=%s\n", &(*(js)));
202 //printf("&buf=[%Fp]\n", &buf);
203 //printf("&buf_seg=[%x]\n", FP_SEG(&buf));
204 //printf("&buf_off=[%x]\n", FP_OFF(&buf));
205 //printf("&buf_fp=[%Fp]\n", MK_FP(FP_SEG(&buf), FP_OFF(&buf)));
206 //printf("buf=[\n%s\n]\n", buf);
207 //printf("buff=[%Fp]\n", buff);
208 //printf("(*buff)=[%Fp]\n", (*buff));
209 //printf("&(*buff)=[\n%s\n]\n", &(*buff));
210 #ifdef DEBUG_DUMPVARS
211 fprintf(stdout, "running dump~\n");
213 dump(js, tok, p.toknext, incr, &js_ss, map, 0);