diff options
Diffstat (limited to 'template.c')
-rw-r--r-- | template.c | 27 |
1 files changed, 7 insertions, 20 deletions
diff --git a/template.c b/template.c index b3ab00f..a47d9f5 100644 --- a/template.c +++ b/template.c @@ -135,30 +135,17 @@ int templateRender( return 0; } -char *templateBuffer( - char *buf, size_t cap, const char *template, - const struct Variable vars[], EscapeFn *escape +char *templateString( + const char *template, const struct Variable vars[], EscapeFn *escape ) { - FILE *file = fmemopen(buf, cap, "w"); - if (!file) err(EX_OSERR, "fmemopen"); + char *buf; + size_t len; + FILE *file = open_memstream(&buf, &len); + if (!file) err(EX_OSERR, "open_memstream"); int error = templateRender(file, template, vars, escape) || fclose(file); - assert(!error); + if (error) err(EX_OSERR, "open_memstream"); - // 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"); - - return templateBuffer(buf, cap, template, vars, escapeURL); -} |