changeset 19:b719cbbac8ba

story: display categories for the post
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Wed, 04 Feb 2009 14:51:10 -0500
parents 8c92d95661de
children b1965983e1c0
files html.c post.c post.h sar.c sar.h
diffstat 5 files changed, 95 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/html.c	Wed Feb 04 14:50:49 2009 -0500
+++ b/html.c	Wed Feb 04 14:51:10 2009 -0500
@@ -1,4 +1,6 @@
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
 #include "sar.h"
 #include "html.h"
@@ -6,14 +8,58 @@
 /************************************************************************/
 /*                                POST                                  */
 /************************************************************************/
+static void __invoke_for_each_cat(struct post *post, void(*f)(struct post*, char*))
+{
+	char *obuf;
+	char tmp;
+	int iidx, oidx;
+	int done;
+
+	if (!post->cats)
+		return;
+
+	obuf = malloc(strlen(post->cats));
+	if (!obuf) {
+		fprintf(post->out, "ERROR: could not alloc memory\n");
+		return;
+	}
+
+	iidx = 0;
+	oidx = 0;
+	done = 0;
+	while(!done) {
+		tmp = post->cats[iidx];
+
+		switch(tmp) {
+			case '\0':
+				done = 1;
+				/* fall-thourgh */
+			case ',':
+				COPYCHAR(obuf, oidx, '\0');
+				f(post, obuf);
+				oidx=0;
+				break;
+			default:
+				COPYCHAR(obuf, oidx, tmp);
+				break;
+		}
+
+		iidx++;
+	}
+
+	free(obuf);
+}
+
+static void __story_cat_item(struct post *post, char *catname)
+{
+	cat(post, catname, "templates/story-cat-item.html", repltab_cat_html);
+}
+
 void html_story(struct post *post)
 {
 	cat(post, NULL, "templates/story-top.html", repltab_html);
 
-#if 0
-	for_each_category(post)
-		cat(stdout, "templates/story-cat-item.html", repltab_html);
-#endif
+	__invoke_for_each_cat(post, __story_cat_item);
 
 	cat(post, NULL, "templates/story-middle.html", repltab_html);
 
--- a/post.c	Wed Feb 04 14:50:49 2009 -0500
+++ b/post.c	Wed Feb 04 14:51:10 2009 -0500
@@ -16,7 +16,7 @@
 #include "xattr.h"
 #include "sar.h"
 
-void cat(struct post *post, struct comment *comm, char *tmpl,
+void cat(struct post *post, void *data, char *tmpl,
 	 struct repltab_entry *repltab)
 {
 	struct stat statbuf;
@@ -42,7 +42,7 @@
 		goto out_close;
 	}
 
-	sar(post, comm, ibuf, statbuf.st_size, repltab);
+	sar(post, data, ibuf, statbuf.st_size, repltab);
 
 	munmap(ibuf, statbuf.st_size);
 
--- a/post.h	Wed Feb 04 14:50:49 2009 -0500
+++ b/post.h	Wed Feb 04 14:51:10 2009 -0500
@@ -33,7 +33,7 @@
 
 extern void cat_post(struct post *post);
 extern void cat_post_comment(struct post *post, struct comment *comm);
-extern void cat(struct post *post, struct comment *comm, char *tmpl,
+extern void cat(struct post *post, void *data, char *tmpl,
 		struct repltab_entry *repltab);
 
 extern void invoke_for_each_comment(struct post *post,
--- a/sar.c	Wed Feb 04 14:50:49 2009 -0500
+++ b/sar.c	Wed Feb 04 14:51:10 2009 -0500
@@ -6,7 +6,7 @@
 #include "post.h"
 #include "sar.h"
 
-static void echo_postid(struct post *post, struct comment *comm)
+static void echo_postid(struct post *post, void *data)
 {
 	fprintf(post->out, "%d", post->id);
 }
@@ -16,25 +16,25 @@
 	"July", "August", "September", "October", "November", "December",
 };
 
-static void echo_title(struct post *post, struct comment *comm)
+static void echo_title(struct post *post, void *data)
 {
 	fprintf(post->out, "%s", post->title);
 }
 
-static void echo_posttime(struct post *post, struct comment *comm)
+static void echo_posttime(struct post *post, void *data)
 {
 	fprintf(post->out, "%02d:%02d", post->time.tm_hour,
 		post->time.tm_min);
 }
 
-static void echo_postdate(struct post *post, struct comment *comm)
+static void echo_postdate(struct post *post, void *data)
 {
 	fprintf(post->out, "%s %d, %04d",
 		up_month_strs[post->time.tm_mon], post->time.tm_mday,
 		1900+post->time.tm_year);
 }
 
-static void echo_comment_count(struct post *post, struct comment *comm)
+static void echo_comment_count(struct post *post, void *data)
 {
 	char path[FILENAME_MAX];
 	struct dirent *de;
@@ -70,24 +70,32 @@
 	{"",		NULL},
 };
 
-static void echo_comment_id(struct post *post, struct comment *comm)
+static void echo_comment_id(struct post *post, void *data)
 {
+	struct comment *comm = data;
+
 	fprintf(post->out, "%d", comm->id);
 }
 
-static void echo_comment_author(struct post *post, struct comment *comm)
+static void echo_comment_author(struct post *post, void *data)
 {
+	struct comment *comm = data;
+
 	fprintf(post->out, "%s", comm->author);
 }
 
-static void echo_comment_time(struct post *post, struct comment *comm)
+static void echo_comment_time(struct post *post, void *data)
 {
+	struct comment *comm = data;
+
 	fprintf(post->out, "%02d:%02d", comm->time.tm_hour,
 		comm->time.tm_min);
 }
 
-static void echo_comment_date(struct post *post, struct comment *comm)
+static void echo_comment_date(struct post *post, void *data)
 {
+	struct comment *comm = data;
+
 	fprintf(post->out, "%s %d, %04d",
 		up_month_strs[comm->time.tm_mon], comm->time.tm_mday,
 		1900+comm->time.tm_year);
@@ -106,10 +114,23 @@
 	{"",		NULL},
 };
 
+static void echo_cat_name(struct post *post, void *data)
+{
+	char *name = data;
+
+	fprintf(post->out, "%s", name);
+}
+
+static struct repltab_entry __repltab_cat_html[] = {
+	{"CATNAME",	echo_cat_name},
+	{"",		NULL},
+};
+
 struct repltab_entry *repltab_html = __repltab_html;
 struct repltab_entry *repltab_comm_html = __repltab_comm_html;
+struct repltab_entry *repltab_cat_html = __repltab_cat_html;
 
-static int invoke_repl(struct post *post, struct comment *comm, char *cmd,
+static int invoke_repl(struct post *post, void *data, char *cmd,
 		       struct repltab_entry *repltab)
 {
 	int i;
@@ -121,7 +142,7 @@
 		if (strcmp(cmd, repltab[i].what))
 			continue;
 
-		repltab[i].f(post, comm);
+		repltab[i].f(post, data);
 		return 0;
 	}
 
@@ -134,12 +155,7 @@
 #define SAR_SPECIAL	3
 #define SAR_ERROR	4
 
-#define COPYCHAR(ob, oi, c)	do { \
-					ob[oi] = c; \
-					oi++; \
-				} while(0)
-
-void sar(struct post *post, struct comment *comm, char *ibuf, int size,
+void sar(struct post *post, void *data, char *ibuf, int size,
 	 struct repltab_entry *repltab)
 {
 	char obuf[size];
@@ -189,7 +205,7 @@
 					state = SAR_ERROR;
 				else {
 					COPYCHAR(cmd, cidx, '\0');
-					if (invoke_repl(post, comm, cmd, repltab))
+					if (invoke_repl(post, data, cmd, repltab))
 						fprintf(post->out, "@@%s@@", cmd);
 					cidx = 0;
 					state = SAR_NORMAL;
--- a/sar.h	Wed Feb 04 14:50:49 2009 -0500
+++ b/sar.h	Wed Feb 04 14:51:10 2009 -0500
@@ -5,13 +5,19 @@
 
 struct repltab_entry {
 	char what[16];
-	void (*f)(struct post*, struct comment*);
+	void (*f)(struct post*, void*);
 };
 
-extern void sar(struct post *post, struct comment *comm, char *ibuf,
+extern void sar(struct post *post, void *data, char *ibuf,
 		int size, struct repltab_entry *repltab);
 
 extern struct repltab_entry *repltab_html;
 extern struct repltab_entry *repltab_comm_html;
+extern struct repltab_entry *repltab_cat_html;
+
+#define COPYCHAR(ob, oi, c)	do { \
+					ob[oi] = c; \
+					oi++; \
+				} while(0)
 
 #endif