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