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