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