diff options
| author | Jon Bergli Heier <snakebite@jvnv.net> | 2009-08-13 15:25:47 +0200 | 
|---|---|---|
| committer | Jon Bergli Heier <snakebite@jvnv.net> | 2009-08-13 15:25:47 +0200 | 
| commit | 150589a813b8ab1cbcd7697c2b8f48a6adcab2d2 (patch) | |
| tree | d90883ff8243726a8182f3c6644c5490559f704f | |
| parent | d436d19acb8a313f159aa67769dd49f7a369eb17 (diff) | |
Store files as a linked list.
| -rw-r--r-- | channel.c | 42 | ||||
| -rw-r--r-- | channel.h | 5 | ||||
| -rw-r--r-- | config.c | 5 | 
3 files changed, 19 insertions, 33 deletions
@@ -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; @@ -6,18 +6,17 @@  struct channel_file_t {  	char *path;  	struct regexset_t *rs; +	struct channel_file_t *next;  };  struct channel_t {  	char *name; -	int file_count;  	struct channel_file_t *files;  };  void channel_init();  struct channel_t *channel_add(const char *name); -int channel_set_file_count(struct channel_t *channel, int count); -int channel_set_file(struct channel_t *channel, int index, const char *path, int rs_index); +struct channel_file_t *channel_file_add(struct channel_t *channel, const char *path, int rs_index);  void channel_free();  #endif @@ -70,9 +70,6 @@ int cfg_init() {  			continue;  		}  		int file_count = config_setting_length(files); -		/* Break if we can't set file count, most likely any following channels will also fail. */ -		if(!channel_set_file_count(channel, file_count)) -			break;  		/* Iterate through files. */  		for(int j = 0; j < file_count; j++) {  			const char *filepath; @@ -96,7 +93,7 @@ int cfg_init() {  				filepath = config_setting_get_string(file);  				rs_index = 0;  			} -			if(!channel_set_file(channel, j, filepath, rs_index)) { +			if(!channel_file_add(channel, filepath, rs_index)) {  				fprintf(stderr, "Failed to set file #%d (%s) for channel %s.\n", j+1, filepath, name);  			}  		}  | 
