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 From bd1f1deafc7f1fec1612d3776377f685f056e5f7 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 14 Aug 2009 01:25:46 +0200 Subject: Main loop groundwork. --- channel.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'channel.c') diff --git a/channel.c b/channel.c index cdd1748..065e606 100644 --- a/channel.c +++ b/channel.c @@ -42,6 +42,14 @@ struct channel_file_t *channel_file_add(struct channel_t *channel, const char *p 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); -- cgit v1.2.3