]> 4ch.mooo.com Git - 16.git/blob - src/lib/mapread.c
36169f951ecfb0acfff4cfe1fdf7733d7cf337dc
[16.git] / src / lib / mapread.c
1 #include "src/lib/mapread.h"
2
3 int jsoneq(const char /*far*/ *json, jsmntok_t /*far*/ *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 int dump(const char /*far*/ *js, jsmntok_t /*far*/ *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(strstr(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(js+t->start);
42                         #ifdef DEBUG_MAPDATA
43                                 fprintf(stdout, "%d[%d]", q, map->data[q]);
44                         #endif
45                 }
46                 else
47                 if(strstr(js_sv, "height"))
48                 {
49 //----                  map->height = (unsigned int)strtol(js+t->start, (char **)js+t->end, 10);
50                         map->height = (unsigned int)atoi(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(strstr(js_sv, "width"))
56                 {
57 //----                  map->width = (unsigned int)strtol(js+t->start, (char **)js+t->end, 10);
58                         map->width = (unsigned int)atoi(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 = _fmalloc(sizeof(byte) * (t+1)->size);
72                         map->tiles = /*_f*/malloc(sizeof(tiles_t));
73                         //map->tiles->data = malloc(sizeof(bitmap_t));
74                         //fix this
75                         bp = bitmapLoadPcx("data/ed.pcx");
76                         map->tiles->data = &bp;
77                         //map->tiles->data->data = malloc((16/**2*/)*16);
78                         //map->tiles->data->width = (16/**2*/);\r
79                         //map->tiles->data->height= 16;\r
80                         map->tiles->tileHeight = 16;\r
81                         map->tiles->tileWidth = 16;\r
82                         map->tiles->rows = 1;\r
83                         map->tiles->cols = 1;
84                         strcpy(js_sv, "data");//strdup(js+t->start);//, t->end - t->start);
85                 }
86                 else
87                 if (jsoneq(js, t, "height") == 0 && indent<=1)
88                 {
89                         strcpy(js_sv, "height");//strdup(js+t->start);//, t->end - t->start);
90                 }else
91                 if(jsoneq(js, t, "width") == 0 && indent<=1)
92                 {
93                         strcpy(js_sv, "width");//strdup(js+t->start);//, t->end - t->start);
94                 }else strcpy(js_sv, "\0");
95                 return 1;
96         } else if (t->type == JSMN_OBJECT) {
97                 //fprintf(stdout, "\n");
98                 j = 0;
99                 for (i = 0; i < t->size; i++) {
100                         //for (k = 0; k < indent; k++) fprintf(stdout, "\t");
101                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
102                         //fprintf(stdout, ": ");
103                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
104                         //fprintf(stdout, "\n");
105                 }
106                 return j+1;
107         } else if (t->type == JSMN_ARRAY) {
108                 j = 0;
109                 //fprintf(stdout, "==\n");
110                 for (i = 0; i < t->size; i++) {
111                         //for (k = 0; k < indent-1; k++) fprintf(stdout, "\t");
112                         //fprintf(stdout, "\t-");
113                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
114                         //fprintf(stdout, "==\n");
115                 }
116                 return j+1;
117         }
118         return 0;
119 }
120
121 int loadmap(char *mn, map_t *map)
122 {
123         int r;
124         static int incr=0;
125         int eof_expected = 0;
126         char /*far*/ *js = NULL;
127         size_t jslen = 0;
128         char buf[BUFSIZ];
129         static char js_ss[16];
130
131         jsmn_parser p;
132         jsmntok_t /*far*/ *tok;
133         size_t tokcount = 2;
134
135         FILE *fh = fopen(mn, "r");
136
137         /* Prepare parser */
138         jsmn_init(&p);
139
140         /* Allocate some tokens as a start */
141         tok = /*_f*/malloc(sizeof(*tok) * tokcount);
142         if (tok == NULL) {
143                 fprintf(stderr, "malloc(): errno=%d\n", errno);
144                 return 3;
145         }
146
147         for (;;) {
148                 /* Read another chunk */
149                 r = fread(buf, 1, sizeof(buf), fh);
150                 if (r < 0) {
151                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
152                         return 1;
153                 }
154                 if (r == 0) {
155                         if (eof_expected != 0) {
156                                 return 0;
157                         } else {
158                                 fprintf(stderr, "fread(): unexpected EOF\n");
159                                 return 2;
160                         }
161                 }
162
163                 js = /*_f*/realloc(js, jslen + r + 1);
164                 if (js == NULL) {
165                         fprintf(stderr, "realloc(): errno = %d\n", errno);
166                         return 3;
167                 }
168                 strncpy(js + jslen, buf, r);
169                 jslen = jslen + r;
170
171 again:
172                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
173                 if (r < 0) {
174                         if (r == JSMN_ERROR_NOMEM) {
175                                 tokcount = tokcount * 2;
176                                 tok = /*_f*/realloc(tok, sizeof(*tok) * tokcount);
177                                 if (tok == NULL) {
178                                         fprintf(stderr, "realloc(): errno=%d\n", errno);
179                                         return 3;
180                                 }
181                                 goto again;
182                         }
183                 } else {
184                         dump(js, tok, p.toknext, incr, &js_ss, map, 0);
185                         eof_expected = 1;
186                 }
187         }
188
189         /*_f*/free(js);
190         /*_f*/free(tok);
191         fclose(fh);
192
193         return 0;
194 }