summaryrefslogtreecommitdiff
path: root/sfeed_stats.c
blob: 3d594d50d9710d035e628eb000c00623299ca8ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

#include "util.h"

static struct feed *feeds = NULL; /* start of feeds linked-list. */
static char *line = NULL;

static void
cleanup(void) {
	free(line); /* free line */
	feedsfree(feeds); /* free feeds linked-list */
}

static void /* print error message to stderr */
die(const char *s) {
	fputs("sfeed_stats: ", stderr);
	fputs(s, stderr);
	fputc('\n', stderr);
	exit(EXIT_FAILURE);
}

int
main(void) {
	char *fields[FieldLast], timenewestformat[64] = "";
	unsigned long totalfeeds = 0, totalnew = 0, totalitems = 0;
	unsigned int islink, isnew;
	struct feed *f, *feedcurrent = NULL;
	time_t parsedtime, comparetime, timenewest = 0;
	size_t size = 0;

	atexit(cleanup);
	comparetime = time(NULL) - (3600 * 24); /* 1 day is old news */

	if(!(feedcurrent = calloc(1, sizeof(struct feed))))
		die("can't allocate enough memory");
	feeds = feedcurrent;

	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. */
		/* TODO: allocate feedcurrent before here, feedcurrent can be NULL */
		if(!totalfeeds || (feedcurrent && strcmp(feedcurrent->name, fields[FieldFeedName]))) {
			if(!(f = calloc(1, sizeof(struct feed))))
				die("can't allocate enough memory");
			if(totalfeeds) { /* end previous one. */
				feedcurrent->next = f;
				feedcurrent = f;
			} else {
				feedcurrent = f;
				feeds = feedcurrent; /* first item. */
			}
			if(parsedtime > timenewest) {
				timenewest = parsedtime;
				strlcpy(timenewestformat, fields[FieldTimeFormatted],
				        sizeof(timenewestformat));
			}
			if(parsedtime > feedcurrent->timenewest) {
				feedcurrent->timenewest = parsedtime;
				strlcpy(feedcurrent->timenewestformat, fields[FieldTimeFormatted],
				        sizeof(feedcurrent->timenewestformat));
			}

			/* TODO: memcpy and make feedcurrent->name static? */
			if(!(feedcurrent->name = strdup(fields[FieldFeedName])))
				die("can't allocate enough memory");

			totalfeeds++;
		}
		totalnew += isnew;
		feedcurrent->totalnew += isnew;
		feedcurrent->total++;
		totalitems++;
	}
	for(feedcurrent = feeds; feedcurrent; feedcurrent = feedcurrent->next) {
		if(!feedcurrent->name || feedcurrent->name[0] == '\0')
			continue;
		fprintf(stdout, "%c %-20.20s [%4lu/%-4lu]",
		        feedcurrent->totalnew > 0 ? 'N' : ' ',
		        feedcurrent->name, feedcurrent->totalnew, feedcurrent->total);
		if(feedcurrent->timenewestformat && feedcurrent->timenewestformat[0])
			fprintf(stdout, " %s", feedcurrent->timenewestformat);
		putchar('\n');
	}
	printf("  ================================\n");
	printf("%c %-20.20s [%4lu/%-4lu] %s\n", totalnew > 0 ? 'N' : ' ', "Total:",
	       totalnew, totalitems, timenewestformat);
	return EXIT_SUCCESS;
}