]> 4ch.mooo.com Git - 16.git/blob - src/lib/mapread.c
wwww added timer!!
[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         #ifdef DEBUG_JS
17         if(indent==0)
18         {
19                 fprintf(stdout, "%s\n", js);
20                 fprintf(stdout, "\n");
21         }
22         #endif
23         #ifdef DEBUG_DUMPVARS
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);
28         #endif
29         if (count == 0) {
30                 return 0;
31         }
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"))
36                 {
37                         /*
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
41
42                                 FUCK well i am stuck.... wwww
43                         */
44                         map->data[q] = (byte)atoi(js+t->start);
45                         #ifdef DEBUG_MAPDATA
46                                 fprintf(stdout, "%d[%d]", q, map->data[q]);
47                         #endif
48                 }
49                 else
50                 if(strstr(js_sv, "height"))
51                 {
52                         map->height = atoi(js+t->start);
53                         #ifdef DEBUG_MAPVAR
54                         fprintf(stdout, "indent= [%d]   ", indent);
55                         fprintf(stdout, "h:[%d]\n", map->height);
56                         #endif
57                 }else if(strstr(js_sv, "width"))
58                 {
59                         map->width = atoi(js+t->start);
60                         #ifdef DEBUG_MAPVAR
61                         fprintf(stdout, "indent= [%d]   ", indent);
62                         fprintf(stdout, "w:[%d]\n", map->width);
63                         #endif
64                 }
65                 return 1;
66                 /* We may use strndup() to fetch string value */
67         } else if (t->type == JSMN_STRING) {
68                 if(jsoneq(js, t, "data") == 0)
69                 {
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->data = malloc(sizeof(bitmap_t));
75                         //fix this to be far~
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*/);
80                         //map->tiles->data->height= 16;
81                         map->tiles->tileHeight = 16;
82                         map->tiles->tileWidth = 16;
83                         map->tiles->rows = 1;
84                         map->tiles->cols = 1;
85                         strcpy(js_sv, "data");//strdup(js+t->start);//, t->end - t->start);
86                 }
87                 else
88                 if (jsoneq(js, t, "height") == 0 && indent<=1)
89                 {
90                         strcpy(js_sv, "height");//strdup(js+t->start);//, t->end - t->start);
91                 }else
92                 if(jsoneq(js, t, "width") == 0 && indent<=1)
93                 {
94                         strcpy(js_sv, "width");//strdup(js+t->start);//, t->end - t->start);
95                 }else strcpy(js_sv, "\0");
96                 return 1;
97         } else if (t->type == JSMN_OBJECT) {
98                 //fprintf(stdout, "\n");
99                 j = 0;
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");
106                 }
107                 return j+1;
108         } else if (t->type == JSMN_ARRAY) {
109                 j = 0;
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");
116                 }
117                 return j+1;
118         }
119         return 0;
120 }
121
122 int loadmap(char *mn, map_t *map)
123 {
124         int r;
125         static word incr=0;
126         int eof_expected = 0;
127         char *js = NULL;
128         size_t jslen = 0;
129         char buf[BUFSIZ];
130         static char js_ss[16];
131
132         jsmn_parser p;
133         jsmntok_t *tok;
134         size_t tokcount = 2;
135
136         FILE *fh = fopen(mn, "r");
137
138         /* Prepare parser */
139         jsmn_init(&p);
140
141         /* Allocate some tokens as a start */
142 //0000fprintf(stderr, "tok malloc\n");
143         tok = malloc(sizeof(*tok) * tokcount);
144         if (tok == NULL) {
145                 fprintf(stderr, "malloc(): errno=%d\n", errno);
146                 return 3;
147         }
148
149         for (;;) {
150                 /* Read another chunk */
151 //0000fprintf(stderr, "read\n");
152                 r = fread(buf, 1, sizeof(buf), fh);
153                 if (r < 0) {
154                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
155                         return 1;
156                 }
157                 if (r == 0) {
158                         if (eof_expected != 0) {
159                                 return 0;
160                         } else {
161                                 fprintf(stderr, "fread(): unexpected EOF\n");
162                                 return 2;
163                         }
164                 }
165 //0000fprintf(stdout, "r=       [%d]    BUFSIZ=%d\n", r, BUFSIZ);
166 //0000fprintf(stderr, "js alloc~\n");
167                 js = realloc(js, jslen + r + 1);
168                 if (js == NULL) {
169                         fprintf(stderr, "*js=%Fp\n", *js);
170                         fprintf(stderr, "realloc(): errno = %d\n", errno);
171                         return 3;
172                 }
173                 strncpy(js + jslen, buf, r);
174                 jslen = jslen + r;
175
176 again:
177 //0000fprintf(stdout, " parse~ tok=%zu  jslen=%zu       r=%d    _memavl()=%u    BUFSIZ=%d~\n", tokcount, jslen, r, _memavl(), BUFSIZ);
178 //0000fprintf(stdout, "p=[%u]   [%u]    [%d]\n", p.pos, p.toknext, p.toksuper);
179 /*
180                 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
181 */
182                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
183 //0000fprintf(stdout, "r=       [%d]\n", r);
184                 if (r < 0) {
185                         if (r == JSMN_ERROR_NOMEM) {
186                                 tokcount = tokcount * 2;
187 //0000fprintf(stderr, "tok realloc~ %zu\n", tokcount);
188                                 tok = realloc(tok, sizeof(*tok) * tokcount);
189                                 if (tok == NULL) {
190                                         fprintf(stderr, "realloc(): errno=%d\n", errno);
191                                         return 3;
192                                 }
193                                 goto again;
194                         }
195                 } else {
196                         //printf("js=%Fp\n", (js));
197                         //printf("*js=%Fp\n", (*(js)));
198                         //printf("&*js=%s\n", &(*(js)));
199                         //printf("&buf=[%Fp]\n", &buf);
200                         //printf("&buf_seg=[%x]\n", FP_SEG(&buf));
201                         //printf("&buf_off=[%x]\n", FP_OFF(&buf));
202                         //printf("&buf_fp=[%Fp]\n", MK_FP(FP_SEG(&buf), FP_OFF(&buf)));
203                         //printf("buf=[\n%s\n]\n", buf);
204                         //printf("buff=[%Fp]\n", buff);
205                         //printf("(*buff)=[%Fp]\n", (*buff));
206                         //printf("&(*buff)=[\n%s\n]\n", &(*buff));
207                         #ifdef DEBUG_DUMPVARS
208                         fprintf(stdout, "running dump~\n");
209                         #endif
210                         dump(js, tok, p.toknext, incr, &js_ss, map, 0);
211                         eof_expected = 1;
212                 }
213         }
214
215         free(js);
216         free(tok);
217         fclose(fh);
218
219         return 0;
220 }