diff options
Diffstat (limited to 'template.c')
-rw-r--r-- | template.c | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/template.c b/template.c index 8653769..afb9173 100644 --- a/template.c +++ b/template.c @@ -103,19 +103,30 @@ int templateRender( return 0; } +char *templateBuffer( + char *buf, size_t cap, const char *template, + const struct Variable vars[], EscapeFn *escape +) { + FILE *file = fmemopen(buf, cap, "w"); + if (!file) err(EX_OSERR, "fmemopen"); + + int error = templateRender(file, template, vars, escape) + || fclose(file); + assert(!error); + + // XXX: fmemopen only null-terminates if there is room. + buf[cap - 1] = '\0'; + return buf; +} + char *templateURL(const char *template, const struct Variable vars[]) { size_t cap = strlen(template) + 1; for (const struct Variable *var = vars; var->name; ++var) { cap += 3 * strlen(var->value); } + char *buf = malloc(cap); if (!buf) err(EX_OSERR, "malloc"); - FILE *file = fmemopen(buf, cap, "w"); - if (!file) err(EX_OSERR, "fmemopen"); - - int error = templateRender(file, template, vars, escapeURL) - || fclose(file); - assert(!error); - return buf; + return templateBuffer(buf, cap, template, vars, escapeURL); } |