From 3589cf3db4b783e84d54b68d8a8096f92cc552fb Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Mon, 20 May 2013 19:34:45 +0200 Subject: sfeed_html: lots changed and cleanup, see CHANGELOG Signed-off-by: Hiltjo Posthuma --- sfeed_html.c | 180 ++++++++++++++--------------------------------------------- 1 file changed, 43 insertions(+), 137 deletions(-) diff --git a/sfeed_html.c b/sfeed_html.c index 0dbb7c9..989ddff 100644 --- a/sfeed_html.c +++ b/sfeed_html.c @@ -3,15 +3,8 @@ #include #include #include -#include "common.c" - -/* Feed info. */ -struct feed { - char *name; /* feed name */ - unsigned long new; /* amount of new items per feed */ - unsigned long total; /* total items */ - struct feed *next; /* linked list */ -}; +#include "common.h" +#include "compat.h" static int showsidebar = 1; /* show sidebar ? */ @@ -23,173 +16,84 @@ die(const char *s) { exit(EXIT_FAILURE); } -struct feed * -feednew(void) { - struct feed *f; - if(!(f = calloc(1, sizeof(struct feed)))) - die("can't allocate enough memory"); - return f; -} - -void -feedsfree(struct feed *f) { - struct feed *next; - while(f) { - next = f->next; - free(f->name); - free(f); - f = next; - } -} - -/* print feed name for id; spaces and tabs in string as "-" (spaces in anchors are not valid). */ -void -printfeednameid(const char *s) { - for(; *s; s++) - putchar(isspace(*s) ? '-' : *s); -} - -void -printhtmlencoded(const char *s) { - for(; *s; s++) { - switch(*s) { - case '<': fputs("<", stdout); break; - case '>': fputs(">", stdout); break; - case '&': fputs("&", stdout); break; - default: - putchar(*s); - } - } -} - int main(void) { char *line = NULL, *fields[FieldLast]; unsigned long totalfeeds = 0, totalnew = 0; - unsigned int islink, isnew; + int islink, isnew; struct feed *feedcurrent = NULL, *feeds = NULL; /* start of feeds linked-list. */ time_t parsedtime, comparetime; size_t size = 0; - tzset(); comparetime = time(NULL) - (3600 * 24); /* 1 day is old news */ fputs( "\n" "\n" " \n" " \n" - " \n" + " \n" " \n" - " \n" - "
\n", + " \n", stdout); - while(parseline(&line, &size, fields, FieldLast, stdin, FieldSeparator) > 0) { + while(parseline(&line, &size, fields, FieldLast, '\t', stdin) > 0) { + parsedtime = (time_t)strtol(fields[FieldUnixTimestamp], NULL, 10); + isnew = (parsedtime >= comparetime); + islink = (fields[FieldLink][0] != '\0'); /* first of feed section or new feed section. */ if(!totalfeeds || strcmp(feedcurrent->name, fields[FieldFeedName])) { if(totalfeeds) { /* end previous one. */ fputs("\n", stdout); - feedcurrent->next = feednew(); + if(!(feedcurrent->next = calloc(1, sizeof(struct feed)))) + die("can't allocate enough memory"); feedcurrent = feedcurrent->next; } else { - feedcurrent = feednew(); + if(!(feedcurrent = calloc(1, sizeof(struct feed)))) + die("can't allocate enough memory"); feeds = feedcurrent; /* first item. */ - fputs("\t\t
\n", stdout); showsidebar = 0; - } - fputs("\">\n", stdout); + } else + fputs("\t\t
\n", stdout); } - if(!(feedcurrent->name = strdup(fields[FieldFeedName]))) + if(!(feedcurrent->name = xstrdup(fields[FieldFeedName]))) die("can't allocate enough memory"); if(fields[FieldFeedName][0] != '\0') { fputs("

name); + printfeednameid(feedcurrent->name, stdout); fputs("\">name); + printfeednameid(feedcurrent->name, stdout); fputs("\">", stdout); fputs(feedcurrent->name, stdout); fputs("

\n", stdout); } - fputs("", stdout); + fputs("
\n", stdout); totalfeeds++; } - parsedtime = (time_t)strtol(fields[FieldUnixTimestamp], NULL, 10); - isnew = (parsedtime >= comparetime); - islink = (strlen(fields[FieldLink]) > 0); totalnew += isnew; - feedcurrent->new += isnew; + feedcurrent->totalnew += isnew; feedcurrent->total++; - fputs("
", stdout); + if(isnew) + fputs("
", stdout); + else + fputs("
", stdout); fputs(fields[FieldTimeFormatted], stdout); - fputs("", stdout); + fputs("", stdout); + if(isnew) fputs("", stdout); if(islink) { fputs("", stdout); } - printhtmlencoded(fields[FieldTitle]); + printhtmlencoded(fields[FieldTitle], stdout); if(islink) fputs("", stdout); if(isnew) @@ -201,27 +105,29 @@ main(void) { fputs("\t\t\n", stdout); /* div items */ } if(showsidebar) { - fputs("\t\t
\n\t\t\t\n\t
\n", stdout); } fputs( - " \n" " \n" - " Newsfeeds (", + " <title>Newsfeed (", stdout); fprintf(stdout, "%lu", totalnew); fputs(")\n", stdout); -- cgit v1.2.3