-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy path03_parsing_database.c
More file actions
118 lines (107 loc) · 3.14 KB
/
03_parsing_database.c
File metadata and controls
118 lines (107 loc) · 3.14 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
#include <stdio.h>
#include <stdbool.h>
#define NOB_IMPLEMENTATION
#define NOB_STRIP_PREFIX
#include "../thirdparty/nob.h"
#define JIMP_IMPLEMENTATION
#include "../jimp.h"
typedef struct {
const char *name;
double age;
const char *location;
double body_count;
} Person;
typedef struct {
Person *items;
size_t count;
size_t capacity;
} People;
bool parse_person(Jimp *jimp, Person *p)
{
if (!jimp_object_begin(jimp)) return false;
while (jimp_object_member(jimp)) {
if (strcmp(jimp->member, "name") == 0) {
if (!jimp_string(jimp, &p->name, NULL)) return false;
} else if (strcmp(jimp->member, "age") == 0) {
if (!jimp_number(jimp, &p->age, 0)) return false;
} else if (strcmp(jimp->member, "location") == 0) {
if (!jimp_string(jimp, &p->location, NULL)) return false;
} else if (strcmp(jimp->member, "body_count") == 0) {
if (!jimp_number(jimp, &p->body_count, 0)) return false;
} else {
jimp_unknown_member(jimp);
return false;
}
}
return jimp_object_end(jimp);
}
bool parse_people(Jimp *jimp, People *ps)
{
if (!jimp_array_begin(jimp)) return false;
while (jimp_array_item(jimp)) {
Person p = {0};
if (!parse_person(jimp, &p)) return false;
da_append(ps, p);
}
if (!jimp_array_end(jimp)) return false;
return true;
}
void print_person(const Person *p)
{
printf("name = %s\n", p->name);
printf("age = %lf\n", p->age);
printf("location = %s\n", p->location);
printf("body_count = %lf\n", p->body_count);
}
typedef struct {
long *items;
size_t count;
size_t capacity;
} Numbers;
int main()
{
// const char *file_path = "profile.json";
// const char *file_path = "numbers.json";
// const char *file_path = "profiles.json";
// const char *file_path = "empty.json";
// const char *file_path = "one.json";
const char *file_path = "database.json";
String_Builder sb = {0};
if (!read_entire_file(file_path, &sb)) return 1;
Jimp jimp = {
.file_path = file_path,
.start = sb.items,
.end = sb.items + sb.count,
.point = sb.items,
};
People ps = {0};
Numbers xs = {0};
if (!jimp_object_begin(&jimp)) return 1;
while (jimp_object_member(&jimp)) {
if (strcmp(jimp.member, "profile") == 0) {
if (!parse_people(&jimp, &ps)) return 1;
} else if (strcmp(jimp.member, "number") == 0) {
if (!jimp_array_begin(&jimp)) return 1;
while (jimp_array_item(&jimp)) {
double x = 0;
if (!jimp_number(&jimp, &x, 0)) return 1;
da_append(&xs, x);
}
if (!jimp_array_end(&jimp)) return 1;
} else {
jimp_unknown_member(&jimp);
return 1;
}
}
if (!jimp_object_end(&jimp)) return 1;
da_foreach(Person, p, &ps) {
print_person(p);
printf("\n");
}
printf("------------------------------\n");
da_foreach(long, x, &xs) {
printf("%ld ", *x);
}
printf("\n");
return 0;
}