]> 4ch.mooo.com Git - 16.git/blob - src/lib/tst.c
See WHAT_WAS_CHANGED for details
[16.git] / src / lib / tst.c
1 #include "stdio.h"
2 #include "stdlib.h"
3 #include "jsmn/jsmn.h"
4
5 static int dump(const char *js, jsmntok_t *t, size_t count, int indent) {
6         int i, j, k;
7         if (count == 0) {
8                 return 0;
9         }
10         if (t->type == JSMN_PRIMITIVE) {
11                 printf("%.*s", t->end - t->start, js+t->start);
12                 return 1;
13         } else if (t->type == JSMN_STRING) {
14                 printf("'%.*s'", t->end - t->start, js+t->start);
15                 return 1;
16         } else if (t->type == JSMN_OBJECT) {
17                 printf("\n");
18                 j = 0;
19                 for (i = 0; i < t->size; i++) {
20                         for (k = 0; k < indent; k++) printf("  ");
21                         j += dump(js, t+1+j, count-j, indent+1);
22                         printf(": ");
23                         j += dump(js, t+1+j, count-j, indent+1);
24                         printf("\n");
25                 }
26                 return j+1;
27         } else if (t->type == JSMN_ARRAY) {
28                 j = 0;
29                 printf("\n");
30                 for (i = 0; i < t->size; i++) {
31                         for (k = 0; k < indent-1; k++) printf("  ");
32                         printf("   - ");
33                         j += dump(js, t+1+j, count-j, indent+1);
34                         printf("\n");
35                 }
36                 return j+1;
37         }
38         return 0;
39 }
40
41 long int
42 filesize(FILE *fp)
43 {
44         long int save_pos, size_of_file;
45
46         save_pos = ftell(fp);
47         fseek(fp, 0L, SEEK_END);
48         size_of_file = ftell(fp);
49         fseek(fp, save_pos, SEEK_SET);
50         return(size_of_file);
51 }
52
53
54 int loadmap(char *mn/*, map_t *map*/)
55 {
56         char *js;
57         char js_ss[16];
58
59         jsmn_parser p;
60         jsmntok_t *tok = NULL;
61         size_t tokcount, file_s;
62
63         FILE *fh = fopen(mn, "r");
64
65         /* Prepare parser */
66         jsmn_init(&p);
67
68         file_s = filesize(fh);
69         js = malloc(file_s);
70         if(js == NULL){
71                 fprintf(stderr, "malloc(): errno = %d", 2);
72                 fclose(fh);
73                 return 3;
74         }
75         if(fread(js, 1, file_s, fh) != file_s){
76                 fprintf(stderr, "Map read error");
77                 free(js);
78                 fclose(fh);
79                 return 1;
80         }
81
82         tokcount = jsmn_parse(&p, js, file_s, NULL, 0);
83         tok = malloc(tokcount*sizeof(jsmntok_t));
84         jsmn_parse(&p, js, file_s, tok, tokcount);
85         #ifdef DEBUG_DUMPVARS
86         fprintf(stdout, "running dump~\n");
87         #endif
88         dump(js, tok, p.toknext, 0);
89
90         free(js);
91         free(tok);
92         fclose(fh);
93         
94         return 0;
95 }
96
97 int main()
98 {
99         puts("3");
100         loadmap("../../data/0es0.map");
101         return 0;
102 }