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