summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2009-08-15 02:51:11 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2009-08-15 02:51:11 +0200
commita245a2c83ce685c3dac57ef92f65f5a10d12c8b0 (patch)
tree5a34e2ec01cdc28097fdaef582f410added28c29
parentb8c88bbf4e928a0ac4af89dfe72f0661beb35827 (diff)
Word count using ctype.h to check for alphabetic and blank characters.
Use defines for buffer sizes. Added missing fclose. Return pointer to the user_t struct in user_get.
-rw-r--r--main.c41
-rw-r--r--user.c2
2 files changed, 38 insertions, 5 deletions
diff --git a/main.c b/main.c
index 79b1eec..02686ec 100644
--- a/main.c
+++ b/main.c
@@ -1,11 +1,16 @@
#include <stdio.h>
#include <string.h>
+#include <ctype.h>
#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();
@@ -34,25 +39,51 @@ int main(int argc, char **argv) {
} else
printf("\tParsing %s\n", file->path);
- char line[0x2ff];
- while(fgets(line, 0x2ff, f)) {
+ 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[0x20], text[0x200];
- pcre_copy_named_substring(rs->text, line, ovector, rc, "nick", nick, 0x20);
- pcre_copy_named_substring(rs->text, line, ovector, rc, "text", text, 0x200);
+ 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;
}
diff --git a/user.c b/user.c
index 68310e7..064007b 100644
--- a/user.c
+++ b/user.c
@@ -41,6 +41,8 @@ struct user_t *user_get(char *nick) {
user->lines = user->words = 0;
user->next = NULL;
}
+
+ return user;
}
void user_free() {