1 #include "src/lib/mapread.h"
3 int jsoneq(const char huge *json, jsmntok_t huge *tok, const char *s) {
4 if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
5 strncmp((char const near *)json + tok->start, s, tok->end - tok->start) == 0) {
11 //this function is quite messy ^^; sorry! it is a quick and dirty fix~
12 int dump(const char huge *js, jsmntok_t huge *t, size_t count, int indent, char *js_sv, map_t *map, int q) {
18 fprintf(stdout, "%s\n", js);
19 fprintf(stdout, "\n");
23 fprintf(stdout, "indent= [%d] ", indent);
24 fprintf(stdout, "js_sv= [%s]\n", js_sv);
29 /* We may want to do strtol() here to get numeric value */
30 if (t->type == JSMN_PRIMITIVE) {
31 if(strstr(js_sv, "data"))
34 here we should recursivly call dump again here to skip over the array until we get the facking width of the map.
35 so we can initiate the map which allocates the facking map->tiles->data->data properly and THEN we can return
36 here to read the data.... That is my design for this... wwww
38 FUCK well i am stuck.... wwww
40 //---- map->data[q] = (byte)strtol(js+t->start, (char **)t->end, 10);
41 map->data[q] = (byte)atoi((const char *)js+t->start);
43 fprintf(stdout, "%d[%d]", q, map->data[q]);
47 if(strstr(js_sv, "height"))
49 //---- map->height = (unsigned int)strtol(js+t->start, (char **)js+t->end, 10);
50 map->height = atoi((const char *)js+t->start);
52 fprintf(stdout, "indent= [%d] ", indent);
53 fprintf(stdout, "h:[%d]\n", map->height);
55 }else if(strstr(js_sv, "width"))
57 //---- map->width = (unsigned int)strtol(js+t->start, (char **)js+t->end, 10);
58 map->width = atoi((const char *)js+t->start);
60 fprintf(stdout, "indent= [%d] ", indent);
61 fprintf(stdout, "w:[%d]\n", map->width);
65 /* We may use strndup() to fetch string value */
66 } else if (t->type == JSMN_STRING) {
67 if(jsoneq(js, t, "data") == 0)
69 // fprintf(stdout, "[[[[%d|%d]]]]\n", &(t+1)->size, (t+1)->size);
70 // 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);
71 map->data = halloc(sizeof(byte) * (t+1)->size, sizeof(byte));
72 //map->data = malloc(sizeof(byte) * (t+1)->size);
73 map->tiles = /*_f*/malloc(sizeof(tiles_t));
74 //map->tiles->data = malloc(sizeof(bitmap_t));
76 bp = bitmapLoadPcx("data/ed.pcx");
77 map->tiles->data = &bp;
78 //map->tiles->data->data = malloc((16/**2*/)*16);
79 //map->tiles->data->width = (16/**2*/);
\r
80 //map->tiles->data->height= 16;
\r
81 map->tiles->tileHeight = 16;
\r
82 map->tiles->tileWidth = 16;
\r
83 map->tiles->rows = 1;
\r
85 strcpy(js_sv, "data");//strdup(js+t->start);//, t->end - t->start);
88 if (jsoneq(js, t, "height") == 0 && indent<=1)
90 strcpy(js_sv, "height");//strdup(js+t->start);//, t->end - t->start);
92 if(jsoneq(js, t, "width") == 0 && indent<=1)
94 strcpy(js_sv, "width");//strdup(js+t->start);//, t->end - t->start);
95 }else strcpy(js_sv, "\0");
97 } else if (t->type == JSMN_OBJECT) {
98 //fprintf(stdout, "\n");
100 for (i = 0; i < t->size; i++) {
101 //for (k = 0; k < indent; k++) fprintf(stdout, "\t");
102 j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
103 //fprintf(stdout, ": ");
104 j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
105 //fprintf(stdout, "\n");
108 } else if (t->type == JSMN_ARRAY) {
110 //fprintf(stdout, "==\n");
111 for (i = 0; i < t->size; i++) {
112 //for (k = 0; k < indent-1; k++) fprintf(stdout, "\t");
113 //fprintf(stdout, "\t-");
114 j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
115 //fprintf(stdout, "==\n");
122 int loadmap(char *mn, map_t *map)
126 int eof_expected = 0;
128 char huge *js = NULL;
131 static char js_ss[16];
137 FILE *fh = fopen(mn, "r");
142 /* Allocate some tokens as a start */
143 tok = _fmalloc(sizeof(*tok) * tokcount);
145 fprintf(stderr, "malloc(): errno=%d\n", errno);
150 /* Read another chunk */
151 r = fread(buf, 1, sizeof(buf), fh);
153 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
157 if (eof_expected != 0) {
160 fprintf(stderr, "fread(): unexpected EOF\n");
164 jz = realloc(jz, jslen + r + 1);
165 //js = _frealloc(js, jslen + r + 1);
167 fprintf(stderr, "*js=%s\n", *js);
168 fprintf(stderr, "*jz=%s\n", *jz);
169 fprintf(stderr, "realloc(): errno = %d\n", errno);
172 //printf("_fstrncpy~\n");
173 strncpy(jz + jslen, buf, r);
174 //printf("_fstrncpy okies~~\n");
178 js = (char huge *)*jz;
179 r = jsmn_parse(&p, js, jslen, tok, tokcount);
181 if (r == JSMN_ERROR_NOMEM) {
182 tokcount = tokcount * 2;
183 tok = _frealloc(tok, sizeof(*tok) * tokcount);
185 fprintf(stderr, "realloc(): errno=%d\n", errno);
191 //printf("*buff=[\n%s\n]\n", *buff);
192 //printf("buff=[\n%Fp\n]\n", buff);
193 dump(js, tok, p.toknext, incr, &js_ss, map, 0);