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