#include #include #include #include "config.h" #include "regexset.h" #include "channel.h" #include "user.h" #define NICK_BUFFER_SIZE 0x100 #define TEXT_BUFFER_SIZE 0x400 #define LINE_BUFFER_SIZE 0x400 int main(int argc, char **argv) { /* Regex sets must be initialized before config. */ rs_init(); channel_init(); if(!cfg_init()) { /* Free any registered regex sets and channels when config fails. Config will fail if a regex set fails to compile all parts. */ rs_free(); channel_free(); return 1; } user_init(); /* Parsing stuff goes here. */ for(int chan_i = 0; chan_i < channel_get_count(); chan_i++) { struct channel_t *channel = channel_get(chan_i); printf("Channel %s\n", channel->name); struct channel_file_t *file = channel->files; while(file) { struct regexset_t *rs = file->rs; FILE *f = fopen(file->path, "r"); if(!f) { fprintf(stderr, "\tFailed to open %s\n", file->path); file = file->next; continue; } else printf("\tParsing %s\n", file->path); char line[LINE_BUFFER_SIZE]; while(fgets(line, LINE_BUFFER_SIZE, f)) { int rc; int ovector[30]; rc = pcre_exec(rs->text, NULL, line, strlen(line), 0, 0, ovector, 30); if(rc > 0) { char nick[NICK_BUFFER_SIZE], text[TEXT_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); struct user_t *user = user_get(nick); user->lines++; /* Count words. */ char word[TEXT_BUFFER_SIZE]; char *end = strchr(text, '\0'); *word = '\0'; int len = 0; for(char *pos = text; pos < end; pos++) { if(isblank(*pos)) { user->words++; word[len] = '\0'; len = 0; *word = '\0'; } else if isalpha(*pos) { word[len++] = *pos; } else { len = 0; *word = '\0'; } } if(len) user->words++; continue; } rc = pcre_exec(rs->join, NULL, line, strlen(line), 0, 0, ovector, 30); if(rc > 0) { char nick[NICK_BUFFER_SIZE]; pcre_copy_named_substring(rs->join, line, ovector, rc, "nick", nick, NICK_BUFFER_SIZE); struct user_t *user = user_get(nick); continue; } } fclose(f); file = file->next; } } user_free(); cfg_free(); channel_free(); rs_free(); return 0; }