From: Mika Laitio Date: Fri, 15 Apr 2011 16:37:35 +0000 (+0300) Subject: Updates to set_config_value parameter X-Git-Url: http://pilppa.org/gitweb/?p=libplp.git;a=commitdiff_plain;h=23a30eb765fc6aaf347da839567140ae41e8cc6b Updates to set_config_value parameter - add boolean parameter for set_configuration_value to specify whether the single config parameter change is is wanted to save immediately (usefull when changing multiple different values) - rename set_cfg_value to set_config_value - renamed get_cfg_value to get_config_value Signed-off-by: Mika Laitio --- diff --git a/src/DataReader.cc b/src/DataReader.cc index 72a44bd..0e6ebf3 100644 --- a/src/DataReader.cc +++ b/src/DataReader.cc @@ -448,7 +448,7 @@ string DataReader::get_device_type() { if (device_config == NULL) { device_config = DeviceConfig::get_device_config(device_id); - ret_val = device_config->get_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE); + ret_val = device_config->get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE); } return ret_val; } diff --git a/src/DeviceConfig.cc b/src/DeviceConfig.cc index 904257e..b663259 100644 --- a/src/DeviceConfig.cc +++ b/src/DeviceConfig.cc @@ -7,6 +7,7 @@ #include "DeviceConfig.hh" +#include #include #include @@ -34,7 +35,7 @@ DeviceConfig::DeviceConfig(string device_id_param) { device_id = device_id_param; uci_handle = load_device_config(device_id_param); if (uci_handle != NULL) { - device_type = get_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE); + device_type = get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE); } else { log_error("Could not read device configuration.\n"); @@ -85,7 +86,7 @@ string DeviceConfig::get_file_name(string device_id_param) { return ret_val; } -string DeviceConfig::get_cfg_value(string key) { +string DeviceConfig::get_config_value(string key) { struct uci_section *section; struct uci_option *option; string ret_val; @@ -113,18 +114,21 @@ string DeviceConfig::get_cfg_value(string key) { return ret_val; } -void DeviceConfig::set_cfg_value(string key, string value) { +void DeviceConfig::set_config_value(string key, + string value, + bool save_immediately) { string cfg_dir; string cfg_fl; cfg_dir = get_dir_name(device_id); cfg_fl = DEVICE_CONFIG__FILE_NAME; - set_config_value(cfg_dir.c_str(), + set_config_value_to_section(cfg_dir.c_str(), cfg_fl.c_str(), DEVICE_CONFIG__SECTION_TYPE, DEVICE_CONFIG__SECTION_NAME, key.c_str(), - value.c_str()); + value.c_str(), + save_immediately); } EnumSummaryCalculationType DeviceConfig::get_summary_calculation_type() { @@ -166,13 +170,13 @@ ConfigHandle *DeviceConfig::load_device_config(string device_id_param) { } else { log_debug("Failed to load device configuration: %s, err code: %d.\n", cfg_fl.c_str(), UCI_OK); - set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, ""); + set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "", true); uci_free_context(ctx); } } else { log_error("Failed to load device device configuration, memory allocation error.\n"); - set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, ""); + set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "", true); } } else { diff --git a/src/DeviceConfig.hh b/src/DeviceConfig.hh index 194b8a7..bc82416 100644 --- a/src/DeviceConfig.hh +++ b/src/DeviceConfig.hh @@ -13,6 +13,7 @@ extern "C" { #include #include "config.h" + #include } enum EnumSummaryPeriod{PERIOD_YEARLY, PERIOD_MONTHLY, PERIOD_DAILY, PERIOD_HOURLY, PERIOD_MINUTELY, PERIOD_SECONDLY}; @@ -21,16 +22,16 @@ enum EnumSummaryCalculationType {SUM, DELTA, MIN, MAX, MEAN}; const std::string SUMMARY_PERIOD_NAMES_ARRAY[] = {"yearly", "monthly", "daily", "minutely", "secondly"}; const std::string CALCULATION_TYPE_NAMES_ARRAY[] = {"sum", "delta", "min", "max", "mean"}; -#define DEVICE_CONFIG__FILE_NAME "device_cfg" -#define DEVICE_CONFIG__SECTION_TYPE "device" -#define DEVICE_CONFIG__SECTION_NAME "base_data" -#define DEVICE_CONFIG_VALUE_KEY__TYPE "type" -#define DEVICE_CONFIG_VALUE_KEY__ID "id" -#define DEVICE_CONFIG_VALUE_KEY__NAME "name" +#define DEVICE_CONFIG__FILE_NAME "device_cfg" +#define DEVICE_CONFIG__SECTION_TYPE "device" +#define DEVICE_CONFIG__SECTION_NAME "base_data" +#define DEVICE_CONFIG_VALUE_KEY__TYPE "type" +#define DEVICE_CONFIG_VALUE_KEY__ID "id" +#define DEVICE_CONFIG_VALUE_KEY__NAME "name" -#define DEFAULT_STORAGE_BASE_DIR "/tmp/w1data" -#define DATAFILE_SUFFIX ".txt" -#define CACHE_DIR_NAME "cache" +#define DEFAULT_STORAGE_BASE_DIR "/tmp/w1data" +#define DATAFILE_SUFFIX ".txt" +#define CACHE_DIR_NAME "cache" namespace plp { struct ConfigHandle { @@ -48,8 +49,10 @@ namespace plp { static std::string get_base_dir_name(); static void set_base_dir_name(std::string store_param); static DeviceConfig *get_device_config(std::string device_id); - std::string get_cfg_value(std::string key); - void set_cfg_value(std::string key, std::string value); + std::string get_config_value(std::string key); + void set_config_value(std::string key, + std::string value, + bool save_immediately); EnumSummaryCalculationType get_summary_calculation_type(); private: static std::string store_base_dir; diff --git a/src/config.c b/src/config.c index 4e51b43..92c8df3 100644 --- a/src/config.c +++ b/src/config.c @@ -3,16 +3,21 @@ #include #include #include +#include #include "log.h" #include "config.h" -static int uci_create_named_section(struct uci_context *ctx, const char *conf_file_name, const char *section_type, const char *section_name) +static int uci_create_named_section(struct uci_context *ctx, + const char *conf_file_name, + const char *section_type, + const char *section_name, + bool save_immediately) { - struct uci_ptr ptr; - int ret_val; - char *cmd_data; - int len; + struct uci_ptr ptr; + int ret_val; + char *cmd_data; + int len; ret_val = -1; if ((ctx != NULL) && @@ -31,7 +36,9 @@ static int uci_create_named_section(struct uci_context *ctx, const char *conf_fi if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) { ret_val = uci_set(ctx, &ptr); if (ret_val == UCI_OK) { - //ret_val = uci_save(ctx, ptr.p); + if (save_immediately) { + ret_val = uci_save(ctx, ptr.p); + } } } free(cmd_data); @@ -40,12 +47,13 @@ static int uci_create_named_section(struct uci_context *ctx, const char *conf_fi return ret_val; } -bool set_config_value(const char *conf_dir_name, +bool set_config_value_to_section(const char *conf_dir_name, const char *conf_file_name, const char *section_type, const char *section_name, const char *key, - const char *value) { + const char *value, + bool save_immediately) { struct uci_context *ctx; struct uci_package *pkg; struct uci_section *sct; @@ -91,10 +99,14 @@ bool set_config_value(const char *conf_dir_name, } } if (sct == NULL) { - log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname); + log_debug("Creating new section %s to configuration file: %s\n", section_name, fname); //err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct); //err_flg = uci_add_section(ctx, pkg, section_name, &sct); - err_flg = uci_create_named_section(ctx, conf_file_name, section_type, section_name); + err_flg = uci_create_named_section(ctx, + conf_file_name, + section_type, + section_name, + false); if (err_flg == UCI_OK) { uci_foreach_element(&pkg->sections, elem) { tmp_sct = uci_to_section(elem); @@ -105,40 +117,52 @@ bool set_config_value(const char *conf_dir_name, } } } - if (err_flg == 0) { + if (err_flg == UCI_OK) { memset(&ptr, 0, sizeof(ptr)); ptr.package = pkg->e.name; ptr.section = sct->e.name; ptr.option = key; - - if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) { + err_flg = uci_lookup_ptr(ctx, &ptr, NULL, false); + if (err_flg == UCI_OK) { ptr.value = value; - uci_set(ctx, &ptr); - uci_save(ctx, pkg); - ret_val = true; - log_debug("Created configuration section %s to configuration file: %s\n", section_name, fname); + err_flg = uci_set(ctx, &ptr); + if (err_flg == UCI_OK) { + if (save_immediately == true) { + err_flg = uci_save(ctx, pkg); + if (err_flg != UCI_OK) { + log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", fname); + } + } + if (err_flg == UCI_OK) { + ret_val = true; + log_debug("Set value to section %s in configuration file: %s\n", section_name, fname); + } + } + else { + log_error("Failed to set value to configuration file: %s\n. Could not set new value\n", fname); + } } else { - log_error("Could not write to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n", fname, pkg->e.name, sct->e.name); + log_error("Failed to set value to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n", fname, pkg->e.name, sct->e.name); } uci_free_context(ctx); } else { - log_error("Could not write to configuration file: %s. Could not create section %s.\n", fname, section_name); + log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", fname, section_name); } } else { - log_error("Could not write to configuration file: %s. File does not exist or is not writable\n.", fname); + log_error("Failed to set value to configuration file: %s. File does not exist or is not writable\n.", fname); } } free(fname); } else { - log_error("Could not change config value, out of memory\n"); + log_error("Failed to set value to configuration file: %s, out of memory\n", fname); } } else { - log_error("Could not change config value, invalid parameters\n"); + log_error("Failed to set value to configuration file, invalid parameters\n"); } return ret_val; } diff --git a/src/config.h b/src/config.h index 4b1fc3f..07a6429 100644 --- a/src/config.h +++ b/src/config.h @@ -10,11 +10,12 @@ #include -bool set_config_value(const char *conf_dir_name, +bool set_config_value_to_section(const char *conf_dir_name, const char *conf_file_name, const char *section_type, const char *section_name, const char *key, - const char *value); + const char *value, + bool save_immediately); #endif /* CONFIG_H_ */ diff --git a/src_test/test_config.c b/src_test/test_config.c index 24716f3..ac1b2a0 100644 --- a/src_test/test_config.c +++ b/src_test/test_config.c @@ -14,18 +14,20 @@ void test_config() { getcwd(work_dir, sizeof(work_dir)); printf("working directory: %s\n", work_dir); - set_config_value(work_dir, - "dev_cfg.txt", + set_config_value_to_section(work_dir, + "dev_cfg_txt", "mysection_type", "mysection_name", "myoption_name", - "my_option_value"); - set_config_value(work_dir, - "dev_cfg.txt", + "my_option_value", + false); + set_config_value_to_section(work_dir, + "dev_cfg_txt", "mysection_type", "mysection_name", "myoption_name", - "my_option_value2"); + "my_option_value3", + true); } int main(int argc, char** argv) {