summary refs log tree commit diff
path: root/concat.c
diff options
context:
space:
mode:
Diffstat (limited to 'concat.c')
-rw-r--r--concat.c35
1 files changed, 24 insertions, 11 deletions
diff --git a/concat.c b/concat.c
index 3e03f3b..82f2c84 100644
--- a/concat.c
+++ b/concat.c
@@ -230,6 +230,15 @@ void concatThreads(struct List threads, const struct Envelope *envelopes) {
 	}
 }
 
+static char *searchPath(const char *name, const char *type) {
+	struct Variable vars[] = {
+		{ "name", name },
+		{ "type", type },
+		{0},
+	};
+	return templateString(PATH_SEARCH, vars, escapePath);
+}
+
 static int numberCompare(const void *_a, const void *_b) {
 	const struct Data *a = _a;
 	const struct Data *b = _b;
@@ -252,20 +261,22 @@ static int sortCompare(const void *_a, const void *_b) {
 	}
 }
 
-size_t concatIndexEntries = 20;
+size_t concatSearchEntries = 20;
 
-void concatIndex(struct List threads, const struct Envelope *envelopes) {
-	const char *path = "index.atom";
+void concatSearch(
+	const char *name, struct List threads, const struct Envelope *envelopes
+) {
+	char *path = searchPath(name, "atom");
 	FILE *file = fopen(path, "w");
 	if (!file) err(EX_CANTCREAT, "%s", path);
 
-	int error = atomIndexOpen(file);
+	int error = atomSearchOpen(file, name);
 	if (error) err(EX_IOERR, "%s", path);
 
 	struct List flat = {0};
 	listFlatten(&flat, threads);
 	qsort(flat.ptr, flat.len, sizeof(*flat.ptr), numberCompare);
-	for (size_t i = 0; i < flat.len && i < concatIndexEntries; ++i) {
+	for (size_t i = 0; i < flat.len && i < concatSearchEntries; ++i) {
 		uint32_t uid = dataCheck(flat.ptr[i], Number).number;
 		char *src = uidPath(uid, "atom");
 		error = concatFile(file, src);
@@ -274,9 +285,10 @@ void concatIndex(struct List threads, const struct Envelope *envelopes) {
 	}
 	listFree(flat);
 
-	error = atomIndexClose(file) || fclose(file);
+	error = atomSearchClose(file) || fclose(file);
 	if (error) err(EX_IOERR, "%s", path);
 	if (!quiet) printf("%s\n", path);
+	free(path);
 
 	struct Sort *order = calloc(threads.len, sizeof(*order));
 	if (!order) err(EX_OSERR, "calloc");
@@ -294,11 +306,11 @@ void concatIndex(struct List threads, const struct Envelope *envelopes) {
 	}
 	qsort(order, threads.len, sizeof(*order), sortCompare);
 
-	path = "index.html";
+	path = searchPath(name, "html");
 	file = fopen(path, "w");
 	if (!file) err(EX_CANTCREAT, "%s", path);
 
-	error = htmlIndexHead(file);
+	error = htmlSearchHead(file, name);
 	if (error) err(EX_IOERR, "%s", path);
 
 	if (concatHead) {
@@ -306,18 +318,19 @@ void concatIndex(struct List threads, const struct Envelope *envelopes) {
 		if (error) err(EX_IOERR, "%s", path);
 	}
 
-	error = htmlIndexOpen(file);
+	error = htmlSearchOpen(file, name);
 	if (error) err(EX_IOERR, "%s", path);
 
 	for (size_t i = threads.len - 1; i < threads.len; --i) {
 		const struct Envelope *envelope = &envelopes[order[i].index];
 		struct List thread = dataCheck(threads.ptr[order[i].index], List).list;
-		error = htmlIndexThread(file, envelope, thread);
+		error = htmlSearchThread(file, envelope, thread);
 		if (error) err(EX_IOERR, "%s", path);
 	}
 	free(order);
 
-	error = htmlIndexClose(file) || fclose(file);
+	error = htmlSearchClose(file) || fclose(file);
 	if (error) err(EX_IOERR, "%s", path);
 	if (!quiet) printf("%s\n", path);
+	free(path);
 }