]> 4ch.mooo.com Git - 16.git/blob - src/lib/mapread.c
modified: makefile
[16.git] / src / lib / mapread.c
1 #include "src/lib/mapread.h"
2
3 static 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 static int dump(const char *js, jsmntok_t *t, size_t count, int indent, /*char *js_sv,*/ map_t *map, int q/*, int *w*/) {
13         int i, j, k;
14         if (count == 0) {
15                 return 0;
16         }
17         /* We may want to do strtol() here to get numeric value */
18         if (t->type == JSMN_PRIMITIVE) {
19                 //if(w)
20                 if(js_sv == "data")
21                 {
22                         /*
23                                 here we should recursivly call dump again here to skip over the array until we get the facking width of the map.
24                                 so we can initiate the map which allocates the facking map->tiles->data->data properly and THEN we can return
25                                 here to read the data.... That is my design for this... wwww
26
27                                 FUCK well i am stuck.... wwww
28                         */
29                         //bgdata[q] = strtol(js+t->start, (char **)js+t->end, 10);
30                         //if(strtol(js+t->start, (char **)js+t->end, 10)==0){ /*printf("%d\n", sizeof(map->tiles->data->data));*/ fprintf(stderr, "\nFACK! %d\n", errno); exit(-1); }
31 //                      map->tiles->data->data[q] = (byte)strtol(js+t->start, (char **)js+t->end, 10);
32 //                      printf("%d[%d]", q, (byte)strtol(js+t->start, (char **)js+t->end, 10));
33                 }
34                 else
35                 if(js_sv == "height")
36                 {
37                         map->height = (int)strtol(js+t->start, (char **)js+t->end, 10);
38                         printf("h:[%d]\n", map->height);
39                 }else if(js_sv == "width")
40                 {
41                         map->width = (int)strtol(js+t->start, (char **)js+t->end, 10);
42                         printf("w:[%d]\n", map->width);
43                 }
44                 return 1;
45                 /* We may use strndup() to fetch string value */
46         } else if (t->type == JSMN_STRING) {
47                 //printf("'%.*s'", t->end - t->start, js+t->start);
48                 //if(w)
49                 if(jsoneq(js, t, "data") == 0 )
50                 {
51 //                      printf("[[[[%d|%d]]]]\n", &(t+1)->size, (t+1)->size);
52                         printf("\n%.*s[xx[%d|%d]xx]\n", (t+1)->end - (t+1)->start, js+(t+1)->start, &(t+1)->size, (t+1)->size);
53                         map->data = malloc(sizeof(byte) * (t+1)->size +1);
54                         map->tiles = malloc(sizeof(tiles_t));
55                         map->tiles->data = malloc(sizeof(bitmap_t));
56                         map->tiles->data->width = (16*2);
57                         map->tiles->data->height= 16;
58                         map->tiles->data->data = malloc((16*2)*16);
59                         js_sv="data";//strdup(js+t->start);//, t->end - t->start);
60                         //printf("%s\n", js_sv);
61                 }
62                 else
63                 if (jsoneq(js, t, "height") == 0 && indent==1)
64                 {
65                         js_sv="height";//strdup(js+t->start);//, t->end - t->start);
66                         //printf("%s\n", js_sv);
67                 }else if (jsoneq(js, t, "width") == 0 && indent==1)
68                 {
69                         js_sv="width";//strdup(js+t->start);//, t->end - t->start);
70                         //printf("%s\n", js_sv);
71                 }else js_sv=NULL;
72                 return 1;
73         } else if (t->type == JSMN_OBJECT) {
74                 //printf("\n");
75                 j = 0;
76                 for (i = 0; i < t->size; i++) {
77                         //for (k = 0; k < indent; k++) printf("\t");
78                         j += dump(js, t+1+j, count-j, indent+1, map, i/*, w*/);
79                         //printf(": ");
80                         j += dump(js, t+1+j, count-j, indent+1, map, i/*, w*/);
81                         //printf("\n");
82                 }
83                 return j+1;
84         } else if (t->type == JSMN_ARRAY) {
85                 j = 0;
86                 //printf("==\n");
87                 for (i = 0; i < t->size; i++) {
88                         //if(bgdata==NULL) bgdata=malloc(sizeof(char)*t->size);
89                         //for (k = 0; k < indent-1; k++) printf("\t");
90                         //printf("\t-");
91                         j += dump(js, t+1+j, count-j, indent+1, map, i/*, &t->size*/);
92                         //printf("==\n");
93                 }
94                 return j+1;
95         }
96         return 0;
97 }
98
99 static int loadmap(char *mn, map_t *map/*, word w*/)
100 {
101         int r;
102         int eof_expected = 0;
103         char *js = NULL;
104         size_t jslen = 0;
105         char buf[BUFSIZ];
106
107         jsmn_parser p;
108         jsmntok_t *tok;
109         size_t tokcount = 2;
110
111         FILE *fh = fopen(mn, "r");
112
113         /* Prepare parser */
114         jsmn_init(&p);
115
116         /* Allocate some tokens as a start */
117         tok = malloc(sizeof(*tok) * tokcount);
118         if (tok == NULL) {
119                 fprintf(stderr, "malloc(): errno=%d\n", errno);
120                 return 3;
121         }
122
123         for (;;) {
124                 /* Read another chunk */
125                 r = fread(buf, 1, sizeof(buf), fh);
126                 if (r < 0) {
127                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
128                         return 1;
129                 }
130                 if (r == 0) {
131                         if (eof_expected != 0) {
132                                 return 0;
133                         } else {
134                                 fprintf(stderr, "fread(): unexpected EOF\n");
135                                 return 2;
136                         }
137                 }
138
139                 js = realloc(js, jslen + r + 1);
140                 if (js == NULL) {
141                         fprintf(stderr, "realloc(): errno = %d\n", errno);
142                         return 3;
143                 }
144                 strncpy(js + jslen, buf, r);
145 //              printf("%s\n\n", js);
146                 jslen = jslen + r;
147
148 again:
149                 //printf("================================================================================%s================================================================================", js);
150                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
151                 //printf("================================================================================%s================================================================================", js);
152                 if (r < 0) {
153                         if (r == JSMN_ERROR_NOMEM) {
154                                 tokcount = tokcount * 2;
155                                 tok = realloc(tok, sizeof(*tok) * tokcount);
156                                 if (tok == NULL) {
157                                         fprintf(stderr, "realloc(): errno=%d\n", errno);
158                                         return 3;
159                                 }
160                                 goto again;
161                         }
162                 } else {
163                         //printf("================================================================================%s================================================================================", js);
164                         dump(js, tok, p.toknext, 0, map, 0);
165                         eof_expected = 1;
166                 }
167         }
168
169         //free(js);
170         //free(tok);
171         fclose(fh);
172
173         return 0;
174 }