-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.c
103 lines (84 loc) · 2.39 KB
/
data.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Sample code showing how to parse and process data.
// Features the usage of jsonst_cstd.h.
// To run:
//
// bazel run //examples:data examples/testdata/fluglaerm-iselisberg.json
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "src/jsonst_cstd.h"
typedef struct {
double *val;
ptrdiff_t cap;
ptrdiff_t len;
} dvec;
static dvec new_dvec(const size_t cap) {
dvec ret = {0};
ret.val = (double *)calloc(cap, sizeof(double));
assert(ret.val != NULL);
ret.len = 0;
ret.cap = cap;
return ret;
}
static void dvec_push(dvec *vec, const double val) {
assert(vec->len < vec->cap);
vec->val[vec->len] = val;
vec->len++;
}
static void dvec_free(dvec *vec) { free(vec->val); }
static void cb(void *user_data, const jsonst_value *value, const jsonst_path *path) {
if (value->type != jsonst_num) {
return;
}
if (path->type != jsonst_arry_elm) {
return;
}
if (path->next == NULL || path->next->type != jsonst_obj_key) {
return;
}
if (strncmp(path->next->props.obj_key.str, "temperatur_degc_max",
path->next->props.obj_key.str_len) != 0) {
return;
}
char *endptr;
double num = strtod(value->val_str.str, &endptr);
assert(endptr == value->val_str.str + value->val_str.str_len);
dvec *vec = (dvec *)user_data;
dvec_push(vec, num);
}
int main(const int argc, const char **argv) {
// Open file.
if (argc != 2) {
printf("usage: data <input>.json\n");
return EXIT_FAILURE;
}
FILE *inf = fopen(argv[1], "r");
if (inf == NULL) {
fprintf(stderr, "Unable to open file '%s'!\n", argv[1]);
return EXIT_FAILURE;
}
// Prepare results vec.
dvec vec = new_dvec(276);
// Prepare parser.
const ptrdiff_t memsz = 1024 * 1;
void *mem = malloc(memsz);
assert(mem != NULL);
jsonst_config conf = {0};
jsonst j = new_jsonst((uint8_t *)mem, memsz, cb, &vec, conf);
const jsonst_feed_doc_ret ret = jsonst_feed_fstream(j, inf);
if (ret.err != jsonst_success) {
fprintf(stderr, "Parsing error: %d\n", ret.err);
return EXIT_FAILURE;
}
// Print results.
for (ptrdiff_t i = 0; i < vec.len; i++) {
printf("[%td] = %f\n", i, vec.val[i]);
}
// Cleanup.
fclose(inf);
free(mem);
dvec_free(&vec);
return EXIT_SUCCESS;
}