about summary refs log tree commit diff
path: root/template.c
diff options
context:
space:
mode:
authorC. McEnroe <june@causal.agency>2020-04-17 16:46:14 -0400
committerC. McEnroe <june@causal.agency>2020-04-17 16:46:14 -0400
commit72b69f813efe5570d90b13507228704915542077 (patch)
treebff92e9caf4282dd922b86e8ee7342f62f853abe /template.c
parentWrite attachment files (diff)
downloadbubger-72b69f813efe5570d90b13507228704915542077.tar.gz
bubger-72b69f813efe5570d90b13507228704915542077.zip
Factor out templateBuffer
Diffstat (limited to 'template.c')
-rw-r--r--template.c25
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);
 }