From bf2ffd03740894bd7592b7a1bbd2c9101e47e888 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Thu, 2 Jan 2020 16:13:48 +1030 Subject: start generating page (blog navigation) bar really needs to be cleaned up though --- pages.c | 38 +++++++++++++++++++++++++++++++++++++- pages.h | 1 + 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/pages.c b/pages.c index ae845e3..da437a2 100644 --- a/pages.c +++ b/pages.c @@ -463,7 +463,6 @@ createdirectpages(int *posts, size_t totalposts) char file[512]; char source[512]; - char buff[128]; for (int x = 1; x < totalposts; x++) { memset(source, 0, 1); @@ -489,6 +488,40 @@ createdirectpages(int *posts, size_t totalposts) } return 1; } +#include +char +*generatepagebar(char *bar, size_t size, int *posts, size_t totalposts, int currentpage) +{ + /* + * create a navigation bar for the posts pages highlighting the 'currentpage' link + * store it in bar and return it, NULL on error + */ + char buff[size]; + int i; + size_t freespace = size; + /* determine how many pages we need */ + int pagecount = (totalposts%posts_per_page == 0) ? totalposts/posts_per_page : totalposts/posts_per_page+1; + /* gross hack, sxprintf returns the number of chars written, keep track of our freespace */ + freespace -= snprintf(buff, freespace, "
prev", (currentpage == 1) ? pagecount : currentpage-1); + strncat(bar, buff, freespace); + for (int i = 1; i < pagecount; i++) + { + // FIXME: this doesn't need to be an if/else.. + if (currentpage == i) + { + freespace -= snprintf(buff, freespace, "%d ", i, i); + } + else + { + freespace -= snprintf(buff, freespace, "%d ", i, i); + } + strncat(bar, buff, freespace); + } + freespace -= snprintf(buff, freespace, "
next", (currentpage == pagecount) ? 1 : currentpage+1); + strncat(bar, buff, freespace); + printf("%s: %ld pages = %d\n", bar, freespace, pagecount); + return bar; +} int postspage(int flags) @@ -537,11 +570,14 @@ postspage(int flags) /* sort posts */ qsort(posts, totalposts, sizeof(int), postscompare); + /* create direct link pages */ if (!createdirectpages(posts, totalposts)) { fprintf(stderr, "unable to create direct post pages, unrecoverable failure\n"); return 0; } + char pagebar[1024] = {0}; + generatepagebar(pagebar, 1024, posts, totalposts, 1); return 1; } diff --git a/pages.h b/pages.h index e61eeb4..106938d 100644 --- a/pages.h +++ b/pages.h @@ -43,6 +43,7 @@ int makedirectories(const char *basedir, const char *file); int postscompare(const void *a, const void *b); int genericpage(int flags, const char *ind, const char *out, const char *tit, const char *inf); int createdirectpages(int *posts, size_t totalposts); +char *generatepagebar(char *bar, size_t size, int *posts, size_t totalposts, int currentpage); /* generators (to be put into the pages array) */ int frontpage(int flags); -- cgit v1.2.3