diff options
author | louiz’ <louiz@louiz.org> | 2017-04-21 11:19:36 +0200 |
---|---|---|
committer | louiz’ <louiz@louiz.org> | 2017-04-21 11:19:36 +0200 |
commit | eac144acdaca02f018bddde5f623fba3e8cd4ad9 (patch) | |
tree | 7a64b0a690e0a007115a0005eecbac0d00d1a13d /src/config | |
parent | cf87cf089251eddf2c33322e07b0cde9f70ec24b (diff) | |
download | biboumi-eac144acdaca02f018bddde5f623fba3e8cd4ad9.tar.gz biboumi-eac144acdaca02f018bddde5f623fba3e8cd4ad9.tar.bz2 biboumi-eac144acdaca02f018bddde5f623fba3e8cd4ad9.tar.xz biboumi-eac144acdaca02f018bddde5f623fba3e8cd4ad9.zip |
Configuration options can be overridden by setting env values
Diffstat (limited to 'src/config')
-rw-r--r-- | src/config/config.cpp | 46 |
1 files changed, 35 insertions, 11 deletions
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 <config/config.hpp> +#include <utils/tolower.hpp> #include <iostream> #include <cstring> #include <cstdlib> +using namespace std::string_literals; + +extern char** environ; + std::string Config::filename{}; std::map<std::string, std::string> Config::values{}; std::vector<t_config_changed_callback> 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; } |