about summary refs log tree commit diff
path: root/template.c
diff options
context:
space:
mode:
authorC. McEnroe <june@causal.agency>2020-11-28 21:10:25 -0500
committerC. McEnroe <june@causal.agency>2020-11-28 21:46:28 -0500
commitd9e8a44f6f56393e4b08b4570115de77cf0d2a2f (patch)
tree16fdcf907fba001844e7eb7fa49db21856fff9c4 /template.c
parentRefactor IMAP struct (diff)
downloadbubger-d9e8a44f6f56393e4b08b4570115de77cf0d2a2f.tar.gz
bubger-d9e8a44f6f56393e4b08b4570115de77cf0d2a2f.zip
Replace templateBuffer with templateString
Diffstat (limited to 'template.c')
-rw-r--r--template.c27
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);
-}