diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-02-02 12:30:56 +0100 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-02-02 12:30:56 +0100 |
commit | d9298a35b6741a918e21e5d04e742f05b784e3ea (patch) | |
tree | 973cba3d696fc5233b5c5d379ea3655eb966b1ff /src/blogc/funcvars.c | |
parent | 0325a15690b47c181bb4a39bd058de8a62fffc63 (diff) | |
download | blogc-d9298a35b6741a918e21e5d04e742f05b784e3ea.tar.gz blogc-d9298a35b6741a918e21e5d04e742f05b784e3ea.tar.bz2 blogc-d9298a35b6741a918e21e5d04e742f05b784e3ea.zip |
blogc: added template variables for memory and cpu time resource usage
this include infrastructure required to have variables evaluated as lazy
function calls.
missing tests, docs and improvements.
Diffstat (limited to 'src/blogc/funcvars.c')
-rw-r--r-- | src/blogc/funcvars.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/blogc/funcvars.c b/src/blogc/funcvars.c new file mode 100644 index 0000000..a3af274 --- /dev/null +++ b/src/blogc/funcvars.c @@ -0,0 +1,47 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2014-2019 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> + +#include "funcvars.h" +#include "rusage.h" +#include "../common/utils.h" + + +static const struct func_map { + const char *variable; + const blogc_funcvars_func_t func; +} funcs[] = { + {"BLOGC_RUSAGE_CPU_TIME", blogc_rusage_cpu_time}, + {"BLOGC_RUSAGE_MEMORY", blogc_rusage_memory}, + {NULL, NULL}, +}; + + +char* +blogc_funcvars_lookup(const char *name, bc_trie_t *global) +{ + // protect against evaluating the same function twice in the same global + // context + if (NULL != bc_trie_lookup(global, name)) + return NULL; + + for (size_t i = 0; funcs[i].variable != NULL; i++) { + if (0 == strcmp(name, funcs[i].variable)) { + char *val = funcs[i].func(); + if (val == NULL) + return NULL; + bc_trie_insert(global, name, bc_strdup(val)); + return val; + } + } + + return NULL; +} |