Skip to content

Commit 8d0f120

Browse files
committed
Roughly outline the jimp idea
1 parent 25be6a2 commit 8d0f120

File tree

12 files changed

+3564
-3
lines changed

12 files changed

+3564
-3
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
CFLAGS=-Wall -Wextra -Wswitch-enum -std=c99 -pedantic -ggdb
1+
CFLAGS=-Wall -Wextra -Wswitch-enum -ggdb
22

33
.PHONY: all
4-
all: examples test
4+
all: examples test jimp
55

66
test: test.c jim.h
77
$(CC) $(CFLAGS) -o test test.c
88

9+
jimp: jimp.c nob.h
10+
$(CC) $(CFLAGS) -o jimp jimp.c
11+
912
.PHONY: examples
1013
examples:
1114
$(MAKE) -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+
}

empty.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

jimp.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
#define NOB_IMPLEMENTATION
4+
#define NOB_STRIP_PREFIX
5+
#include "nob.h"
6+
#define JIMP_IMPLEMENTATION
7+
#include "jimp.h"
8+
9+
typedef struct {
10+
const char *name;
11+
long age;
12+
const char *location;
13+
long 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->key, "name") == 0) {
27+
if (!jimp_string(jimp, &p->name)) return false;
28+
} else if (strcmp(jimp->key, "age") == 0) {
29+
if (!jimp_number(jimp, &p->age)) return false;
30+
} else if (strcmp(jimp->key, "location") == 0) {
31+
if (!jimp_string(jimp, &p->location)) return false;
32+
} else if (strcmp(jimp->key, "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_has_items(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 = %ld\n", p->age);
59+
printf("location = %s\n", p->location);
60+
printf("body_count = %ld\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 *path = "profile.json";
72+
// const char *path = "numbers.json";
73+
// const char *path = "profiles.json";
74+
// const char *path = "empty.json";
75+
// const char *path = "one.json";
76+
// const char *path = "database.json";
77+
String_Builder sb = {0};
78+
if (!read_entire_file(path, &sb)) return 1;
79+
Jimp jimp = {
80+
.path = path,
81+
};
82+
static char string_store[1024];
83+
stb_c_lexer_init(&jimp.l, sb.items, sb.items + sb.count, string_store, sizeof(string_store));
84+
85+
Person p = {0};
86+
if (!parse_person(&jimp, &p)) return 1;
87+
print_person(&p);
88+
89+
/*
90+
People ps = {0};
91+
Numbers xs = {0};
92+
if (!jimp_object_begin(&jimp)) return 1;
93+
while (jimp_object_member(&jimp)) {
94+
if (strcmp(jimp.key, "profile") == 0) {
95+
if (!parse_people(&jimp, &ps)) return 1;
96+
} else if (strcmp(jimp.key, "number") == 0) {
97+
if (!jimp_array_begin(&jimp)) return 1;
98+
while (jimp_array_has_items(&jimp)) {
99+
long x = 0;
100+
jimp_number(&jimp, &x);
101+
da_append(&xs, x);
102+
}
103+
if (!jimp_array_end(&jimp)) return 1;
104+
} else {
105+
jimp_unknown_member(&jimp);
106+
return 1;
107+
}
108+
}
109+
if (!jimp_object_end(&jimp)) return 1;
110+
111+
da_foreach(Person, p, &ps) {
112+
print_person(p);
113+
printf("\n");
114+
}
115+
printf("------------------------------\n");
116+
da_foreach(long, x, &xs) {
117+
printf("%ld ", *x);
118+
}
119+
printf("\n");
120+
*/
121+
122+
return 0;
123+
}

0 commit comments

Comments
 (0)