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