diff options
author | Danny van Kooten <dannyvankooten@users.noreply.github.com> | 2020-03-18 19:21:48 +0100 |
---|---|---|
committer | Danny van Kooten <dannyvankooten@users.noreply.github.com> | 2020-03-18 19:21:48 +0100 |
commit | 8d74fd5b0e5fc9f319a5b7d2e0716afcc312fd75 (patch) | |
tree | a6c195d183694304fbe238a68486aceea01d404a /src/template.c | |
parent | 82de777afb99aa4129abcb01da76bf62e7b45263 (diff) | |
download | unja-8d74fd5b0e5fc9f319a5b7d2e0716afcc312fd75.tar.gz unja-8d74fd5b0e5fc9f319a5b7d2e0716afcc312fd75.zip |
clean-up environment in env_free(). add hashmap_walk function for visiting all values in a hashmap.
Diffstat (limited to 'src/template.c')
-rw-r--r-- | src/template.c | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/template.c b/src/template.c index 7ea4d05..a1b3ef4 100644 --- a/src/template.c +++ b/src/template.c @@ -3,6 +3,7 @@ #include <err.h> #include <unistd.h> #include <dirent.h> +#include <string.h> #include "vendor/mpc.h" #include "template.h" @@ -167,9 +168,13 @@ struct env *env_new(char *dirname) { continue; } - char *name = de->d_name; + // copy template name as closedir free's it otherwise + char *name = malloc(strlen(de->d_name) + 1); + strcpy(name, de->d_name); + char *tmpl = read_file(name); mpc_ast_t *ast = parse(tmpl); + free(tmpl); struct template *t = malloc(sizeof *t); t->ast = ast; @@ -182,14 +187,24 @@ struct env *env_new(char *dirname) { } hashmap_insert(env->templates, name, t); + } closedir(dr); return env; } +void template_free(void *v) { + struct template *t = (struct template *)v; + hashmap_free(t->blocks); + mpc_ast_delete(t->ast); + free(t->name); + free(t); +} + void env_free(struct env *env) { - // TODO: Free template strings + hashmap_walk(env->templates, template_free); + hashmap_free(env->templates); free(env); } @@ -215,7 +230,7 @@ char *read_file(char *filename) { } char *trim_trailing_whitespace(char *str) { - for (int i=strlen(str)-1; isspace(str[i]); i--) { + for (int i=strlen(str)-1; i >= 0 && isspace(str[i]); i--) { str[i] = '\0'; } return str; |