changeset 14:06b2d0a67c4d

post: parse post time
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Wed, 04 Feb 2009 01:32:32 -0500
parents 76cee581f48b
children d2628fb14ec6
files Makefile post.c post.h sar.c
diffstat 4 files changed, 38 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Wed Feb 04 01:14:15 2009 -0500
+++ b/Makefile	Wed Feb 04 01:32:32 2009 -0500
@@ -1,5 +1,5 @@
 CC=gcc
-CFLAGS=-Wall -g -O2
+CFLAGS=-Wall -g -O2 -std=c99
 
 FILES=sar.c post.c xattr.c html.c
 
--- a/post.c	Wed Feb 04 01:14:15 2009 -0500
+++ b/post.c	Wed Feb 04 01:32:32 2009 -0500
@@ -1,3 +1,4 @@
+#define _XOPEN_SOURCE
 #include <stdlib.h>
 #include <stdio.h>
 #include <limits.h>
@@ -8,6 +9,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <time.h>
 
 #include "post.h"
 #include "xattr.h"
@@ -75,7 +77,9 @@
 	}
 
 	/* FIXME: do <p> insertion, etc. */
+	fwrite("<p>", 1, 3, post->out);
 	fwrite(ibuf, 1, statbuf.st_size, post->out);
+	fwrite("</p>", 1, 4, post->out);
 
 	munmap(ibuf, statbuf.st_size);
 
@@ -86,18 +90,26 @@
 int load_post(int postid, struct post *post)
 {
 	char path[FILENAME_MAX];
+	char *buf;
 
 	snprintf(path, FILENAME_MAX, "data/posts/%d", postid);
 
 	post->id = postid;
 	post->title = safe_getxattr(path, XATTR_TITLE);
 	post->cats  = safe_getxattr(path, XATTR_CATS);
-	post->time  = safe_getxattr(path, XATTR_TIME);
+	buf         = safe_getxattr(path, XATTR_TIME);
 
-	if (post->title && post->time)
+	if (post->title && buf && (strlen(buf) == 16)) {
+		/* "2005-01-02 03:03" */
+		strptime(buf, "%Y-%m-%d %H:%M", &post->time);
+
+		free(buf);
+
 		return 0;
+	}
 
 	destroy_post(post);
+	free(buf);
 	return ENOMEM;
 }
 
@@ -105,7 +117,6 @@
 {
 	free(post->title);
 	free(post->cats);
-	free(post->time);
 }
 
 void dump_post(struct post *post)
@@ -113,7 +124,9 @@
 	if (!post)
 		fprintf(stdout, "p=NULL\n");
 	else
-		fprintf(post->out, "p=%p { %d, '%s', '%s', '%s' }\n\n",
+		fprintf(post->out, "p=%p { %d, '%s', '%s', '%04d-%02d-%02d %02d:%02d' }\n\n",
 			post, post->id, post->title, post->cats,
-			post->time);
+			post->time.tm_year, post->time.tm_mon,
+			post->time.tm_mday, post->time.tm_hour,
+			post->time.tm_min);
 }
--- a/post.h	Wed Feb 04 01:14:15 2009 -0500
+++ b/post.h	Wed Feb 04 01:32:32 2009 -0500
@@ -1,12 +1,14 @@
 #ifndef __POST_H
 #define __POST_H
 
+#include <time.h>
+
 struct post {
 	FILE *out;
 	int id;
 	char *title;
 	char *cats;
-	char *time;
+	struct tm time;
 };
 
 #define XATTR_TITLE	"user.post_title"
--- a/sar.c	Wed Feb 04 01:14:15 2009 -0500
+++ b/sar.c	Wed Feb 04 01:32:32 2009 -0500
@@ -9,6 +9,11 @@
 	fprintf(post->out, "%d", post->id);
 }
 
+static char* up_month_strs[12] = {
+	"January", "February", "March", "April", "May", "June",
+	"July", "August", "September", "October", "November", "December",
+};
+
 static void echo_title(struct post *post)
 {
 	fprintf(post->out, "%s", post->title);
@@ -16,12 +21,20 @@
 
 static void echo_posttime(struct post *post)
 {
-	fprintf(post->out, "||posttime||");
+	fprintf(post->out, "%02d:%02d", post->time.tm_hour,
+		post->time.tm_min);
 }
 
 static void echo_postdate(struct post *post)
 {
-	fprintf(post->out, "||postdate||");
+	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)
+{
+	fprintf(post->out, "???"); // FIXME
 }
 
 static struct repltab_entry __repltab_html[] = {
@@ -29,6 +42,7 @@
 	{"POSTDATE",	echo_postdate},
 	{"POSTTIME",	echo_posttime},
 	{"TITLE",	echo_title},
+	{"COMCOUNT",	echo_comment_count},
 	{"",		NULL},
 };