]> 4ch.mooo.com Git - 16.git/blob - src/lib/jsmn/example/maptest2.c
deleted: src/lib/jsmn/example/MAPTEST2.EXE
[16.git] / src / lib / jsmn / example / maptest2.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; /* We expect no more than 128 tokens */
42         size_t tokcount = 2;
43         /* Allocate some tokens as a start */
44         t = malloc(sizeof(*t) * tokcount);
45         char JSON_S[BUFSIZ];
46         memset(JSON_S, 0, sizeof(JSON_S));
47
48         if(fh != NULL)
49         {
50                 fread(JSON_S, sizeof(char), filesize(fh), fh);
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, filesize(fh), t, sizeof(t)/sizeof(t[0]));
58                 fclose(fh); fh = NULL;
59         printf("%s", JSON_STRING);
60         if (r < 0) {
61                 printf("Failed to parse JSON: %d\n", r);
62                 return 1;
63         }
64
65         /* Assume the top-level element is an object */
66         if (r < 1 || t[0].type != JSMN_OBJECT) {
67                 printf("Object expected\n");
68                 return 1;
69         }
70
71         /* Loop over all keys of the root object */
72         for (i = 1; i < r; i++) {
73                 if (jsoneq(JSON_STRING, &t[i], "image") == 0) {
74                         /* We may use strndup() to fetch string value */
75                         printf("- image: %.*s\n", t[i+1].end-t[i+1].start,
76                                         JSON_STRING + t[i+1].start);
77                         i++;
78                 } else if (jsoneq(JSON_STRING, &t[i], "admin") == 0) {
79                         /* We may additionally check if the value is either "true" or "false" */
80                         printf("- Admin: %.*s\n", t[i+1].end-t[i+1].start,
81                                         JSON_STRING + t[i+1].start);
82                         i++;
83                 } else if (jsoneq(JSON_STRING, &t[i], "uid") == 0) {
84                         /* We may want to do strtol() here to get numeric value */
85                         printf("- UID: %.*s\n", t[i+1].end-t[i+1].start,
86                                         JSON_STRING + t[i+1].start);
87                         i++;
88                 } else if (jsoneq(JSON_STRING, &t[i], "tilesets") == 0) {
89                         int j;
90                         printf("- tilesets:\n");
91                         if (t[i+1].type != JSMN_ARRAY) {
92                                 continue; /* We expect groups to be an array of strings */
93                         }
94                         for (j = 0; j < t[i+1].size; j++) {
95                                 jsmntok_t *g = &t[i+j+2];
96                                 printf("  * %.*s\n", g->end - g->start, JSON_STRING + g->start);
97                         }
98                         i += t[i+1].size + 1;
99                 } else {
100                         printf("Unexpected key: %.*s\n", t[i].end-t[i].start,
101                                         JSON_STRING + t[i].start);
102                 }
103         }
104
105         //free(JSON_STRING);
106         }
107         if (fh != NULL) fclose(fh);
108   ////}
109         return 0;
110 }