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