From 93c935a639da55c1bc4d39e616588cc0f73df1e3 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Thu, 13 Aug 2009 02:23:33 +0200 Subject: Added channels. Minor fixes - known bugs! --- channel.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 channel.c (limited to 'channel.c') diff --git a/channel.c b/channel.c new file mode 100644 index 0000000..cab4612 --- /dev/null +++ b/channel.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#include "channel.h" + +struct channel_t *channels; +int channel_count; + +void channel_init() { + channels = NULL; + channel_count = 0; +} + +struct channel_t *channel_add(const char *name) { + channels = realloc(channels, ++channel_count * sizeof(struct regexset_t)); + if(!channels) { + char *error = strerror(errno); + fprintf(stderr, "Could not (re)allocate memory for channels: %s\n", error); + return NULL; + } + struct channel_t *channel = &channels[channel_count-1]; + channel->name = strdup(name); + channel->file_count = 0; + channel->files = NULL; + return channel; +} + +int channel_set_file_count(struct channel_t *channel, int count) { + channel->files = malloc(count * sizeof(struct channel_file_t)); + if(!channel->files) { + char *error = strerror(errno); + fprintf(stderr, "Could not allocate memory for channel files (%s): %s\n", channel->name, error); + return 0; + } + channel->file_count = count; + return 1; +} + +int channel_set_file(struct channel_t *channel, int index, const char *path, int rs_index) { + struct channel_file_t *file; + /* Make sure index is in range. */ + if(!(index < channel->file_count)) + return 0; + file = &channel->files[index]; + file->path = strdup(path); + file->rs = rs_get(rs_index); + /* Fail if we don't get a regex set. */ + if(!file->rs) + return 0; + return 1; +} + +void channel_free() { + for(int i = 0; i < channel_count; i++) { + free(channels[i].name); + /* Free all file path strings. */ + for(int j = 0; j < channels[i].file_count; j++) { + free(channels[i].files[j].path); + } + /* 'files' is a dynamically allocated array, must be freed. */ + free(channels[i].files); + } + free(channels); + channels = NULL; +} -- cgit v1.2.3