#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; }