diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | main.c | 3 | ||||
-rw-r--r-- | nick.c | 85 | ||||
-rw-r--r-- | nick.h | 20 |
4 files changed, 109 insertions, 1 deletions
@@ -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) @@ -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(); @@ -0,0 +1,85 @@ +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <stdio.h> + +#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); +} @@ -0,0 +1,20 @@ +#ifndef _NICK_H_ +#define _NICK_H_ + +#include <pcre.h> + +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 |