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