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