From d3bc945e6317b7f1b80464387e11b191c8143c69 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sat, 25 Apr 2020 15:45:17 -0400 Subject: Render index.html --- concat.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'concat.c') diff --git a/concat.c b/concat.c index e75c857..8e165d2 100644 --- a/concat.c +++ b/concat.c @@ -213,5 +213,60 @@ void concatThreads(struct List threads, const struct Envelope *envelopes) { } } +struct Sort { + size_t index; + time_t updated; + time_t created; +}; + +static int compar(const void *_a, const void *_b) { + const struct Sort *a = _a; + const struct Sort *b = _b; + if (a->updated == b->updated) { + return (a->created > b->created) - (a->created < b->created); + } else { + return (a->updated > b->updated) - (a->updated < b->updated); + } +} + void concatIndex(struct List threads, const struct Envelope *envelopes) { + struct Sort *order = calloc(threads.len, sizeof(*order)); + if (!order) err(EX_OSERR, "calloc"); + + for (size_t i = 0; i < threads.len; ++i) { + struct stat status; + const char *path = threadPath(envelopes[i].messageID, "html"); + int error = stat(path, &status); + if (error) err(EX_DATAERR, "%s", path); + + order[i].index = i; + order[i].created = envelopes[i].time; + order[i].updated = status.st_mtime; + } + qsort(order, threads.len, sizeof(*order), compar); + + const char *path = "index.html"; + FILE *file = fopen(path, "w"); + if (!file) err(EX_CANTCREAT, "%s", path); + + int error = htmlIndexHead(file); + if (error) err(EX_IOERR, "%s", path); + + if (concatHead) { + error = concatFile(file, concatHead); + if (error) err(EX_IOERR, "%s", path); + } + + error = htmlIndexOpen(file); + 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); + if (error) err(EX_IOERR, "%s", path); + } + + error = htmlIndexClose(file) || fclose(file); + if (error) err(EX_IOERR, "%s", path); } -- cgit 1.4.1