From 44f3e1486fb5fd45644683af83aaaf27047e4250 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 17 Aug 2009 13:53:58 +0200 Subject: Count lines per quarter hour for channels and users. --- channel.c | 1 + channel.h | 2 ++ main.c | 16 ++++++++++++++-- user.c | 3 ++- user.h | 2 +- 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/channel.c b/channel.c index 45bbb71..c69cf2b 100644 --- a/channel.c +++ b/channel.c @@ -23,6 +23,7 @@ struct channel_t *channel_add(const char *name) { struct channel_t *channel = &channels[channel_count-1]; channel->name = strdup(name); channel->files = NULL; + memset(channel->hours, 0, 24*4 * sizeof(unsigned long)); return channel; } diff --git a/channel.h b/channel.h index 73f2dde..0335782 100644 --- a/channel.h +++ b/channel.h @@ -12,6 +12,8 @@ struct channel_file_t { struct channel_t { char *name; struct channel_file_t *files; + + unsigned long hours[24*4]; }; void channel_init(); diff --git a/main.c b/main.c index d32b091..8175dbe 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -11,6 +12,7 @@ #define NICK_BUFFER_SIZE 0x100 #define TEXT_BUFFER_SIZE 0x400 #define LINE_BUFFER_SIZE 0x400 +#define TIME_BUFFER_SIZE 0xf int main(int argc, char **argv) { /* Regex sets must be initialized before config. */ @@ -48,11 +50,21 @@ int main(int argc, char **argv) { rc = pcre_exec(rs->text, rs->text_e, line, strlen(line), 0, 0, ovector, 30); if(rc > 0) { - char nick[NICK_BUFFER_SIZE], text[TEXT_BUFFER_SIZE]; + char nick[NICK_BUFFER_SIZE], text[TEXT_BUFFER_SIZE], hour_s[TIME_BUFFER_SIZE], min_s[TIME_BUFFER_SIZE]; pcre_copy_named_substring(rs->text, line, ovector, rc, "nick", nick, NICK_BUFFER_SIZE); pcre_copy_named_substring(rs->text, line, ovector, rc, "text", text, TEXT_BUFFER_SIZE); + pcre_copy_named_substring(rs->text, line, ovector, rc, "hour", hour_s, TIME_BUFFER_SIZE); + pcre_copy_named_substring(rs->text, line, ovector, rc, "minute", min_s, TIME_BUFFER_SIZE); struct user_t *user = user_get(nick); - user->lines++; + + /* Calculate array index for lines. */ + int hour, min, time_i; + hour = atoi(hour_s); + min = atoi(min_s); + time_i = hour*4 + min / 15; + + user->lines[time_i]++; + channel->hours[time_i]++; /* Count words. */ char word[TEXT_BUFFER_SIZE]; diff --git a/user.c b/user.c index a9e2927..aed1ea6 100644 --- a/user.c +++ b/user.c @@ -30,7 +30,8 @@ struct user_t *user_get(char *nick) { if(!user->nick) { user->hash = hash; user->nick = strdup(nick); - user->lines = user->words = 0; + memset(user->lines, 0, 24*4 * sizeof(unsigned long)); + user->words = 0; user->next = NULL; } diff --git a/user.h b/user.h index 0bf3686..042eb32 100644 --- a/user.h +++ b/user.h @@ -6,7 +6,7 @@ struct user_t { unsigned long hash; char *nick; - unsigned long lines; + unsigned long lines[24*4]; unsigned long long words; struct user_t *next; }; -- cgit v1.2.3