-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
61 lines (49 loc) · 1.53 KB
/
main.c
File metadata and controls
61 lines (49 loc) · 1.53 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
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "libjson.h"
static void print_diff(char *msg, struct timespec start, struct timespec end) {
printf("%s: %Lf ms\n", msg, ((end.tv_sec - start.tv_sec) * (long) 1e9
+ (end.tv_nsec - start.tv_nsec)) * (long double) 1e-6);
}
int main() {
size_t size = 0;
size_t capacity = 1024;
ssize_t ret;
char *json = safe_malloc(capacity * sizeof(char));
while ((ret = read(STDIN_FILENO, (json + size), capacity - size)) != 0) {
if (ret == -1) {
free(json);
perror("read");
return 1;
}
size += ret;
if (capacity == size) {
json = realloc(json, (capacity *= 2) * sizeof(char));
}
}
json[size] = '\0';
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
json_entry_t *ent = json_parse(json);
clock_gettime(CLOCK_MONOTONIC, &end);
if (ent) {
print_diff("parse", start, end);
size_t n;
clock_gettime(CLOCK_MONOTONIC, &start);
char *json_out = json_stringify(ent, &n);
clock_gettime(CLOCK_MONOTONIC, &end);
print_diff("stringify", start, end);
printf("%s\n", json_out);
clock_gettime(CLOCK_MONOTONIC, &start);
json_destroy(ent);
clock_gettime(CLOCK_MONOTONIC, &end);
print_diff("destroy", start, end);
free(json_out);
}
free(json);
return 0;
}