-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_tokens.c
68 lines (62 loc) · 1.43 KB
/
print_tokens.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include "file.h"
#include "jsmnx.h"
//
void show_token(const char* js, jsmntok_t *token) {
if (token != NULL) {
printf("%.*s\n", token->end - token->start, js + token->start);
}
}
//
void help(const char *app) {
printf("Usage: %s [json-file]\n", app);
exit(EXIT_FAILURE);
}
//
int main(int argc, const char** argv) {
const char* filename;
FILE* fp;
char* input;
size_t input_length;
jsmntok_t *tokens, *token;
int ret;
int i;
//
if (argc == 0) { fp = stdin; }
else if (argc == 2) {
//
filename = argv[1];
fp = fopen(filename, "r");
if (!fp) {
printf("File not found: %s\n", filename);
exit(EXIT_FAILURE);
}
}
else { help(argv[0]); }
//
input_length = read_file(fp, &input);
if (fp != stdin) { fclose(fp); }
//
if (input_length == 0) {return 0;}
//
tokens = calloc(1, sizeof(jsmntok_t));
ret = jsmn_tokenise(input, &tokens);
//
if (ret < 0) {return 1;}
//
for (i = 0; i < ret; i++) {
token = &tokens[i];
printf("%d: [%d, %d), type: %d, size: %d\n", i,
token->start, token->end, token->type, token->size);
if (token->type == JSMN_OBJECT || token->type == JSMN_ARRAY) {
printf("[%d elems]\n", token->size);
}
show_token(input, token);
printf("\n");
}
//
jsmn_free_tokens(&tokens);
//
return 0;
}