diff options
-rw-r--r-- | archive.h | 4 | ||||
-rw-r--r-- | template.c | 25 |
2 files changed, 22 insertions, 7 deletions
diff --git a/archive.h b/archive.h index 06fd435..b1cb3dc 100644 --- a/archive.h +++ b/archive.h @@ -150,6 +150,10 @@ int templateRender( FILE *file, const char *template, const struct Variable vars[], EscapeFn *escape ); +char *templateBuffer( + char *buf, size_t cap, const char *template, + const struct Variable vars[], EscapeFn *escape +); char *templateURL(const char *template, const struct Variable vars[]); char *decodeHeader(const char *header); 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); } |