]> 4ch.mooo.com Git - 16.git/blob - src/lib/mapread.c
pngwen help!!!!
[16.git] / src / lib / mapread.c
1 #include "src/lib/mapread.h"
2
3 int jsoneq(const char huge *json, jsmntok_t huge *tok, const char huge *s) {
4         if (tok->type == JSMN_STRING && (int)_fstrlen(s) == tok->end - tok->start &&
5                         _fstrncmp((char const *)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 huge *js, jsmntok_t huge *t, size_t count, word indent, char *js_sv, map_t *map, unsigned long q) {
13         unsigned long 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(_fstrstr(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)strtol(js+t->start, (char **)t->end, 10);
45                         map->data[q] = (byte)atoi((const char *)js+t->start);
46                         #ifdef DEBUG_MAPDATA
47                                 fprintf(stdout, "%d[%d]", q, map->data[q]);
48                         #endif
49                 }
50                 else
51                 if(_fstrstr(js_sv, "height"))
52                 {
53 //----                  map->height = (unsigned int)strtol(js+t->start, (char **)js+t->end, 10);
54                         map->height = atoi((const char *)js+t->start);
55                         #ifdef DEBUG_MAPVAR
56                         fprintf(stdout, "indent= [%d]   ", indent);
57                         fprintf(stdout, "h:[%d]\n", map->height);
58                         #endif
59                 }else if(_fstrstr(js_sv, "width"))
60                 {
61 //----                  map->width = (unsigned int)strtol(js+t->start, (char **)js+t->end, 10);
62                         map->width = atoi((const char *)js+t->start);
63                         #ifdef DEBUG_MAPVAR
64                         fprintf(stdout, "indent= [%d]   ", indent);
65                         fprintf(stdout, "w:[%d]\n", map->width);
66                         #endif
67                 }
68                 return 1;
69                 /* We may use strndup() to fetch string value */
70         } else if (t->type == JSMN_STRING) {
71                 if(jsoneq(js, t, "data") == 0)
72                 {
73 //                      fprintf(stdout, "[[[[%d|%d]]]]\n", &(t+1)->size, (t+1)->size);
74 //                      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);
75                         map->data = halloc(sizeof(byte) * (t+1)->size, sizeof(byte));
76                         //map->data = malloc(sizeof(byte) * (t+1)->size);
77                         map->tiles = /*_f*/malloc(sizeof(tiles_t));
78                         //map->tiles->data = malloc(sizeof(bitmap_t));
79                         //fix this to be far~
80 //++++                  bp = bitmapLoadPcx("data/ed.pcx");
81 //++++                  map->tiles->data = &bp;
82                         //map->tiles->data->data = malloc((16/**2*/)*16);
83                         //map->tiles->data->width = (16/**2*/);\r
84                         //map->tiles->data->height= 16;\r
85                         map->tiles->tileHeight = 16;\r
86                         map->tiles->tileWidth = 16;\r
87                         map->tiles->rows = 1;\r
88                         map->tiles->cols = 1;
89                         _fstrcpy(js_sv, "data");//strdup(js+t->start);//, t->end - t->start);
90                 }
91                 else
92                 if (jsoneq(js, t, "height") == 0 && indent<=1)
93                 {
94                         _fstrcpy(js_sv, "height");//strdup(js+t->start);//, t->end - t->start);
95                 }else
96                 if(jsoneq(js, t, "width") == 0 && indent<=1)
97                 {
98                         _fstrcpy(js_sv, "width");//strdup(js+t->start);//, t->end - t->start);
99                 }else _fstrcpy(js_sv, "\0");
100                 return 1;
101         } else if (t->type == JSMN_OBJECT) {
102                 //fprintf(stdout, "\n");
103                 j = 0;
104                 for (i = 0; i < t->size; i++) {
105                         //for (k = 0; k < indent; k++) fprintf(stdout, "\t");
106                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
107                         //fprintf(stdout, ": ");
108                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
109                         //fprintf(stdout, "\n");
110                 }
111                 return j+1;
112         } else if (t->type == JSMN_ARRAY) {
113                 j = 0;
114                 //fprintf(stdout, "==\n");
115                 for (i = 0; i < t->size; i++) {
116                         //for (k = 0; k < indent-1; k++) fprintf(stdout, "\t");
117                         //fprintf(stdout, "\t-");
118                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
119                         //fprintf(stdout, "==\n");
120                 }
121                 return j+1;
122         }
123         return 0;
124 }
125
126 int loadmap(char *mn, map_t *map)
127 {
128         int r;
129         static word incr=0;
130         int eof_expected = 0;
131         char huge *js = NULL;
132         size_t jslen = 0;
133         char buf[BUFSIZ];
134         char huge *buff = &buf;
135         static char js_ss[16];
136
137         jsmn_parser p;
138         jsmntok_t huge *tok;
139         size_t tokcount = 2;
140
141         FILE *fh = fopen(mn, "r");
142
143         /* Prepare parser */
144         jsmn_init(&p);
145
146         /* Allocate some tokens as a start */
147 //0000fprintf(stderr, "tok malloc\n");
148         tok = _fmalloc(sizeof(*tok) * tokcount);
149         if (tok == NULL) {
150                 fprintf(stderr, "malloc(): errno=%d\n", errno);
151                 return 3;
152         }
153
154         for (;;) {
155                 /* Read another chunk */
156 //0000fprintf(stderr, "read\n");
157                 r = fread(buf, 1, sizeof(buf), fh);
158                 if (r < 0) {
159                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
160                         return 1;
161                 }
162                 if (r == 0) {
163                         if (eof_expected != 0) {
164                                 return 0;
165                         } else {
166                                 fprintf(stderr, "fread(): unexpected EOF\n");
167                                 return 2;
168                         }
169                 }
170 //0000fprintf(stdout, "r=       [%d]    BUFSIZ=%d\n", r, BUFSIZ);
171 //0000fprintf(stderr, "js alloc~\n");
172                 js = _frealloc(js, jslen + r + 1);
173                 if (js == NULL) {
174                         fprintf(stderr, "*js=%Fp\n", *js);
175                         fprintf(stderr, "realloc(): errno = %d\n", errno);
176                         return 3;
177                 }
178                 _fstrncpy(js + jslen, &(*buff), r);
179                 jslen = jslen + r;
180
181 again:
182 //0000fprintf(stdout, " parse~ tok=%zu  jslen=%zu       r=%d    _memavl()=%u    BUFSIZ=%d~\n", tokcount, jslen, r, _memavl(), BUFSIZ);
183 //0000fprintf(stdout, "p=[%u]   [%u]    [%d]\n", p.pos, p.toknext, p.toksuper);
184 /*
185                 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
186 */
187 //++++          
188                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
189 //0000fprintf(stdout, "r=       [%d]\n", r);
190                 if (r < 0) {
191                         if (r == JSMN_ERROR_NOMEM) {
192                                 tokcount = tokcount * 2;
193 //0000fprintf(stderr, "tok realloc~ %zu\n", tokcount);
194                                 tok = _frealloc(tok, sizeof(*tok) * tokcount);
195                                 if (tok == NULL) {
196                                         fprintf(stderr, "realloc(): errno=%d\n", errno);
197                                         return 3;
198                                 }
199                                 goto again;
200                         }
201                 } else {
202                         //printf("js=%Fp\n", (js));
203                         //printf("*js=%Fp\n", (*(js)));
204                         printf("&*js=%s\n", &(*(js)));
205                         //printf("&buf=[%Fp]\n", &buf);
206                         //printf("&buf_seg=[%x]\n", FP_SEG(&buf));
207                         //printf("&buf_off=[%x]\n", FP_OFF(&buf));
208                         //printf("&buf_fp=[%Fp]\n", MK_FP(FP_SEG(&buf), FP_OFF(&buf)));
209                         //printf("buf=[\n%s\n]\n", buf);
210                         //printf("buff=[%Fp]\n", buff);
211                         //printf("(*buff)=[%Fp]\n", (*buff));
212                         //printf("&(*buff)=[\n%s\n]\n", &(*buff));
213                         #ifdef DEBUG_DUMPVARS
214                         fprintf(stdout, "running dump~\n");
215                         #endif
216 //++++
217                         dump(js, tok, p.toknext, incr, &js_ss, map, 0);
218                         eof_expected = 1;
219                 }
220         }
221
222         _ffree(js);
223         _ffree(buff);
224         _ffree(tok);
225         fclose(fh);
226
227         return 0;
228 }