-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.c
74 lines (60 loc) · 1.81 KB
/
json.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
69
70
71
72
73
74
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "args.h"
#include "hashmap.h"
#include "tokenize.h"
#include "parse.h"
#include "serialize.h"
#include "dynarray2.h"
#include "read_file.h"
int main(int argc, char** argv)
{
clock_t start, end;
json_args_t args;
if (get_args(&args, argc, argv) == 1) {
print_help();
exit(EXIT_FAILURE);
}
start = clock();
text_file_t* input = read_text_file(args.input_file);
if (input->error != 0) {
fprintf(stderr, "Couldn't read file, error = %s\n", get_file_error(input->error));
exit(EXIT_FAILURE);
}
end = clock();
printf("Reading file took: %f ms\n", ((double) end - start) / CLOCKS_PER_SEC * 1000);
printf("Input file length = %zu\n", input->buffer_len);
start = clock();
dynarray2_t* tokens = tokenize(input->buffer);
end = clock();
printf("Tokenization took: %f ms\n", ((double) end - start) / CLOCKS_PER_SEC * 1000);
printf("\n\ntoken list length = %zu\n", tokens->len);
// if (tokens->len > 0) {
// printf("TOKENS:\n\n");
// for (size_t i = 0; i < tokens->len; i++) {
// DEBUG_print_token(dynarray2_get(tokens, i));
// }
// printf("\n");
// }
start = clock();
node_t* root = parse(tokens, input->buffer);
end = clock();
printf("Parsing took: %f ms\n", ((double) end - start) / CLOCKS_PER_SEC * 1000);
dynarray2_free(tokens);
printf("Freed tokens...\n");
start = clock();
char* serialized = serialize(root);
end = clock();
printf("Serialization took: %f ms\n", ((double) end - start) / CLOCKS_PER_SEC * 1000);
// printf("\n\nSERIALIZED:\n");
// printf("%s\n", serialized);
FILE* fout = NULL;
fout = fopen(args.output_file, "w");
if (fout == NULL) {
die("couldn't open %s for writing", args.output_file);
}
fputs(serialized, fout);
fclose(fout);
return EXIT_SUCCESS;
}