]> 4ch.mooo.com Git - 16.git/blob - src/lib/mapread.c
853b809ac964dadc2aff79bd0f6246f0b47e532b
[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 int dump(const char huge *js, jsmntok_t huge *t, size_t count, int indent, char *js_sv, map_t *map, int q) {
13         int i, j, k;
14         bitmap_t bp;
15         #ifdef DEBUG_JS
16         if(indent==0)
17         {
18                 fprintf(stdout, "%s\n", js);
19                 fprintf(stdout, "\n");
20         }
21         #endif
22         #ifdef DEBUG_DUMPVARS
23         fprintf(stdout, "indent= [%d]   ", indent);
24         fprintf(stdout, "js_sv= [%s]\n", js_sv);
25         #endif
26         if (count == 0) {
27                 return 0;
28         }
29         /* We may want to do strtol() here to get numeric value */
30         if (t->type == JSMN_PRIMITIVE) {
31                 if(_fstrstr(js_sv, "data"))
32                 {
33                         /*
34                                 here we should recursivly call dump again here to skip over the array until we get the facking width of the map.
35                                 so we can initiate the map which allocates the facking map->tiles->data->data properly and THEN we can return
36                                 here to read the data.... That is my design for this... wwww
37
38                                 FUCK well i am stuck.... wwww
39                         */
40 //----                  map->data[q] = (byte)strtol(js+t->start, (char **)t->end, 10);
41                         map->data[q] = (byte)atoi((const char *)js+t->start);
42                         #ifdef DEBUG_MAPDATA
43                                 fprintf(stdout, "%d[%d]", q, map->data[q]);
44                         #endif
45                 }
46                 else
47                 if(_fstrstr(js_sv, "height"))
48                 {
49 //----                  map->height = (unsigned int)strtol(js+t->start, (char **)js+t->end, 10);
50                         map->height = atoi((const char *)js+t->start);
51                         #ifdef DEBUG_MAPVAR
52                         fprintf(stdout, "indent= [%d]   ", indent);
53                         fprintf(stdout, "h:[%d]\n", map->height);
54                         #endif
55                 }else if(_fstrstr(js_sv, "width"))
56                 {
57 //----                  map->width = (unsigned int)strtol(js+t->start, (char **)js+t->end, 10);
58                         map->width = atoi((const char *)js+t->start);
59                         #ifdef DEBUG_MAPVAR
60                         fprintf(stdout, "indent= [%d]   ", indent);
61                         fprintf(stdout, "w:[%d]\n", map->width);
62                         #endif
63                 }
64                 return 1;
65                 /* We may use strndup() to fetch string value */
66         } else if (t->type == JSMN_STRING) {
67                 if(jsoneq(js, t, "data") == 0)
68                 {
69 //                      fprintf(stdout, "[[[[%d|%d]]]]\n", &(t+1)->size, (t+1)->size);
70 //                      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);
71                         map->data = halloc(sizeof(byte) * (t+1)->size, sizeof(byte));
72                         //map->data = malloc(sizeof(byte) * (t+1)->size);
73                         map->tiles = /*_f*/malloc(sizeof(tiles_t));
74                         //map->tiles->data = malloc(sizeof(bitmap_t));
75                         //fix this
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*/);\r
80                         //map->tiles->data->height= 16;\r
81                         map->tiles->tileHeight = 16;\r
82                         map->tiles->tileWidth = 16;\r
83                         map->tiles->rows = 1;\r
84                         map->tiles->cols = 1;
85                         _fstrcpy(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                         fprintf(stdout, "height\n");
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                         fprintf(stdout, "width\n");
96                         _fstrcpy(js_sv, "width");//strdup(js+t->start);//, t->end - t->start);
97                 }else _fstrcpy(js_sv, "\0");
98                 return 1;
99         } else if (t->type == JSMN_OBJECT) {
100                 //fprintf(stdout, "\n");
101                 j = 0;
102                 for (i = 0; i < t->size; i++) {
103                         //for (k = 0; k < indent; k++) fprintf(stdout, "\t");
104                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
105                         //fprintf(stdout, ": ");
106                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
107                         //fprintf(stdout, "\n");
108                 }
109                 return j+1;
110         } else if (t->type == JSMN_ARRAY) {
111                 j = 0;
112                 //fprintf(stdout, "==\n");
113                 for (i = 0; i < t->size; i++) {
114                         //for (k = 0; k < indent-1; k++) fprintf(stdout, "\t");
115                         //fprintf(stdout, "\t-");
116                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
117                         //fprintf(stdout, "==\n");
118                 }
119                 return j+1;
120         }
121         return 0;
122 }
123
124 int loadmap(char *mn, map_t *map)
125 {
126         int r;
127         static int incr=0;
128         int eof_expected = 0;
129         //char *jz = NULL;
130         char huge *js = NULL;
131         size_t jslen = 0;
132         char buf[BUFSIZ];
133         char huge *buff = &buf;//(char huge *)(MK_FP(FP_SEG(&buf), FP_OFF(&buf)));//
134         static char js_ss[16];
135
136         jsmn_parser p;
137         jsmntok_t huge *tok;
138         size_t tokcount = 2;
139
140         FILE *fh = fopen(mn, "r");
141
142         /* Prepare parser */
143         jsmn_init(&p);
144
145         /* Allocate some tokens as a start */
146         tok = _fmalloc(sizeof(*tok) * tokcount);
147         if (tok == NULL) {
148                 fprintf(stderr, "malloc(): errno=%d\n", errno);
149                 return 3;
150         }
151
152         for (;;) {
153                 /* Read another chunk */
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                 //buf[BUFSIZ]='\0';
168                 js = _frealloc(js, jslen + r + 1);
169                 if (js == NULL) {
170                         fprintf(stderr, "*js=%Fp\n", *js);
171                         fprintf(stderr, "realloc(): errno = %d\n", errno);
172                         return 3;
173                 }
174                 //printf("strncpy~\n");
175                 //strncpy(jz + jslen, buf, r);
176                 //if(
177                 //_fstrncpy(js + jslen, &(*buff), r);
178                 _fstrncpy(js + jslen, &(*buff), r);
179                 //strncpy((char *)js + jslen, buf, r);
180                 // == NULL)
181 //                      fprintf(stderr, "_fstrncpy(): errno = %d\n", errno);
182                 //printf("strncpy okies~~\n");
183                 jslen = jslen + r;
184
185 again:
186                 //printf("(*js)=%Fp\n", (*js));
187                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
188                 if (r < 0) {
189                         if (r == JSMN_ERROR_NOMEM) {
190                                 tokcount = tokcount * 2;
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                         dump(js, tok, p.toknext, incr, &js_ss, map, 0);
211                         eof_expected = 1;
212                 }
213         }
214
215         _ffree(js);
216         _ffree(buff);
217         _ffree(tok);
218         fclose(fh);
219
220         return 0;
221 }