Skip to content

Commit 677aeab

Browse files
committed
Merge branch 'jimp'
2 parents 25be6a2 + 0b3c5b9 commit 677aeab

File tree

9 files changed

+2672
-4
lines changed

9 files changed

+2672
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
example
2-
test
2+
test
3+
jimp

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CFLAGS=-Wall -Wextra -Wswitch-enum -std=c99 -pedantic -ggdb
1+
CFLAGS=-Wall -Wextra -Wswitch-enum -ggdb
22

33
.PHONY: all
44
all: examples test

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,5 @@ $ ./test record
114114

115115
1. Does not depends on libc. Could be theoretically used in embedded, but I know nothing about embedded, so maybe not.
116116
2. `jim_float()` is quite likely very stupid and imprecise
117+
118+
<!-- TODO: document jimp.h here -->

examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
01_from_readme
22
02_binary_tree
3+
03_parsing_database

examples/03_parsing_database.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
#define NOB_IMPLEMENTATION
4+
#define NOB_STRIP_PREFIX
5+
#include "../thirdparty/nob.h"
6+
#define JIMP_IMPLEMENTATION
7+
#include "../jimp.h"
8+
9+
typedef struct {
10+
const char *name;
11+
double age;
12+
const char *location;
13+
double body_count;
14+
} Person;
15+
16+
typedef struct {
17+
Person *items;
18+
size_t count;
19+
size_t capacity;
20+
} People;
21+
22+
bool parse_person(Jimp *jimp, Person *p)
23+
{
24+
if (!jimp_object_begin(jimp)) return false;
25+
while (jimp_object_member(jimp)) {
26+
if (strcmp(jimp->member, "name") == 0) {
27+
if (!jimp_string(jimp, &p->name)) return false;
28+
} else if (strcmp(jimp->member, "age") == 0) {
29+
if (!jimp_number(jimp, &p->age)) return false;
30+
} else if (strcmp(jimp->member, "location") == 0) {
31+
if (!jimp_string(jimp, &p->location)) return false;
32+
} else if (strcmp(jimp->member, "body_count") == 0) {
33+
if (!jimp_number(jimp, &p->body_count)) return false;
34+
} else {
35+
jimp_unknown_member(jimp);
36+
return false;
37+
}
38+
}
39+
return jimp_object_end(jimp);
40+
}
41+
42+
bool parse_people(Jimp *jimp, People *ps)
43+
{
44+
if (!jimp_array_begin(jimp)) return false;
45+
while (jimp_array_item(jimp)) {
46+
Person p = {0};
47+
if (!parse_person(jimp, &p)) return false;
48+
da_append(ps, p);
49+
}
50+
if (!jimp_array_end(jimp)) return false;
51+
52+
return true;
53+
}
54+
55+
void print_person(const Person *p)
56+
{
57+
printf("name = %s\n", p->name);
58+
printf("age = %lf\n", p->age);
59+
printf("location = %s\n", p->location);
60+
printf("body_count = %lf\n", p->body_count);
61+
}
62+
63+
typedef struct {
64+
long *items;
65+
size_t count;
66+
size_t capacity;
67+
} Numbers;
68+
69+
int main()
70+
{
71+
// const char *file_path = "profile.json";
72+
// const char *file_path = "numbers.json";
73+
// const char *file_path = "profiles.json";
74+
// const char *file_path = "empty.json";
75+
// const char *file_path = "one.json";
76+
const char *file_path = "database.json";
77+
String_Builder sb = {0};
78+
if (!read_entire_file(file_path, &sb)) return 1;
79+
Jimp jimp = {
80+
.file_path = file_path,
81+
.start = sb.items,
82+
.end = sb.items + sb.count,
83+
.point = sb.items,
84+
};
85+
86+
People ps = {0};
87+
Numbers xs = {0};
88+
if (!jimp_object_begin(&jimp)) return 1;
89+
while (jimp_object_member(&jimp)) {
90+
if (strcmp(jimp.member, "profile") == 0) {
91+
if (!parse_people(&jimp, &ps)) return 1;
92+
} else if (strcmp(jimp.member, "number") == 0) {
93+
if (!jimp_array_begin(&jimp)) return 1;
94+
while (jimp_array_item(&jimp)) {
95+
double x = 0;
96+
if (!jimp_number(&jimp, &x)) return 1;
97+
da_append(&xs, x);
98+
}
99+
if (!jimp_array_end(&jimp)) return 1;
100+
} else {
101+
jimp_unknown_member(&jimp);
102+
return 1;
103+
}
104+
}
105+
if (!jimp_object_end(&jimp)) return 1;
106+
107+
da_foreach(Person, p, &ps) {
108+
print_person(p);
109+
printf("\n");
110+
}
111+
printf("------------------------------\n");
112+
da_foreach(long, x, &xs) {
113+
printf("%ld ", *x);
114+
}
115+
printf("\n");
116+
117+
return 0;
118+
}

examples/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
CFLAGS=-Wall -Wextra -Wswitch-enum -std=c99 -pedantic -ggdb
1+
CFLAGS=-Wall -Wextra -Wswitch-enum -ggdb
22

33
.PHONY: all
4-
all: 01_from_readme 02_binary_tree
4+
all: 01_from_readme 02_binary_tree 03_parsing_database
55

66
01_from_readme: 01_from_readme.c ../jim.h
77
$(CC) $(CFLAGS) -o 01_from_readme 01_from_readme.c
88

99
02_binary_tree: 02_binary_tree.c fruits.h ../jim.h
1010
$(CC) $(CFLAGS) -o 02_binary_tree 02_binary_tree.c
11+
12+
03_parsing_database: 03_parsing_database.c ../jimp.h ../thirdparty/nob.h
13+
$(CC) $(CFLAGS) -o 03_parsing_database 03_parsing_database.c

examples/database.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"number": [69, 420, 1337, 80085],
3+
"profile": [
4+
{
5+
"location": "Wonderland",
6+
"location": "Wonderland",
7+
"body_count": 150,
8+
"name": "Alice Smith",
9+
"age": 34
10+
},
11+
{
12+
"name": "Bob Johnson",
13+
"age": 45,
14+
"location": "Atlantis",
15+
"body_count": 300
16+
},
17+
{
18+
"name": "Charlie Brown",
19+
"age": 28,
20+
"location": "Chocolate Factory",
21+
"body_count": 200
22+
},
23+
{
24+
"name": "Diana Prince",
25+
"age": 32,
26+
"location": "Themyscira",
27+
"body_count": 250
28+
},
29+
{
30+
"name": "Ethan Hunt",
31+
"age": 40,
32+
"location": "Mission HQ",
33+
"body_count": 350
34+
},
35+
{
36+
"name": "Fiona Apple",
37+
"age": 37,
38+
"location": "Music City",
39+
"body_count": 180
40+
},
41+
{
42+
"name": "George Lucas",
43+
"age": 75,
44+
"location": "Galaxy Far Far Away",
45+
"body_count": 500
46+
},
47+
{
48+
"name": "Hannah Montana",
49+
"age": 25,
50+
"location": "Nashville",
51+
"body_count": 100
52+
},
53+
{
54+
"name": "Ian Malcolm",
55+
"age": 60,
56+
"location": "Jurassic Park",
57+
"body_count": 400
58+
},
59+
{
60+
"name": "Jessica Rabbit",
61+
"age": 30,
62+
"location": "Toontown",
63+
"body_count": 220
64+
}
65+
]
66+
}

0 commit comments

Comments
 (0)