Skip to content

Commit a09cdca

Browse files
authored
Merge pull request #13 from tsoding/more-examples
Add more examples
2 parents 7fb6166 + 7af243b commit a09cdca

File tree

6 files changed

+183
-5
lines changed

6 files changed

+183
-5
lines changed

Makefile

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

33
.PHONY: all
4-
all: example test
5-
6-
example: example.c jim.h
7-
$(CC) $(CFLAGS) -o example example.c
4+
all: examples test
85

96
test: test.c jim.h
107
$(CC) $(CFLAGS) -o test test.c
8+
9+
.PHONY: examples
10+
examples:
11+
$(MAKE) -C examples/

examples/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
01_from_readme
2+
02_binary_tree

example.c renamed to examples/01_from_readme.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdio.h>
22

33
#define JIM_IMPLEMENTATION
4-
#include "./jim.h"
4+
#include "../jim.h"
55

66
int main()
77
{

examples/02_binary_tree.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <time.h>
4+
5+
#define JIM_IMPLEMENTATION
6+
#include "../jim.h"
7+
8+
#include "fruits.h"
9+
10+
typedef struct Node Node;
11+
12+
struct Node {
13+
const char *value;
14+
Node *left;
15+
Node *right;
16+
};
17+
18+
Node *generate_tree_of_fruits(size_t level_cur, size_t level_max)
19+
{
20+
if (level_cur < level_max) {
21+
// Let It Leak! Let It Leak!
22+
// Let It Leak! Oh, Let It Leak!
23+
// Memory costs nothing!
24+
// Let It Leak!
25+
Node *node = malloc(sizeof(*node));
26+
node->value = fruits[rand() % fruits_count];
27+
node->left = generate_tree_of_fruits(level_cur + 1, level_max);
28+
node->right = generate_tree_of_fruits(level_cur + 1, level_max);
29+
return node;
30+
} else {
31+
return NULL;
32+
}
33+
}
34+
35+
void print_node_as_json(Jim *jim, Node *node)
36+
{
37+
if (node == NULL) {
38+
jim_null(jim);
39+
} else {
40+
jim_object_begin(jim);
41+
jim_member_key(jim, "value");
42+
jim_string(jim, node->value);
43+
44+
jim_member_key(jim, "left");
45+
print_node_as_json(jim, node->left);
46+
47+
jim_member_key(jim, "right");
48+
print_node_as_json(jim, node->right);
49+
jim_object_end(jim);
50+
}
51+
}
52+
53+
int main()
54+
{
55+
srand(time(0));
56+
Jim jim = {
57+
.sink = stdout,
58+
.write = (Jim_Write) fwrite,
59+
};
60+
print_node_as_json(&jim, generate_tree_of_fruits(0, 4));
61+
return 0;
62+
}

examples/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CFLAGS=-Wall -Wextra -Wswitch-enum -std=c99 -pedantic -ggdb
2+
3+
.PHONY: all
4+
all: 01_from_readme 02_binary_tree
5+
6+
01_from_readme: 01_from_readme.c ../jim.h
7+
$(CC) $(CFLAGS) -o 01_from_readme 01_from_readme.c
8+
9+
02_binary_tree: 02_binary_tree.c fruits.h ../jim.h
10+
$(CC) $(CFLAGS) -o 02_binary_tree 02_binary_tree.c

examples/fruits.h

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#ifndef FRUITS_H_
2+
#define FRUITS_H_
3+
4+
// What? This is just a list of fruits. What did you expect?
5+
6+
const char *fruits[] = {
7+
"Apple",
8+
"Apricot",
9+
"Avocado",
10+
"Banana",
11+
"Bilberry",
12+
"Blackberry",
13+
"Blackcurrant",
14+
"Blueberry",
15+
"Boysenberry",
16+
"Currant",
17+
"Cherry",
18+
"Cherimoya",
19+
"Chico fruit",
20+
"Cloudberry",
21+
"Coconut",
22+
"Cranberry",
23+
"Cucumber",
24+
"Custard apple",
25+
"Damson",
26+
"Date",
27+
"Dragonfruit",
28+
"Durian",
29+
"Elderberry",
30+
"Feijoa",
31+
"Fig",
32+
"Goji berry",
33+
"Gooseberry",
34+
"Grape",
35+
"Raisin",
36+
"Grapefruit",
37+
"Guava",
38+
"Honeyberry",
39+
"Huckleberry",
40+
"Jabuticaba",
41+
"Jackfruit",
42+
"Jambul",
43+
"Jujube",
44+
"Juniper berry",
45+
"Kiwano",
46+
"Kiwifruit",
47+
"Kumquat",
48+
"Lemon",
49+
"Lime",
50+
"Loquat",
51+
"Longan",
52+
"Lychee",
53+
"Mango",
54+
"Mangosteen",
55+
"Marionberry",
56+
"Melon",
57+
"Cantaloupe",
58+
"Honeydew",
59+
"Watermelon",
60+
"Miracle fruit",
61+
"Mulberry",
62+
"Nectarine",
63+
"Nance",
64+
"Olive",
65+
"Orange",
66+
"Blood orange",
67+
"Clementine",
68+
"Mandarine",
69+
"Tangerine",
70+
"Papaya",
71+
"Passionfruit",
72+
"Peach",
73+
"Pear",
74+
"Persimmon",
75+
"Physalis",
76+
"Plantain",
77+
"Plum",
78+
"Prune",
79+
"Pineapple",
80+
"Plumcot",
81+
"Pomegranate",
82+
"Pomelo",
83+
"Purple mangosteen",
84+
"Quince",
85+
"Raspberry",
86+
"Salmonberry",
87+
"Rambutan",
88+
"Redcurrant",
89+
"Salal berry",
90+
"Salak",
91+
"Satsuma",
92+
"Soursop",
93+
"Star fruit",
94+
"Solanum quitoense",
95+
"Strawberry",
96+
"Tamarillo",
97+
"Tamarind",
98+
"Ugli fruit",
99+
"Yuzu"
100+
};
101+
const size_t fruits_count = sizeof(fruits) / sizeof(fruits[0]);
102+
103+
#endif // FRUITS_H_

0 commit comments

Comments
 (0)