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