-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
81 lines (64 loc) · 1.93 KB
/
main.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
75
76
77
78
79
80
81
/* Copyright (C) 2023 James W M Barford-Evans
* <jamesbarfordevans at gmail dot com>
* All Rights Reserved
*
* This code is released under the BSD 2 clause license.
* See the COPYING file for more information.
*
* Command line wrapper for Easy JSON, times parsing a json buffer and times
* freeing the struct, prints error if there is one
*/
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "json.h"
static void __panic(char *fmt, ...) {
va_list va;
char msg[1024];
va_start(va, fmt);
vsnprintf(msg, sizeof(msg), fmt, va);
fprintf(stderr, "%s\n", msg);
va_end(va);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv) {
FILE *fp;
struct stat st;
char *raw_json;
if (argc == 1) {
__panic("Usage: %s <file>\n", argv[0]);
}
if (stat(argv[1], &st) == -1) {
__panic("Failed to stat file: %s\n", strerror(errno));
}
if ((fp = fopen(argv[1], "r")) == NULL) {
__panic("Failed to open file: %s\n", strerror(errno));
}
raw_json = malloc(sizeof(char) * st.st_size);
if (fread(raw_json, 1, st.st_size, fp) != st.st_size) {
__panic("Failed to read file: %s\n", strerror(errno));
}
raw_json[st.st_size] = '\0';
clock_t start_parse = clock();
json *J = jsonParse(raw_json);
clock_t end_parse = clock();
long double elapsed_ms = (double)(end_parse - start_parse) /
CLOCKS_PER_SEC * 1000;
free(raw_json);
jsonPrint(J);
if (J->state->error != JSON_OK) {
jsonPrintError(J);
}
clock_t start_free = clock();
jsonRelease(J);
clock_t end_free = clock();
long double elapsed_free = (double)(end_free - start_free) /
CLOCKS_PER_SEC * 1000;
fprintf(stderr, "parsed in: %0.10Lfms\n", elapsed_ms);
fprintf(stderr, "freed in: %0.10Lfms\n", elapsed_free);
}