#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->files = NULL; return channel; } struct channel_file_t *channel_file_add(struct channel_t *channel, const char *path, int rs_index) { struct channel_file_t *file = malloc(sizeof(struct channel_file_t)); struct channel_file_t *last = channel->files; if(last) { while(last->next) last = last->next; last->next = file; } else channel->files = file; file->path = strdup(path); file->rs = rs_get(rs_index); file->next = NULL; if(!file->rs) return NULL; return file; } int channel_get_count() { return channel_count; } struct channel_t *channel_get(int index) { return (index < channel_count ? &channels[index] : NULL); } void channel_free() { for(int i = 0; i < channel_count; i++) { free(channels[i].name); struct channel_file_t *file = channels[i].files; while(file) { struct channel_file_t *next = file->next; free(file); file = next; } } free(channels); channels = NULL; }