]> 4ch.mooo.com Git - 16.git/blob - src/lib/jsmn/example/maptest.c
1a3416cc31f9854e6731f4d0a4b66c7203e690a1
[16.git] / src / lib / jsmn / example / maptest.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "../jsmn.c"
5
6 /*
7  * A small example of jsmn parsing when JSON structure is known and number of
8  * tokens is predictable.
9  */
10
11 /*char *json_string =
12         "{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n  "
13         "\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}";*/
14
15 long int filesize(FILE *fp)\r
16 {\r
17         long int save_pos, size_of_file;\r
18 \r
19         save_pos = ftell(fp);\r
20         fseek(fp, 0L, SEEK_END);\r
21         size_of_file = ftell(fp);\r
22         fseek(fp, save_pos, SEEK_SET);\r
23         return(size_of_file);\r
24 }
25
26 static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
27         if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
28                         strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
29                 return 0;
30         }
31         return -1;
32 }
33
34 int main() {
35         int i;
36         int r;
37         size_t z;
38         jsmn_parser p;
39         jsmntok_t t[128]; /* We expect no more than 128 tokens */
40         FILE *fh = fopen("../../../../data/test0.map", "r");
41         char *json_string = malloc(filesize(fh));
42         //memset(json_string, 0, sizeof(*json_string));
43         memset(&p, 0, sizeof(p));
44
45         if(fh != NULL)
46         {
47                 z = fread(json_string, 1, filesize(fh), fh);
48                 fclose(fh); fh = NULL;
49                 printf("[[%d]]\n", z);
50                 json_string[z] = '\0';
51                 // we can now close the file
52                 //printf("]%s[\n", json_s);
53                 //json_string=json_s;
54                 //printf("[[%s]]\n", json_string);
55
56         jsmn_init(&p);
57         r = jsmn_parse(&p, json_string, strlen(json_string), t, sizeof(t)/sizeof(t[0]));
58         printf("[\n%s\n]", json_string);
59         if (r < 0) {
60                 printf("Failed to parse JSON: %d\n", r);
61                 return 1;
62         }
63
64         /* Assume the top-level element is an object */
65         if (r < 1 || t[0].type != JSMN_OBJECT) {
66                 printf("Object expected\n");
67                 return 1;
68         }
69
70         /* Loop over all keys of the root object */
71         for (i = 1; i < r; i++) {
72                 if (jsoneq(json_string, &t[i], "image") == 0) {
73                         /* We may use strndup() to fetch string value */
74                         printf("- image: %.*s\n", t[i+1].end-t[i+1].start,
75                                         json_string + t[i+1].start);
76                         i++;
77                 } else if (jsoneq(json_string, &t[i], "admin") == 0) {
78                         /* We may additionally check if the value is either "true" or "false" */
79                         printf("- Admin: %.*s\n", t[i+1].end-t[i+1].start,
80                                         json_string + t[i+1].start);
81                         i++;
82                 } else if (jsoneq(json_string, &t[i], "uid") == 0) {
83                         /* We may want to do strtol() here to get numeric value */
84                         printf("- UID: %.*s\n", t[i+1].end-t[i+1].start,
85                                         json_string + t[i+1].start);
86                         i++;
87                 } else if (jsoneq(json_string, &t[i], "tilesets") == 0) {
88                         int j;
89                         printf("- tilesets:\n");
90                         if (t[i+1].type != JSMN_ARRAY) {
91                                 continue; /* We expect groups to be an array of strings */
92                         }
93                         for (j = 0; j < t[i+1].size; j++) {
94                                 jsmntok_t *g = &t[i+j+2];
95                                 printf("  * %.*s\n", g->end - g->start, json_string + g->start);
96                         }
97                         i += t[i+1].size + 1;
98                 } else {
99                         printf("Unexpected key: %.*s\n", t[i].end-t[i].start,
100                                         json_string + t[i].start);
101                 }
102         }
103
104         //free(json_string);
105         }
106         if (fh != NULL) fclose(fh);
107   ////}
108         return 0;
109 }