6 #include "../../../lib/types.h"
9 * An example of reading JSON from stdin and printing its content to stdout.
10 * The output looks like YAML, but I'm not sure if it's really compatible.
31 static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
32 if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
33 strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
39 static int dump(const char *js, jsmntok_t *t, size_t count, int indent, /*char *js_sv,*/ map_t *map) {
44 /* We may want to do strtol() here to get numeric value */
45 if (t->type == JSMN_PRIMITIVE) {
48 map->height = (int)strtol(js+t->start, (char **)js+t->end, 10);
49 printf("h:[%d]\n", map->height);
50 }else if(js_sv == "width")
52 map->width = (int)strtol(js+t->start, (char **)js+t->end, 10);
53 printf("w:[%d]\n", map->width);
56 /* We may use strndup() to fetch string value */
57 } else if (t->type == JSMN_STRING) {
58 //printf("'%.*s'", t->end - t->start, js+t->start);
59 if (jsoneq(js, t, "height") == 0 && indent==1)
61 js_sv="height";//strdup(js+t->start);//, t->end - t->start);
62 printf("%s\n", js_sv);
63 }else if (jsoneq(js, t, "width") == 0 && indent==1)
65 js_sv="width";//strdup(js+t->start);//, t->end - t->start);
66 printf("%s\n", js_sv);
69 } else if (t->type == JSMN_OBJECT) {
72 for (i = 0; i < t->size; i++) {
73 //for (k = 0; k < indent; k++) printf("\t");
74 j += dump(js, t+1+j, count-j, indent+1, map);
76 j += dump(js, t+1+j, count-j, indent+1, map);
80 } else if (t->type == JSMN_ARRAY) {
83 for (i = 0; i < t->size; i++) {
84 //for (k = 0; k < indent-1; k++) printf("\t");
86 j += dump(js, t+1+j, count-j, indent+1, map);
94 int loadmap(char *mn, map_t *map)
106 FILE *fh = fopen(mn, "r");
111 /* Allocate some tokens as a start */
112 tok = malloc(sizeof(*tok) * tokcount);
114 fprintf(stderr, "malloc(): errno=%d\n", errno);
119 /* Read another chunk */
120 r = fread(buf, 1, sizeof(buf), fh);
122 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
126 if (eof_expected != 0) {
129 fprintf(stderr, "fread(): unexpected EOF\n");
134 js = realloc(js, jslen + r + 1);
136 fprintf(stderr, "realloc(): errno=%d\n", errno);
139 strncpy(js + jslen, buf, r);
143 r = jsmn_parse(&p, js, jslen, tok, tokcount);
145 if (r == JSMN_ERROR_NOMEM) {
146 tokcount = tokcount * 2;
147 tok = realloc(tok, sizeof(*tok) * tokcount);
149 fprintf(stderr, "realloc(): errno=%d\n", errno);
155 dump(js, tok, p.toknext, 0, map);
156 //fprintf(stdout, "[[[[%d]]]]\n", sizeof(tok));
157 //printf("[\n%d\n]", jslen);
168 loadmap("../../../../data/test.map", &map);