From 791e03d8199fa1177f970f45a8a436912d229d1c Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Thu, 12 Mar 2020 15:07:09 +0100 Subject: add vector type --- src/hyde.c | 5 +++++ src/template.c | 3 +-- src/template.h | 12 +----------- src/vector.c | 23 +++++++++++++++++++++++ src/vector.h | 11 +++++++++++ 5 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 src/vector.c create mode 100644 src/vector.h (limited to 'src') diff --git a/src/hyde.c b/src/hyde.c index bab2513..350a518 100644 --- a/src/hyde.c +++ b/src/hyde.c @@ -2,6 +2,11 @@ #include #include +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 +#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 + +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 -- cgit v1.2.3