]> 4ch.mooo.com Git - 16.git/blob - src/lib/jsmn/example/maptest.c
modified: data/test0.map
[16.git] / src / lib / jsmn / 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 /*
8  * A small example of jsmn parsing when JSON structure is known and number of
9  * tokens is predictable.
10  */
11
12 /*char *json_string =
13         "{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n  "
14         "\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}";*/
15
16 long int filesize(FILE *fp)\r
17 {\r
18         long int save_pos, size_of_file;\r
19 \r
20         save_pos = ftell(fp);\r
21         fseek(fp, 0L, SEEK_END);\r
22         size_of_file = ftell(fp);\r
23         fseek(fp, save_pos, SEEK_SET);\r
24         return(size_of_file);\r
25 }
26
27 static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
28         if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
29                         strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
30                 return 0;
31         }
32         return -1;
33 }
34
35 int main() {
36         int i;
37         int r;
38         size_t z;
39         jsmn_parser p;
40         jsmntok_t t[(BUFSIZ/sizeof(jsmntok_t))*2]; /* We expect no more than 128 tokens */
41         FILE *fh = fopen("../../../../data/test0.map", "r");
42         char *json_string = malloc(filesize(fh));
43         //memset(json_string, 0, sizeof(*json_string));
44         //memset(&p, 0, sizeof(p));
45
46         //printf("\n[[[[%d]]]]\n\n", BUFSIZ);
47
48         if(fh != NULL)
49         {
50         /*t = malloc(2048);
51         if (t == NULL) {
52                 fprintf(stderr, "malloc(): errno=%d\n", errno);
53                 return 3;
54         }*/
55                 //printf("\n%d\n\n", sizeof(*t));
56                 printf("\n%d", sizeof(t));
57                 printf("\n%d\n\n", sizeof(t)/sizeof(t[0]));
58                 z = fread(json_string, 1, filesize(fh), fh);
59                 //char json_s[2048];
60                 fclose(fh); fh = NULL;
61                 printf("[%d]\n", z);
62                 json_string[z] = '\0';
63                 // we can now close the file
64                 //printf("]%s[\n", json_s);
65                 //json_string=json_s;
66                 //printf("[[%s]]\n", json_string);
67
68         jsmn_init(&p);
69         r = jsmn_parse(&p, json_string, strlen(json_string), t, sizeof(t)/sizeof(t[0]));
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 }