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