summary refs log tree commit diff
path: root/template.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--template.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/template.c b/template.c
index 92c6381..426f1d0 100644
--- a/template.c
+++ b/template.c
@@ -68,7 +68,7 @@ int escapeXML(FILE *file, const char *str) {
 
 int templateRender(
 	FILE *file, const char *template,
-	const struct Variable *vars, EscapeFn *escape
+	const struct Variable vars[], EscapeFn *escape
 ) {
 	for (bool subst = false; *template; subst ^= true) {
 		size_t len = strcspn(template, "[]");
@@ -97,3 +97,21 @@ int templateRender(
 	}
 	return 0;
 }
+
+char *templateURL(const char *template, const struct Variable vars[]) {
+	size_t cap = 3 * 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;
+}