diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/hyde.c | 5 | ||||
-rw-r--r-- | src/template.c | 3 | ||||
-rw-r--r-- | src/template.h | 12 | ||||
-rw-r--r-- | src/vector.c | 23 | ||||
-rw-r--r-- | src/vector.h | 11 | ||||
-rw-r--r-- | tests/test_template.c | 17 |
7 files changed, 52 insertions, 23 deletions
@@ -6,13 +6,13 @@ all: bin/hyde bin:; mkdir -p bin/ -bin/hyde: src/hyde.c src/hashmap.c src/template.c vendor/mpc.c | bin +bin/hyde: src/hyde.c src/hashmap.c src/template.c src/vector.c vendor/mpc.c | bin $(CC) $(CFLAGS) $^ -o $@ bin/test_hashmap: src/hashmap.c tests/test_hashmap.c | bin $(CC) $(TESTFLAGS) $^ -o $@ -bin/test_template: src/template.c src/hashmap.c tests/test_template.c vendor/mpc.c | bin +bin/test_template: src/template.c src/hashmap.c src/vector.c tests/test_template.c vendor/mpc.c | bin $(CC) $(TESTFLAGS) $^ -o $@ .PHONY: check @@ -2,6 +2,11 @@ #include <stdio.h> #include <stdlib.h> +struct post { + char title[64]; + char tags[8][32]; +}; + int main() { char *input = read_file("index.tpl"); diff --git a/src/template.c b/src/template.c index c88d90a..e5296c8 100644 --- a/src/template.c +++ b/src/template.c @@ -30,7 +30,6 @@ int eval(char *dest, mpc_ast_t* t, struct hashmap *ctx) { printf("%s: %s\n", t->tag, t->contents); if (strstr(t->tag, "content|var")) { - printf("Key: %s\n", t->contents); char *value = (char *) hashmap_get(ctx, t->children[1]->contents); if (value == NULL) { return 1; @@ -42,7 +41,7 @@ int eval(char *dest, mpc_ast_t* t, struct hashmap *ctx) { if (strstr(t->tag, "content|for")) { char *tmp_key = t->children[2]->contents; char *iterator_key = t->children[4]->contents; - struct list *list = hashmap_get(ctx, iterator_key); + struct vector *list = hashmap_get(ctx, iterator_key); for (int i=0; i < list->size; i++) { hashmap_insert(ctx, tmp_key, list->values[i]); eval(dest, t->children[6], ctx); diff --git a/src/template.h b/src/template.h index b4d46b3..36d540d 100644 --- a/src/template.h +++ b/src/template.h @@ -1,15 +1,5 @@ #include "hashmap.h" +#include "vector.h" char *read_file(char *filename); char *template(char *tmpl, struct hashmap *ctx); - -struct post { - char title[64]; - char tags[8][32]; -}; - -struct list { - void **values; - int size; - int cap; -};
\ No newline at end of file diff --git a/src/vector.c b/src/vector.c new file mode 100644 index 0000000..46a65e5 --- /dev/null +++ b/src/vector.c @@ -0,0 +1,23 @@ +#include <stdlib.h> +#include "vector.h" + +/* create a new vector of the given capacity */ +struct vector* vector_new(int cap) { + struct vector *l = malloc(sizeof *l); + l->size = 0; + l->cap = cap; + l->values = malloc(l->cap * sizeof *l->values); + return l; +} + +/* push a new value to the end of the vector's memory */ +int vector_push(struct vector *vec, void *value) { + vec->values[vec->size++] = value; + return vec->size - 1; +} + +/* free vector related memory */ +void vector_free(struct vector *l) { + free(l->values); + free(l); +}
\ No newline at end of file diff --git a/src/vector.h b/src/vector.h new file mode 100644 index 0000000..e4a912a --- /dev/null +++ b/src/vector.h @@ -0,0 +1,11 @@ +#include <stdlib.h> + +struct vector { + void **values; + int size; + int cap; +}; + +struct vector* vector_new(int cap); +int vector_push(struct vector *vec, void *value); +void vector_free(struct vector *vec);
\ No newline at end of file diff --git a/tests/test_template.c b/tests/test_template.c index 69f1d94..55747e7 100644 --- a/tests/test_template.c +++ b/tests/test_template.c @@ -33,16 +33,17 @@ TEST(text_multiline) { TEST(for_block) { char *input = "{% for n in numbers %}{{ n }}, {% endfor %}"; struct hashmap *ctx = hashmap_new(); - struct list numbers; - numbers.size = 0; - numbers.cap = 64; - numbers.values = malloc(numbers.cap * sizeof *numbers.values); - numbers.values[numbers.size++] = "1"; - numbers.values[numbers.size++] = "2"; - numbers.values[numbers.size++] = "3"; - hashmap_insert(ctx, "numbers", &numbers); + + struct vector *numbers = vector_new(3); + vector_push(numbers, "1"); + vector_push(numbers, "2"); + vector_push(numbers, "3"); + hashmap_insert(ctx, "numbers", numbers); + char *output = template(input, ctx); assert_str(output, "1, 2, 3, "); + + vector_free(numbers); hashmap_free(ctx); free(output); } |