Line data Source code
1 : #include <minipot_config_load.h> 2 : #include <string.h> 3 : #include "log.h" 4 : 5 : int minipot_config_load_from_file(config_t *cfg, const char *file) { 6 0 : config_init(cfg); 7 0 : if (config_read_file(cfg, file) != CONFIG_TRUE) { 8 0 : switch(config_error_type(cfg)) { 9 0 : case CONFIG_ERR_FILE_IO: 10 0 : warning("Couldn't open config file: %s", file); 11 0 : return 0; 12 0 : case CONFIG_ERR_PARSE: 13 0 : critical("Unable to parse configuration file '%s' (failed on line %d due to: %s)", 14 : file, config_error_line(cfg), config_error_text(cfg)); 15 0 : default: 16 0 : critical("Unknown error '%d' while reading file: %s", 17 : config_error_type(cfg), file); 18 : } 19 : } 20 : return 1; 21 : } 22 : 23 0 : static const char *config_type_name(short type) { 24 0 : switch (type) { 25 : case CONFIG_TYPE_NONE: 26 : return "none"; 27 0 : case CONFIG_TYPE_GROUP: 28 0 : return "group"; 29 0 : case CONFIG_TYPE_INT: 30 0 : return "int"; 31 0 : case CONFIG_TYPE_INT64: 32 0 : return "int64"; 33 0 : case CONFIG_TYPE_FLOAT: 34 0 : return "float"; 35 0 : case CONFIG_TYPE_STRING: 36 0 : return "string"; 37 0 : case CONFIG_TYPE_BOOL: 38 0 : return "bool"; 39 0 : case CONFIG_TYPE_ARRAY: 40 0 : return "array"; 41 0 : case CONFIG_TYPE_LIST: 42 0 : return "list"; 43 0 : default: 44 0 : return "UNKNOWN"; 45 : } 46 : } 47 : 48 : void minipot_config_parse_setting(struct minipot_config *minipot_config, 49 : config_setting_t *setting) { 50 0 : if (setting == NULL) { 51 0 : warning("No log configuration provided in configuration file."); 52 0 : return; 53 : } 54 : 55 0 : const config_setting_t *user_st = config_setting_lookup(setting, "user"); 56 0 : if (user_st && !minipot_config->user) { 57 0 : const char *user = config_setting_get_string(user_st); 58 0 : if (user) 59 0 : minipot_config->user = strdup(user); 60 : else 61 0 : error("Setting: user in file: %s on line: %d has invalid type: %s." 62 : "It must be string.", 63 : config_setting_source_file(user_st), 64 : config_setting_source_line(user_st), 65 : config_type_name(config_setting_type(user_st))); 66 : } 67 : 68 0 : const config_setting_t *topic_st = config_setting_lookup(setting, "topic"); 69 0 : if (topic_st && !minipot_config->topic) { 70 0 : const char *topic = config_setting_get_string(topic_st); 71 0 : if (topic) 72 0 : minipot_config->topic = strdup(topic); 73 : else 74 0 : error("Setting: topic in file: %s on line: %d has invalid type: %s." 75 : "It must be string.", 76 : config_setting_source_file(topic_st), 77 : config_setting_source_line(topic_st), 78 : config_type_name(config_setting_type(topic_st))); 79 : } 80 : 81 0 : const config_setting_t *endpoint_st = config_setting_lookup(setting, "endpoint"); 82 0 : if (endpoint_st && !minipot_config->zmq_endopint) { 83 0 : const char *endpoint = config_setting_get_string(endpoint_st); 84 0 : if (endpoint) 85 0 : minipot_config->zmq_endopint = strdup(endpoint); 86 : else 87 0 : error("Setting: endpoint in file: %s on line: %d has invalid type: %s." 88 : "It must be string.", 89 : config_setting_source_file(endpoint_st), 90 : config_setting_source_line(endpoint_st), 91 : config_type_name(config_setting_type(endpoint_st))); 92 : } 93 : 94 0 : const config_setting_t *ports_st = config_setting_lookup(setting, "ports"); 95 0 : if (ports_st && !minipot_config->ports_cnt) { 96 0 : if (config_setting_is_array(ports_st) == CONFIG_TRUE) { 97 0 : int array_len = config_setting_length(ports_st); 98 0 : for (int i = 0; i < array_len; i++) { 99 0 : int port = config_setting_get_int_elem(ports_st, i); 100 0 : if (port < 1 || port > 65535) { 101 0 : error("Port defined in file: %s on line: %d must be integer" 102 : " in range <1, 65535> !", 103 : config_setting_source_file(ports_st), 104 : config_setting_source_line(ports_st)); 105 : } else { 106 0 : minipot_config->ports[minipot_config->ports_cnt] = (uint16_t)port; 107 0 : minipot_config->ports_cnt++; 108 0 : if (minipot_config->ports_cnt == minipot_config->ports_size) { 109 0 : minipot_config->ports_size *= 2; 110 0 : minipot_config->ports = realloc(minipot_config->ports, 111 : sizeof(*minipot_config->ports) 112 : * minipot_config->ports_size); 113 : } 114 : } 115 : } 116 : } else { 117 0 : error("Setting: ports in file: %s on line: %d has invalid type: %s." 118 : "It must be array.", 119 : config_setting_source_file(ports_st), 120 : config_setting_source_line(ports_st), 121 : config_type_name(config_setting_type(ports_st))); 122 : } 123 : } 124 : }