Merge: lib/config: fix doc
[nit.git] / benchmarks / json / scripts / c_parser.c
1 #include "ujdecode.c"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <assert.h>
9
10 // Gets the byte size of a file
11 int file_byte_size(char* path)
12 {
13 int f = open(path, O_RDONLY);
14 if(f == -1)
15 return -1;
16 struct stat s;
17 int ln = 0;
18 if(!fstat(f, &s))
19 ln = s.st_size;
20 close(f);
21 return ln;
22 }
23
24 int main(int argc, char** argv)
25 {
26 if(argc == 1) {
27 printf("Usage: ./c_parser file\n");
28 exit(1);
29 }
30
31 int fl_sz = file_byte_size(argv[1]);
32 char* input = malloc(fl_sz);
33
34 FILE* ifl = fopen(argv[1], "r");
35 if(ifl == NULL) {
36 printf("Error: cannot read file %s, are you sure permissions are set correctly ?\n", argv[1]);
37 exit(2);
38 }
39 int rd = fread(input, 1, fl_sz, ifl);
40 assert(rd == fl_sz);
41
42 void *state;
43
44 UJObject obj = UJDecode(input, fl_sz, NULL, &state);
45
46 free(input);
47 UJFree(state);
48 }