From 150589a813b8ab1cbcd7697c2b8f48a6adcab2d2 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Thu, 13 Aug 2009 15:25:47 +0200 Subject: Store files as a linked list. --- channel.c | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) (limited to 'channel.c') diff --git a/channel.c b/channel.c index cab4612..cdd1748 100644 --- a/channel.c +++ b/channel.c @@ -22,45 +22,35 @@ struct channel_t *channel_add(const char *name) { } 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]; +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); - /* Fail if we don't get a regex set. */ + file->next = NULL; if(!file->rs) - return 0; - return 1; + return NULL; + return file; } 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); + struct channel_file_t *file = channels[i].files; + while(file) { + struct channel_file_t *next = file->next; + free(file); + file = next; } - /* 'files' is a dynamically allocated array, must be freed. */ - free(channels[i].files); } free(channels); channels = NULL; -- cgit v1.2.3