]> 4ch.mooo.com Git - 16.git/blob - src/lib/mapread.c
did some cleanings wwww
[16.git] / src / lib / mapread.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover\r
3  *\r
4  * This file is part of Project 16.\r
5  *\r
6  * Project 16 is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Project 16 is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or\r
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,\r
19  * Fifth Floor, Boston, MA 02110-1301 USA.\r
20  *\r
21  */\r
22 \r
23 #include "src/lib/mapread.h"\r
24 \r
25 int jsoneq(const char *json, jsmntok_t *tok, const char *s) {\r
26         if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&\r
27                         strncmp(json + tok->start, s, tok->end - tok->start) == 0) {\r
28                 return 0;\r
29         }\r
30         return -1;\r
31 }\r
32 \r
33 //this function is quite messy ^^; sorry! it is a quick and dirty fix~\r
34 word dump(const char *js, jsmntok_t *t, size_t count, word indent, char *js_sv, map_t *map, dword q) {\r
35         dword i;\r
36         word j;//, k;\r
37         bitmap_t bp;\r
38         #ifdef DEBUG_JS\r
39         if(indent==0)\r
40         {\r
41                 fprintf(stdout, "%s\n", js);\r
42                 fprintf(stdout, "\n");\r
43         }\r
44         #endif\r
45         #ifdef DEBUG_DUMPVARS\r
46         fprintf(stdout, "t->size=[%d]   ", t->size);\r
47         fprintf(stdout, "q=[%d] ", q);\r
48         fprintf(stdout, "indent= [%d]   ", indent);\r
49         fprintf(stdout, "js_sv= [%s]\n", js_sv);\r
50         #endif\r
51         if (count == 0) {\r
52                 return 0;\r
53         }\r
54         /* We may want to do strtol() here to get numeric value */\r
55 //0000fprintf(stderr, "t->type=%d\n", t->type);\r
56         if (t->type == JSMN_PRIMITIVE) {\r
57                 if(strstr(js_sv, "data"))\r
58                 {\r
59                         /*\r
60                                 here we should recursivly call dump again here to skip over the array until we get the facking width of the map.\r
61                                 so we can initiate the map which allocates the facking map->tiles->data->data properly and THEN we can return\r
62                                 here to read the data.... That is my design for this... wwww\r
63 \r
64                                 FUCK well i am stuck.... wwww\r
65                         */\r
66                         map->data[q] = (byte)atoi(js+t->start);\r
67                         #ifdef DEBUG_MAPDATA\r
68                                 fprintf(stdout, "%d[%d]", q, map->data[q]);\r
69                         #endif\r
70                 }\r
71                 else\r
72                 if(strstr(js_sv, "height"))\r
73                 {\r
74                         map->height = atoi(js+t->start);\r
75                         #ifdef DEBUG_MAPVAR\r
76                         fprintf(stdout, "indent= [%d]   ", indent);\r
77                         fprintf(stdout, "h:[%d]\n", map->height);\r
78                         #endif\r
79                 }else if(strstr(js_sv, "width"))\r
80                 {\r
81                         map->width = atoi(js+t->start);\r
82                         #ifdef DEBUG_MAPVAR\r
83                         fprintf(stdout, "indent= [%d]   ", indent);\r
84                         fprintf(stdout, "w:[%d]\n", map->width);\r
85                         #endif\r
86                 }\r
87                 return 1;\r
88                 /* We may use strndup() to fetch string value */\r
89         } else if (t->type == JSMN_STRING) {\r
90                 if(jsoneq(js, t, "data") == 0)\r
91                 {\r
92 //                      fprintf(stdout, "[[[[%d|%d]]]]\n", &(t+1)->size, (t+1)->size);\r
93 //                      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);\r
94                         map->data = malloc(sizeof(byte) * (t+1)->size);\r
95                         map->tiles = malloc(sizeof(tiles_t));\r
96                         map->tiles->btdata = malloc(sizeof(bitmap_t));\r
97                         //fix this to be far~\r
98 //0000                  bp = bitmapLoadPcx("data/ed.pcx");\r
99 //                      bp = bitmapLoadPcx("data/koishi^^.pcx");\r
100                         map->tiles->btdata = &bp;\r
101 //----                  map->tiles->data = planar_buf_from_bitmap(&bp);\r
102                         //map->tiles->data->data = malloc((16/**2*/)*16);\r
103                         //map->tiles->data->width = (16/**2*/);\r
104                         //map->tiles->data->height= 16;\r
105                         map->tiles->tileHeight = 16;\r
106                         map->tiles->tileWidth = 16;\r
107                         map->tiles->rows = 1;\r
108                         map->tiles->cols = 1;\r
109                         map->tiles->debug_text=false;\r
110                         strcpy(js_sv, "data");//strdup(js+t->start);//, t->end - t->start);\r
111                 }\r
112                 else\r
113                 if (jsoneq(js, t, "height") == 0 && indent<=1)\r
114                 {\r
115                         strcpy(js_sv, "height");//strdup(js+t->start);//, t->end - t->start);\r
116                 }else\r
117                 if(jsoneq(js, t, "width") == 0 && indent<=1)\r
118                 {\r
119                         strcpy(js_sv, "width");//strdup(js+t->start);//, t->end - t->start);\r
120                 }else strcpy(js_sv, "\0");\r
121                 return 1;\r
122         } else if (t->type == JSMN_OBJECT) {\r
123                 //fprintf(stdout, "\n");\r
124                 j = 0;\r
125                 for (i = 0; i < t->size; i++) {\r
126                         //for (k = 0; k < indent; k++) fprintf(stdout, "\t");\r
127                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);\r
128                         //fprintf(stdout, ": ");\r
129                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);\r
130                         //fprintf(stdout, "\n");\r
131                 }\r
132                 return j+1;\r
133         } else if (t->type == JSMN_ARRAY) {\r
134                 j = 0;\r
135                 //fprintf(stdout, "==\n");\r
136                 for (i = 0; i < t->size; i++) {\r
137                         //for (k = 0; k < indent-1; k++) fprintf(stdout, "\t");\r
138                         //fprintf(stdout, "\t-");\r
139                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);\r
140                         //fprintf(stdout, "==\n");\r
141                 }\r
142                 return j+1;\r
143         }\r
144         return 0;\r
145 }\r
146 \r
147 int loadmap(char *mn, map_t *map)\r
148 {\r
149         int r;\r
150         static word incr=0;\r
151         int eof_expected = 0;\r
152         char *js = NULL;\r
153         size_t jslen = 0;\r
154         char buf[BUFSIZ];\r
155         static char js_ss[16];\r
156 \r
157         jsmn_parser p;\r
158         jsmntok_t *tok;\r
159         size_t tokcount = 2;\r
160 \r
161         FILE *fh = fopen(mn, "r");\r
162 \r
163         /* Prepare parser */\r
164         jsmn_init(&p);\r
165 \r
166         /* Allocate some tokens as a start */\r
167 //0000fprintf(stderr, "tok malloc\n");\r
168         tok = malloc(sizeof(*tok) * tokcount);\r
169         if (tok == NULL) {\r
170                 fprintf(stderr, "malloc(): errno=%d\n", errno);\r
171                 return 3;\r
172         }\r
173 \r
174         for (;;) {\r
175                 /* Read another chunk */\r
176 //0000fprintf(stderr, "read\n");\r
177                 r = fread(buf, 1, sizeof(buf), fh);\r
178                 if (r < 0) {\r
179                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);\r
180                         return 1;\r
181                 }\r
182                 if (r == 0) {\r
183                         if (eof_expected != 0) {\r
184                                 return 0;\r
185                         } else {\r
186                                 fprintf(stderr, "fread(): unexpected EOF\n");\r
187                                 return 2;\r
188                         }\r
189                 }\r
190 //0000fprintf(stdout, "r=       [%d]    BUFSIZ=%d\n", r, BUFSIZ);\r
191 //0000fprintf(stderr, "js alloc~\n");\r
192                 js = realloc(js, jslen + r + 1);\r
193                 if (js == NULL) {\r
194                         fprintf(stderr, "*js=%Fp\n", *js);\r
195                         fprintf(stderr, "realloc(): errno = %d\n", errno);\r
196                         return 3;\r
197                 }\r
198                 strncpy(js + jslen, buf, r);\r
199                 jslen = jslen + r;\r
200 \r
201 again:\r
202 //0000fprintf(stdout, " parse~ tok=%zu  jslen=%zu       r=%d    _memavl()=%u    BUFSIZ=%d~\n", tokcount, jslen, r, _memavl(), BUFSIZ);\r
203 //0000fprintf(stdout, "p=[%u]   [%u]    [%d]\n", p.pos, p.toknext, p.toksuper);\r
204 /*\r
205                 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\r
206 */\r
207                 r = jsmn_parse(&p, js, jslen, tok, tokcount);\r
208 //0000fprintf(stdout, "r=       [%d]\n", r);\r
209                 if (r < 0) {\r
210                         if (r == JSMN_ERROR_NOMEM) {\r
211                                 tokcount = tokcount * 2;\r
212 //0000fprintf(stderr, "tok realloc~ %zu\n", tokcount);\r
213                                 tok = realloc(tok, sizeof(*tok) * tokcount);\r
214                                 if (tok == NULL) {\r
215                                         fprintf(stderr, "realloc(): errno=%d\n", errno);\r
216                                         return 3;\r
217                                 }\r
218                                 goto again;\r
219                         }\r
220                 } else {\r
221                         //printf("js=%Fp\n", (js));\r
222                         //printf("*js=%Fp\n", (*(js)));\r
223                         //printf("&*js=%s\n", &(*(js)));\r
224                         //printf("&buf=[%Fp]\n", &buf);\r
225                         //printf("&buf_seg=[%x]\n", FP_SEG(&buf));\r
226                         //printf("&buf_off=[%x]\n", FP_OFF(&buf));\r
227                         //printf("&buf_fp=[%Fp]\n", MK_FP(FP_SEG(&buf), FP_OFF(&buf)));\r
228                         //printf("buf=[\n%s\n]\n", buf);\r
229                         //printf("buff=[%Fp]\n", buff);\r
230                         //printf("(*buff)=[%Fp]\n", (*buff));\r
231                         //printf("&(*buff)=[\n%s\n]\n", &(*buff));\r
232                         #ifdef DEBUG_DUMPVARS\r
233                         fprintf(stdout, "running dump~\n");\r
234                         #endif\r
235                         dump(js, tok, p.toknext, incr, &js_ss, map, 0);\r
236                         eof_expected = 1;\r
237                 }\r
238         }\r
239 \r
240         free(js);\r
241         free(tok);\r
242         fclose(fh);\r
243 \r
244         return 0;\r
245 }\r