Lines Matching defs:config
21 #include "osi/include/config.h"
55 static bool config_parse(FILE* fp, config_t* config);
59 static section_t* section_find(const config_t* config, const char* section);
63 static entry_t* entry_find(const config_t* config, const char* section,
67 config_t* config = static_cast<config_t*>(osi_calloc(sizeof(config_t)));
69 config->sections = list_new(section_free);
70 if (!config->sections) {
75 return config;
78 config_free(config);
85 config_t* config = config_new_empty();
86 if (!config) return NULL;
92 config_free(config);
96 if (!config_parse(fp, config)) {
97 config_free(config);
98 config = NULL;
102 return config;
128 void config_free(config_t* config) {
129 if (!config) return;
131 list_free(config->sections);
132 osi_free(config);
135 bool config_has_section(const config_t* config, const char* section) {
136 CHECK(config != NULL);
139 return (section_find(config, section) != NULL);
142 bool config_has_key(const config_t* config, const char* section,
144 CHECK(config != NULL);
148 return (entry_find(config, section, key) != NULL);
151 int config_get_int(const config_t* config, const char* section, const char* key,
153 CHECK(config != NULL);
157 entry_t* entry = entry_find(config, section, key);
165 bool config_get_bool(const config_t* config, const char* section,
167 CHECK(config != NULL);
171 entry_t* entry = entry_find(config, section, key);
180 const char* config_get_string(const config_t* config, const char* section,
182 CHECK(config != NULL);
186 entry_t* entry = entry_find(config, section, key);
192 void config_set_int(config_t* config, const char* section, const char* key,
194 CHECK(config != NULL);
200 config_set_string(config, section, key, value_str);
203 void config_set_bool(config_t* config, const char* section, const char* key,
205 CHECK(config != NULL);
209 config_set_string(config, section, key, value ? "true" : "false");
212 void config_set_string(config_t* config, const char* section, const char* key,
214 section_t* sec = section_find(config, section);
217 list_append(config->sections, sec);
234 bool config_remove_section(config_t* config, const char* section) {
235 CHECK(config != NULL);
238 section_t* sec = section_find(config, section);
241 return list_remove(config->sections, sec);
244 bool config_remove_key(config_t* config, const char* section, const char* key) {
245 CHECK(config != NULL);
249 section_t* sec = section_find(config, section);
250 entry_t* entry = entry_find(config, section, key);
256 const config_section_node_t* config_section_begin(const config_t* config) {
257 CHECK(config != NULL);
258 return (const config_section_node_t*)list_begin(config->sections);
261 const config_section_node_t* config_section_end(const config_t* config) {
262 CHECK(config != NULL);
263 return (const config_section_node_t*)list_end(config->sections);
279 bool config_save(const config_t* config, const char* filename) {
280 CHECK(config != NULL);
284 // Steps to ensure content of config file gets to disk:
288 // 3) Rename temp file to actual config file (e.g. bt_config.conf).
295 // Build temp config file based on config file (e.g. bt_config.conf.new).
325 for (const list_node_t* node = list_begin(config->sections);
326 node != list_end(config->sections); node = list_next(node)) {
345 if (list_next(node) != list_end(config->sections)) {
375 // Rename written temp file to the actual config file.
421 static bool config_parse(FILE* fp, config_t* config) {
423 CHECK(config != NULL);
455 config_set_string(config, section, trim(line_ptr), trim(split + 1));
478 static section_t* section_find(const config_t* config, const char* section) {
479 for (const list_node_t* node = list_begin(config->sections);
480 node != list_end(config->sections); node = list_next(node)) {
505 static entry_t* entry_find(const config_t* config, const char* section,
507 section_t* sec = section_find(config, section);