From 0ab40dc1ab4e689921da54080b135e1d22b1c586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 14 Mar 2017 21:45:23 +0100 Subject: Refactoring louloulibs and cmake Use OBJECT libraries Remove the louloulibs directory Write FOUND variables in the cache --- src/config/config.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/config/config.cpp (limited to 'src/config/config.cpp') diff --git a/src/config/config.cpp b/src/config/config.cpp new file mode 100644 index 0000000..24a1c87 --- /dev/null +++ b/src/config/config.cpp @@ -0,0 +1,103 @@ +#include + +#include +#include + +#include + +std::string Config::filename{}; +std::map Config::values{}; +std::vector Config::callbacks{}; + +std::string Config::get(const std::string& option, const std::string& def) +{ + auto it = Config::values.find(option); + + if (it == Config::values.end()) + return def; + return it->second; +} + +int Config::get_int(const std::string& option, const int& def) +{ + std::string res = Config::get(option, ""); + if (!res.empty()) + return std::atoi(res.c_str()); + else + return def; +} + +void Config::set(const std::string& option, const std::string& value, bool save) +{ + Config::values[option] = value; + if (save) + { + Config::save_to_file(); + Config::trigger_configuration_change(); + } +} + +void Config::connect(t_config_changed_callback callback) +{ + Config::callbacks.push_back(callback); +} + +void Config::clear() +{ + Config::values.clear(); +} + +/** + * Private methods + */ +void Config::trigger_configuration_change() +{ + std::vector::iterator it; + for (it = Config::callbacks.begin(); it < Config::callbacks.end(); ++it) + (*it)(); +} + +bool Config::read_conf(const std::string& name) +{ + if (!name.empty()) + Config::filename = name; + + std::ifstream file(Config::filename.data()); + if (!file.is_open()) + { + std::cerr << "Error while opening file " << filename << " for reading: " << strerror(errno) << std::endl; + return false; + } + + Config::clear(); + + std::string line; + size_t pos; + std::string option; + std::string value; + while (file.good()) + { + std::getline(file, line); + if (line == "" || line[0] == '#') + continue ; + pos = line.find('='); + if (pos == std::string::npos) + continue ; + option = line.substr(0, pos); + value = line.substr(pos+1); + Config::values[option] = value; + } + return true; +} + +void Config::save_to_file() +{ + std::ofstream file(Config::filename.data()); + if (file.fail()) + { + std::cerr << "Could not save config file." << std::endl; + return ; + } + for (const auto& it: Config::values) + file << it.first << "=" << it.second << '\n'; +} -- cgit v1.2.3 From 1a09c965eb3723cdaab9ea556f30ffbc7f09a6dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 29 Mar 2017 23:32:43 +0200 Subject: Remove two sneaky log_debug --- src/config/config.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/config/config.cpp') diff --git a/src/config/config.cpp b/src/config/config.cpp index 24a1c87..0db5751 100644 --- a/src/config/config.cpp +++ b/src/config/config.cpp @@ -37,7 +37,7 @@ void Config::set(const std::string& option, const std::string& value, bool save) } } -void Config::connect(t_config_changed_callback callback) +void Config::connect(const t_config_changed_callback& callback) { Config::callbacks.push_back(callback); } -- cgit v1.2.3 From eac144acdaca02f018bddde5f623fba3e8cd4ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 21 Apr 2017 11:19:36 +0200 Subject: Configuration options can be overridden by setting env values --- src/config/config.cpp | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) (limited to 'src/config/config.cpp') diff --git a/src/config/config.cpp b/src/config/config.cpp index 0db5751..0f3d639 100644 --- a/src/config/config.cpp +++ b/src/config/config.cpp @@ -1,10 +1,15 @@ #include +#include #include #include #include +using namespace std::string_literals; + +extern char** environ; + std::string Config::filename{}; std::map Config::values{}; std::vector Config::callbacks{}; @@ -71,21 +76,40 @@ bool Config::read_conf(const std::string& name) Config::clear(); + auto parse_line = [](const std::string& line, const bool env) + { + static const auto env_option_prefix = "BIBOUMI_"s; + + if (line == "" || line[0] == '#') + return; + size_t pos = line.find('='); + if (pos == std::string::npos) + return; + std::string option = line.substr(0, pos); + std::string value = line.substr(pos+1); + if (env) + { + auto a = option.substr(0, env_option_prefix.size()); + if (a == env_option_prefix) + option = utils::tolower(option.substr(env_option_prefix.size())); + else + return; + } + Config::values[option] = value; + }; + std::string line; - size_t pos; - std::string option; - std::string value; while (file.good()) { std::getline(file, line); - if (line == "" || line[0] == '#') - continue ; - pos = line.find('='); - if (pos == std::string::npos) - continue ; - option = line.substr(0, pos); - value = line.substr(pos+1); - Config::values[option] = value; + parse_line(line, false); + } + + char** env_line = environ; + while (*env_line) + { + parse_line(*env_line, true); + env_line++; } return true; } -- cgit v1.2.3