diff options
-rw-r--r-- | include/template.h | 24 | ||||
-rw-r--r-- | src/template.c | 27 |
2 files changed, 51 insertions, 0 deletions
diff --git a/include/template.h b/include/template.h index 116960f..a242a08 100644 --- a/include/template.h +++ b/include/template.h @@ -40,4 +40,28 @@ char *tpl_render(char *str); */ int tpl_render_to_file(char *str, const char *filename); +struct tplfunc_frame *tpl_getfunc(char *key); +struct tplfunc_frame; +typedef int tplfunc(struct tplfunc_frame *frame); +struct tplfunc_frame { + char *key; + tplfunc *func; + int argc; + union { + char **t_char_refptr; + char *t_char_ptr; + void *t_void_ptr; + int *t_int_ptr; + unsigned *t_uint_ptr; + float *t_float_ptr; + double *t_double_ptr; + char t_char; + int t_int; + unsigned t_uint; + float t_float; + double t_double; + } argv[10]; // accept up to 10 arguments +}; +void tpl_register_func(char *key, struct tplfunc_frame *frame); + #endif //OMC_TEMPLATE_H diff --git a/src/template.c b/src/template.c index 4fff31c..39f01ac 100644 --- a/src/template.c +++ b/src/template.c @@ -16,6 +16,15 @@ struct tpl_item { }; struct tpl_item *tpl_pool[1024] = {0}; unsigned tpl_pool_used = 0; +unsigned tpl_pool_func_used = 0; + +struct tplfunc_frame *tpl_pool_func[1024] = {0}; + +void tpl_register_func(char *key, struct tplfunc_frame *frame) { + tpl_pool_func[tpl_pool_func_used] = calloc(1, sizeof(tpl_pool_func[tpl_pool_func_used])); + memcpy(tpl_pool_func[tpl_pool_func_used], frame, sizeof(*frame)); + tpl_pool_func_used++; +} void tpl_register(char *key, char **ptr) { struct tpl_item *item = NULL; @@ -55,6 +64,19 @@ char *tpl_getval(char *key) { return result; } +struct tplfunc_frame *tpl_getfunc(char *key) { + struct tplfunc_frame *result = NULL; + for (size_t i = 0; i < tpl_pool_func_used; i++) { + if (tpl_pool_func[i]->key) { + if (!strcmp(tpl_pool_func[i]->key, key)) { + result = tpl_pool_func[i]; + break; + } + } + } + return result; +} + static int grow(size_t z, size_t *output_bytes, char **output) { if (z >= *output_bytes) { size_t new_size = *output_bytes + z + 1; @@ -117,9 +139,12 @@ char *tpl_render(char *str) { type_stop = strchr(key, ':'); int do_env = 0; + int do_func = 0; if (type_stop) { if (!strncmp(key, "env", type_stop - key)) { do_env = 1; + } else if (!strncmp(key, "func", type_stop - key)) { + do_func = 1; } } @@ -139,6 +164,8 @@ char *tpl_render(char *str) { key[klen] = 0; char *env_val = getenv(key); value = strdup(env_val ? env_val : ""); + } else if (do_func) { + // TODO } else { // Read replacement value value = strdup(tpl_getval(key) ? tpl_getval(key) : ""); |