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