فهرست منبع

Merge pull request #13 from tsoding/more-examples

Add more examples
Alexey Kutepov 4 سال پیش
والد
کامیت
a09cdcae36
6فایلهای تغییر یافته به همراه183 افزوده شده و 5 حذف شده
  1. 5 4
      Makefile
  2. 2 0
      examples/.gitignore
  3. 1 1
      examples/01_from_readme.c
  4. 62 0
      examples/02_binary_tree.c
  5. 10 0
      examples/Makefile
  6. 103 0
      examples/fruits.h

+ 5 - 4
Makefile

@@ -1,10 +1,11 @@
 CFLAGS=-Wall -Wextra -Wswitch-enum -std=c99 -pedantic -ggdb
 
 .PHONY: all
-all: example test
-
-example: example.c jim.h
-	$(CC) $(CFLAGS) -o example example.c 
+all: examples test
 
 test: test.c jim.h
 	$(CC) $(CFLAGS) -o test test.c 
+
+.PHONY: examples
+examples: 
+	$(MAKE) -C examples/

+ 2 - 0
examples/.gitignore

@@ -0,0 +1,2 @@
+01_from_readme
+02_binary_tree

+ 1 - 1
example.c → examples/01_from_readme.c

@@ -1,7 +1,7 @@
 #include <stdio.h>
 
 #define JIM_IMPLEMENTATION
-#include "./jim.h"
+#include "../jim.h"
 
 int main()
 {

+ 62 - 0
examples/02_binary_tree.c

@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define JIM_IMPLEMENTATION
+#include "../jim.h"
+
+#include "fruits.h"
+
+typedef struct Node Node;
+
+struct Node {
+    const char *value;
+    Node *left;
+    Node *right;
+};
+
+Node *generate_tree_of_fruits(size_t level_cur, size_t level_max)
+{
+    if (level_cur < level_max) {
+        // Let It Leak! Let It Leak!
+        // Let It Leak! Oh, Let It Leak!
+        // Memory costs nothing!
+        // Let It Leak!
+        Node *node = malloc(sizeof(*node));
+        node->value = fruits[rand() % fruits_count];
+        node->left = generate_tree_of_fruits(level_cur + 1, level_max);
+        node->right = generate_tree_of_fruits(level_cur + 1, level_max);
+        return node;
+    } else {
+        return NULL;
+    }
+}
+
+void print_node_as_json(Jim *jim, Node *node)
+{
+    if (node == NULL) {
+        jim_null(jim);
+    } else {
+        jim_object_begin(jim);
+        jim_member_key(jim, "value");
+        jim_string(jim, node->value);
+
+        jim_member_key(jim, "left");
+        print_node_as_json(jim, node->left);
+
+        jim_member_key(jim, "right");
+        print_node_as_json(jim, node->right);
+        jim_object_end(jim);
+    }
+}
+
+int main()
+{
+    srand(time(0));
+    Jim jim = {
+        .sink = stdout,
+        .write = (Jim_Write) fwrite,
+    };
+    print_node_as_json(&jim, generate_tree_of_fruits(0, 4));
+    return 0;
+}

+ 10 - 0
examples/Makefile

@@ -0,0 +1,10 @@
+CFLAGS=-Wall -Wextra -Wswitch-enum -std=c99 -pedantic -ggdb
+
+.PHONY: all
+all: 01_from_readme 02_binary_tree
+
+01_from_readme: 01_from_readme.c ../jim.h
+	$(CC) $(CFLAGS) -o 01_from_readme 01_from_readme.c
+
+02_binary_tree: 02_binary_tree.c fruits.h ../jim.h
+	$(CC) $(CFLAGS) -o 02_binary_tree 02_binary_tree.c

+ 103 - 0
examples/fruits.h

@@ -0,0 +1,103 @@
+#ifndef FRUITS_H_
+#define FRUITS_H_
+
+// What? This is just a list of fruits. What did you expect?
+
+const char *fruits[] = {
+    "Apple",
+    "Apricot",
+    "Avocado",
+    "Banana",
+    "Bilberry",
+    "Blackberry",
+    "Blackcurrant",
+    "Blueberry",
+    "Boysenberry",
+    "Currant",
+    "Cherry",
+    "Cherimoya",
+    "Chico fruit",
+    "Cloudberry",
+    "Coconut",
+    "Cranberry",
+    "Cucumber",
+    "Custard apple",
+    "Damson",
+    "Date",
+    "Dragonfruit",
+    "Durian",
+    "Elderberry",
+    "Feijoa",
+    "Fig",
+    "Goji berry",
+    "Gooseberry",
+    "Grape",
+    "Raisin",
+    "Grapefruit",
+    "Guava",
+    "Honeyberry",
+    "Huckleberry",
+    "Jabuticaba",
+    "Jackfruit",
+    "Jambul",
+    "Jujube",
+    "Juniper berry",
+    "Kiwano",
+    "Kiwifruit",
+    "Kumquat",
+    "Lemon",
+    "Lime",
+    "Loquat",
+    "Longan",
+    "Lychee",
+    "Mango",
+    "Mangosteen",
+    "Marionberry",
+    "Melon",
+    "Cantaloupe",
+    "Honeydew",
+    "Watermelon",
+    "Miracle fruit",
+    "Mulberry",
+    "Nectarine",
+    "Nance",
+    "Olive",
+    "Orange",
+    "Blood orange",
+    "Clementine",
+    "Mandarine",
+    "Tangerine",
+    "Papaya",
+    "Passionfruit",
+    "Peach",
+    "Pear",
+    "Persimmon",
+    "Physalis",
+    "Plantain",
+    "Plum",
+    "Prune",
+    "Pineapple",
+    "Plumcot",
+    "Pomegranate",
+    "Pomelo",
+    "Purple mangosteen",
+    "Quince",
+    "Raspberry",
+    "Salmonberry",
+    "Rambutan",
+    "Redcurrant",
+    "Salal berry",
+    "Salak",
+    "Satsuma",
+    "Soursop",
+    "Star fruit",
+    "Solanum quitoense",
+    "Strawberry",
+    "Tamarillo",
+    "Tamarind",
+    "Ugli fruit",
+    "Yuzu"
+};
+const size_t fruits_count = sizeof(fruits) / sizeof(fruits[0]);
+
+#endif // FRUITS_H_