diff options
Diffstat (limited to 'config.c')
-rw-r--r-- | config.c | 60 |
1 files changed, 60 insertions, 0 deletions
@@ -4,6 +4,7 @@ #include <libconfig.h> #include "regexset.h" +#include "channel.h" config_t config; @@ -42,6 +43,65 @@ int cfg_init() { return 0; } + config_setting_t *channels_setting = config_lookup(&config, "channels"); + if(!config_setting_is_aggregate(channels_setting)) { + fprintf(stderr, "Setting \"channels\" must be an aggregate type.\n"); + return 0; + } + + int channel_count = config_setting_length(channels_setting); + for(int i = 0; i < channel_count; i++) { + config_setting_t *channel_setting = config_setting_get_elem(channels_setting, i); + const char *name; + if(!config_setting_lookup_string(channel_setting, "name", &name)) { + char *sname; + sname = malloc(sizeof(char) * 16); + sprintf(sname, "channel #%d", i+1); + name = sname; + } + printf("Channel %s\n", name); + struct channel_t *channel; + if(!(channel = channel_add(name))) { + return 0; + } + config_setting_t *files = config_setting_get_member(channel_setting, "files"); + if(!config_setting_is_aggregate(files)) { + fprintf(stderr, "Setting \"files\" must be an aggregate type (no files added).\n", name); + 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; + int rs_index; + config_setting_t *file = config_setting_get_elem(files, j); + /* (filepath, regexset) pair. */ + if(config_setting_is_aggregate(file)) { + if(config_setting_length(file) != 2) { + fprintf(stderr, "File lists must have exactly two elements.\n"); + continue; + } + config_setting_t *path_setting = config_setting_get_elem(file, 0); + config_setting_t *rs_setting = config_setting_get_elem(file, 1); + if(!(config_setting_type(path_setting) == CONFIG_TYPE_STRING && config_setting_type(rs_setting) == CONFIG_TYPE_INT)) { + fprintf(stderr, "File lists must have a string and an integer.\n"); + continue; + } + filepath = config_setting_get_string(path_setting); + rs_index = config_setting_get_int(rs_setting); + } else { /* filepath only. */ + filepath = config_setting_get_string(file); + rs_index = 0; + } + if(!channel_set_file(channel, j, filepath, rs_index)) { + fprintf(stderr, "Failed to set file #%d (%s) for channel %s.\n", j+1, filepath, name); + } + } + } + return 1; } |