commit 1521a6495499ede707eb6cbdf5c21a8dd24f6b31
parent 045ce43755cb86538a781931764b6136c8cb6905
Author: Paco Esteban <paco@e1e0.net>
Date: Mon, 29 Jun 2020 16:43:34 +0200
add sorting function.
requirement for rss and index generation
Diffstat:
M | ssnail.c | | | 40 | ++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 40 insertions(+), 0 deletions(-)
diff --git a/ssnail.c b/ssnail.c
@@ -19,6 +19,7 @@
#include <sys/syslimits.h>
#include <sys/types.h>
+#include <assert.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
@@ -62,6 +63,7 @@ static const struct ssnail_error *process_dir(char *, char *, int);
static const struct ssnail_error *write_html(struct article *, char *, char *);
static int gen_html(struct article *);
+static int article_sort(struct listhead *);
__dead static void
usage(void)
@@ -141,6 +143,11 @@ main(int argc, char *argv[])
if ((error = process_dir(srcdir, dstdir, force)) != NULL)
goto done;
+ if (article_sort(&head) == -1) {
+ error = ssnail_error_msg(2, "sort error");
+ goto done;
+ }
+
printf("Generate html files ... \n");
LIST_FOREACH(ap, &head, entries) {
if ((ap->src_mtime > ap->dst_mtime) || force) {
@@ -436,3 +443,36 @@ out:
closedir(dirp);
return error;
}
+
+static int
+article_sort(struct listhead *h)
+{
+ struct article *a1 = NULL, *a2 = NULL;
+ int count_elems = 0;
+
+ assert(h != NULL);
+
+ if (LIST_EMPTY(h))
+ return -1;
+
+ /* count list elemets */
+ LIST_FOREACH(a1, h, entries) {
+ count_elems++;
+ }
+
+ for (int i = 0; i < count_elems; i++) {
+ a1 = LIST_FIRST(h);
+
+ /* move element to the "right" until is the smallest value */
+ /* or we're at the end of the list */
+ while ((a2 = LIST_NEXT(a1, entries)) != NULL) {
+ if (strcmp(a1->date, a2->date) < 0) {
+ LIST_REMOVE(a2, entries);
+ LIST_INSERT_BEFORE(a1, a2, entries);
+ } else
+ a1 = a2;
+ }
+ }
+
+ return 0;
+}