]> 4ch.mooo.com Git - 16.git/blob - src/lib/mapread.c
updated my todo
[16.git] / src / lib / mapread.c
1 #include "src/lib/mapread.h"
2
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) {
6                 return 0;
7         }
8         return -1;
9 }
10
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) {
13         dword i;
14         word j;//, k;
15         bitmap_t bp;
16         planar_buf_t bpp;
17         #ifdef DEBUG_JS
18         if(indent==0)
19         {
20                 fprintf(stdout, "%s\n", js);
21                 fprintf(stdout, "\n");
22         }
23         #endif
24         #ifdef DEBUG_DUMPVARS
25         fprintf(stdout, "t->size=[%d]   ", t->size);
26         fprintf(stdout, "q=[%d] ", q);
27         fprintf(stdout, "indent= [%d]   ", indent);
28         fprintf(stdout, "js_sv= [%s]\n", js_sv);
29         #endif
30         if (count == 0) {
31                 return 0;
32         }
33         /* We may want to do strtol() here to get numeric value */
34 //0000fprintf(stderr, "t->type=%d\n", t->type);
35         if (t->type == JSMN_PRIMITIVE) {
36                 if(strstr(js_sv, "data"))
37                 {
38                         /*
39                                 here we should recursivly call dump again here to skip over the array until we get the facking width of the map.
40                                 so we can initiate the map which allocates the facking map->tiles->data->data properly and THEN we can return
41                                 here to read the data.... That is my design for this... wwww
42
43                                 FUCK well i am stuck.... wwww
44                         */
45                         map->data[q] = (byte)atoi(js+t->start);
46                         #ifdef DEBUG_MAPDATA
47                                 fprintf(stdout, "%d[%d]", q, map->data[q]);
48                         #endif
49                 }
50                 else
51                 if(strstr(js_sv, "height"))
52                 {
53                         map->height = atoi(js+t->start);
54                         #ifdef DEBUG_MAPVAR
55                         fprintf(stdout, "indent= [%d]   ", indent);
56                         fprintf(stdout, "h:[%d]\n", map->height);
57                         #endif
58                 }else if(strstr(js_sv, "width"))
59                 {
60                         map->width = atoi(js+t->start);
61                         #ifdef DEBUG_MAPVAR
62                         fprintf(stdout, "indent= [%d]   ", indent);
63                         fprintf(stdout, "w:[%d]\n", map->width);
64                         #endif
65                 }
66                 return 1;
67                 /* We may use strndup() to fetch string value */
68         } else if (t->type == JSMN_STRING) {
69                 if(jsoneq(js, t, "data") == 0)
70                 {
71 //                      fprintf(stdout, "[[[[%d|%d]]]]\n", &(t+1)->size, (t+1)->size);
72 //                      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);
73                         map->data = malloc(sizeof(byte) * (t+1)->size);
74                         map->tiles = malloc(sizeof(tiles_t));
75                         map->tiles->btdata = malloc(sizeof(bitmap_t));
76                         map->tiles->btdata = malloc(sizeof(bitmap_t));
77                         //fix this to be far~
78 //                      bp = bitmapLoadPcx("data/ed.pcx");
79                         bp = bitmapLoadPcx("data/koishi^^.pcx");
80                         bpp = planar_buf_from_bitmap0(&bp);
81                         map->tiles->btdata = &bp;
82                         //0000++++map->tiles->data = planar_buf_from_bitmap0(&bp);
83                         map->tiles->data = bpp;
84                         //map->tiles->data->data = malloc((16/**2*/)*16);
85                         //map->tiles->data->width = (16/**2*/);
86                         //map->tiles->data->height= 16;
87                         map->tiles->tileHeight = 16;
88                         map->tiles->tileWidth = 16;
89                         map->tiles->rows = 1;
90                         map->tiles->cols = 1;
91                         map->tiles->debug_text=false;
92                         strcpy(js_sv, "data");//strdup(js+t->start);//, t->end - t->start);
93                 }
94                 else
95                 if (jsoneq(js, t, "height") == 0 && indent<=1)
96                 {
97                         strcpy(js_sv, "height");//strdup(js+t->start);//, t->end - t->start);
98                 }else
99                 if(jsoneq(js, t, "width") == 0 && indent<=1)
100                 {
101                         strcpy(js_sv, "width");//strdup(js+t->start);//, t->end - t->start);
102                 }else strcpy(js_sv, "\0");
103                 return 1;
104         } else if (t->type == JSMN_OBJECT) {
105                 //fprintf(stdout, "\n");
106                 j = 0;
107                 for (i = 0; i < t->size; i++) {
108                         //for (k = 0; k < indent; k++) fprintf(stdout, "\t");
109                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
110                         //fprintf(stdout, ": ");
111                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
112                         //fprintf(stdout, "\n");
113                 }
114                 return j+1;
115         } else if (t->type == JSMN_ARRAY) {
116                 j = 0;
117                 //fprintf(stdout, "==\n");
118                 for (i = 0; i < t->size; i++) {
119                         //for (k = 0; k < indent-1; k++) fprintf(stdout, "\t");
120                         //fprintf(stdout, "\t-");
121                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
122                         //fprintf(stdout, "==\n");
123                 }
124                 return j+1;
125         }
126         return 0;
127 }
128
129 int loadmap(char *mn, map_t *map)
130 {
131         int r;
132         static word incr=0;
133         int eof_expected = 0;
134         char *js = NULL;
135         size_t jslen = 0;
136         char buf[BUFSIZ];
137         static char js_ss[16];
138
139         jsmn_parser p;
140         jsmntok_t *tok;
141         size_t tokcount = 2;
142
143         FILE *fh = fopen(mn, "r");
144
145         /* Prepare parser */
146         jsmn_init(&p);
147
148         /* Allocate some tokens as a start */
149 //0000fprintf(stderr, "tok malloc\n");
150         tok = malloc(sizeof(*tok) * tokcount);
151         if (tok == NULL) {
152                 fprintf(stderr, "malloc(): errno=%d\n", errno);
153                 return 3;
154         }
155
156         for (;;) {
157                 /* Read another chunk */
158 //0000fprintf(stderr, "read\n");
159                 r = fread(buf, 1, sizeof(buf), fh);
160                 if (r < 0) {
161                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
162                         return 1;
163                 }
164                 if (r == 0) {
165                         if (eof_expected != 0) {
166                                 return 0;
167                         } else {
168                                 fprintf(stderr, "fread(): unexpected EOF\n");
169                                 return 2;
170                         }
171                 }
172 //0000fprintf(stdout, "r=       [%d]    BUFSIZ=%d\n", r, BUFSIZ);
173 //0000fprintf(stderr, "js alloc~\n");
174                 js = realloc(js, jslen + r + 1);
175                 if (js == NULL) {
176                         fprintf(stderr, "*js=%Fp\n", *js);
177                         fprintf(stderr, "realloc(): errno = %d\n", errno);
178                         return 3;
179                 }
180                 strncpy(js + jslen, buf, r);
181                 jslen = jslen + r;
182
183 again:
184 //0000fprintf(stdout, " parse~ tok=%zu  jslen=%zu       r=%d    _memavl()=%u    BUFSIZ=%d~\n", tokcount, jslen, r, _memavl(), BUFSIZ);
185 //0000fprintf(stdout, "p=[%u]   [%u]    [%d]\n", p.pos, p.toknext, p.toksuper);
186 /*
187                 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
188 */
189                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
190 //0000fprintf(stdout, "r=       [%d]\n", r);
191                 if (r < 0) {
192                         if (r == JSMN_ERROR_NOMEM) {
193                                 tokcount = tokcount * 2;
194 //0000fprintf(stderr, "tok realloc~ %zu\n", tokcount);
195                                 tok = realloc(tok, sizeof(*tok) * tokcount);
196                                 if (tok == NULL) {
197                                         fprintf(stderr, "realloc(): errno=%d\n", errno);
198                                         return 3;
199                                 }
200                                 goto again;
201                         }
202                 } else {
203                         //printf("js=%Fp\n", (js));
204                         //printf("*js=%Fp\n", (*(js)));
205                         //printf("&*js=%s\n", &(*(js)));
206                         //printf("&buf=[%Fp]\n", &buf);
207                         //printf("&buf_seg=[%x]\n", FP_SEG(&buf));
208                         //printf("&buf_off=[%x]\n", FP_OFF(&buf));
209                         //printf("&buf_fp=[%Fp]\n", MK_FP(FP_SEG(&buf), FP_OFF(&buf)));
210                         //printf("buf=[\n%s\n]\n", buf);
211                         //printf("buff=[%Fp]\n", buff);
212                         //printf("(*buff)=[%Fp]\n", (*buff));
213                         //printf("&(*buff)=[\n%s\n]\n", &(*buff));
214                         #ifdef DEBUG_DUMPVARS
215                         fprintf(stdout, "running dump~\n");
216                         #endif
217                         dump(js, tok, p.toknext, incr, &js_ss, map, 0);
218                         eof_expected = 1;
219                 }
220         }
221
222         free(js);
223         free(tok);
224         fclose(fh);
225
226         return 0;
227 }