From 07e2208a51d7bc64d4639c6eb56a4c33d8d87d7d Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 21 Aug 2009 02:36:28 +0200 Subject: Added structs and functions for nick grouping. --- Makefile | 2 +- main.c | 3 +++ nick.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ nick.h | 20 +++++++++++++++ 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 nick.c create mode 100644 nick.h diff --git a/Makefile b/Makefile index 34f989a..193627d 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ CFLAGS += $(shell xml2-config --cflags) LDFLAGS += $(shell pkg-config --libs libconfig) LDFLAGS += $(shell pcre-config --libs) LDFLAGS += $(shell xml2-config --libs) -OBJECTS = main.o config.o regexset.o channel.o user.o word.o sdbm.o export_xml.o +OBJECTS = main.o config.o regexset.o channel.o user.o word.o sdbm.o export_xml.o nick.o TARGET = ircstats all: $(TARGET) diff --git a/main.c b/main.c index 1ccca8a..69435ac 100644 --- a/main.c +++ b/main.c @@ -9,6 +9,7 @@ #include "user.h" #include "word.h" #include "export_xml.h" +#include "nick.h" #define NICK_BUFFER_SIZE 0x100 #define TEXT_BUFFER_SIZE 0x400 @@ -26,6 +27,7 @@ int main(int argc, char **argv) { channel_free(); return 1; } + nick_init(); /* Parsing stuff goes here. */ for(int chan_i = 0; chan_i < channel_get_count(); chan_i++) { @@ -115,6 +117,7 @@ int main(int argc, char **argv) { word_free(); } + nick_free(); cfg_free(); channel_free(); rs_free(); diff --git a/nick.c b/nick.c new file mode 100644 index 0000000..f1d2c38 --- /dev/null +++ b/nick.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include + +#include "nick.h" + +struct nick_t *nicks; + +void nick_init() { + nicks = NULL; +} + +int nick_add(char *name) { + struct nick_t *nick = malloc(sizeof(struct nick_t)); + if(!nick) { + char *error = strerror(errno); + fprintf(stderr, "Could not allocate memory for nick: %s\n", error); + return 0; + } + + if(nicks) { + struct nick_t *n = nicks; + while(n->next) n = n->next; + n->next = nick; + } else + nicks = nick; + + nick->name = strdup(name); + nick->regexes = NULL; + nick->next = NULL; + + return 1; +} + +int nick_regex_add(struct nick_t *nick, char *re_s) { + struct nick_regex_t *nre = malloc(sizeof(struct nick_regex_t)); + if(!nre) { + char *error = strerror(errno); + fprintf(stderr, "Could not allocate memory for nick regex: %s\n", error); + return 0; + } + + const char *error; + int erroffset; + nre->re = pcre_compile(re_s, 0, &error, &erroffset, NULL); + if(!nre->re) { + /* Copied from re_error in regexset.c */ + fprintf(stderr, "Nick RE failed to compile: %s\n", error); + fprintf(stderr, "%s\n", re_s); + for(int i = 0; i < erroffset; i++) fprintf(stderr, " "); + fprintf(stderr, "^\n"); + free(nre); + return 0; + } + + nre->next = NULL; + + if(nick->regexes) { + struct nick_regex_t *n = nick->regexes; + while(n->next) n = n->next; + n->next = nre; + } else + nick->regexes = nre; + + return 1; +} + +void nick_free() { + struct nick_t *nick = nicks; + while(nick) { + free(nick->name); + struct nick_regex_t *re = nick->regexes; + while(re) { + pcre_free(re->re); + struct nick_regex_t *next = re->next; + free(re); + re = next; + } + struct nick_t *next = nick->next; + free(nick); + nick = next; + } + free(nicks); +} diff --git a/nick.h b/nick.h new file mode 100644 index 0000000..8cac6d8 --- /dev/null +++ b/nick.h @@ -0,0 +1,20 @@ +#ifndef _NICK_H_ +#define _NICK_H_ + +#include + +struct nick_t { + char *name; + struct nick_regex_t { + pcre *re; + struct nick_regex_t *next; + } *regexes; + struct nick_t *next; +}; + +void nick_init(); +int nick_add(char *name); +int nick_regex_add(struct nick_t *nick, char *re_s); +void nick_free(); + +#endif -- cgit v1.2.3