-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
164 lines (155 loc) · 4.12 KB
/
main.c
File metadata and controls
164 lines (155 loc) · 4.12 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "./include/gson.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define PARSING_TIMER(source) \
do { \
clock_t t; \
Parser *__p = parser_init(source); \
t = clock(); \
JSONNode *__n = gson_parse(__p, NULL); \
t = clock() - t; \
double time_taken = ((double)t)/CLOCKS_PER_SEC; \
if (__n != NULL) \
{ \
printf("parsing took %f seconds to execute \n", time_taken); \
} \
gson_destroy(__n); \
parser_destroy(__p); \
break; \
} while (1); \
int main(int argc, char* argv[])
{
if (argc != 2)
{
printf("Usage: ./gson <input.json>");
return 1;
}
char *source = NULL;
FILE *fp = fopen(argv[1], "r");
if (fp != NULL)
{
if (fseek(fp, 0L, SEEK_END) == 0)
{
long bufsize = ftell(fp);
if (bufsize == -1)
{
return 1;
}
source = malloc(sizeof(char) * (bufsize + 1));
if (source == NULL)
{
perror("Memory allocation failed");
return 1;
}
if (fseek(fp, 0L, SEEK_SET) != 0)
{
return 1;
}
size_t new_len = fread(source, sizeof(char), bufsize, fp);
if (ferror(fp) != 0)
{
fputs("Error reading file", stderr);
}
else
{
source[new_len++] = '\0';
}
}
fclose(fp);
}
PARSING_TIMER(source);
// Parser *parser = parser_init(source);
// JSONNode *node = gson_parse(parser, NULL);
// if (node == NULL)
// {
// free(source);
// parser_destroy(parser);
// gson_destroy(node);
// return 1; // failed to parse
// }
// //
// // SOME TESTING
// JSONNode *key = gson_find_by_key(node, "\"gender\"");
// if (key != NULL)
// {
// printf("1Found: %s\n", gson_get_key_value(key));
// } else {
// printf("1Key not found: gender\n");
// }
//
// key = gson_find_by_key(key, "\"gender\"");
// if (key != NULL)
// {
// printf("Key found: %s\n", gson_get_key_value(key));
// } else {
// printf("Key not found: gender\n");
// }
//
//
// JSONNode *val = gson_find_by_str_val(node, "\"male\"");
// if (val != NULL)
// {
// printf("Val found: %s\n", gson_get_str_value(val));
// } else {
// printf("Val not found: male\n");
// }
// val = gson_find_by_str_val(val, "\"male\"");
// if (val != NULL)
// {
// printf("Val found: %s\n", gson_get_str_value(val));
// } else {
// printf("Val not found: male\n");
// }
//
// JSONNode *_float = gson_find_by_float_val(node, 30);
// if (_float != NULL)
// {
// printf("Val found: %s:%f\n", gson_get_key_value(_float), gson_get_float_value(_float));
// } else {
// printf("Val not found: 30.00\n");
// }
//
// _float = gson_find_by_float_val(_float, 30);
// if (_float != NULL)
// {
// printf("Val found: %s:%f\n", gson_get_key_value(_float), gson_get_float_value(_float));
// } else {
// printf("Val not found: 30.00\n");
// }
//
// _float = gson_find_by_float_val(_float, 30);
// if (_float != NULL)
// {
// printf("Val found: %s:%f\n", gson_get_key_value(_float), gson_get_float_value(_float));
// } else {
// printf("Val not found: 30.00\n");
// }
//
//
// if (_float != NULL)
// {
// JSONType _float_type = gson_get_object_type(_float);
// if (_float_type == JSON_NUMBER)
// {
// printf("4correct type found, JSON_NUMBER\n");
// }
// }
//
// JSONNode *_bool = gson_find_by_key(node, "\"has_kids\"");
// if (_bool != NULL)
// {
// JSONType bool_type = gson_get_object_type(_bool);
// if (bool_type == JSON_TRUE_VAL)
// {
// printf("5correct type found, JSON_TRUE_VAL\n");
// }
// }
#if DEBUG==1
gson_debug_print_tree(node, 0);
#endif
// parser_destroy(parser);
// gson_destroy(node);
free(source);
return 0;
}