2016-06-15 03:32:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/property_tree/ini_parser.hpp>
|
|
|
|
#include <boost/property_tree/ptree.hpp>
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/logger.hpp"
|
|
|
|
#include "utils/string.hpp"
|
2016-11-02 19:22:45 +00:00
|
|
|
#include "x11/xresources.hpp"
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
LEMONBUDDY_NS
|
|
|
|
|
2016-10-15 11:15:56 +00:00
|
|
|
#define GET_CONFIG_VALUE(section, var, name) var = m_conf.get<decltype(var)>(section, name, var)
|
|
|
|
#define REQ_CONFIG_VALUE(section, var, name) var = m_conf.get<decltype(var)>(section, name)
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
using ptree = boost::property_tree::ptree;
|
|
|
|
|
|
|
|
DEFINE_ERROR(value_error);
|
|
|
|
DEFINE_ERROR(key_error);
|
|
|
|
|
|
|
|
class config {
|
|
|
|
public:
|
2016-10-19 07:16:08 +00:00
|
|
|
explicit config(const logger& logger, const xresource_manager& xrm)
|
|
|
|
: m_logger(logger), m_xrm(xrm) {}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
void load(string file, string barname);
|
|
|
|
string filepath() const;
|
|
|
|
string bar_section() const;
|
|
|
|
vector<string> defined_bars() const;
|
|
|
|
string build_path(const string& section, const string& key) const;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parameter for the current bar by name
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
T get(string key) const {
|
|
|
|
return get<T>(bar_section(), key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value of a variable by section and parameter name
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
T get(string section, string key) const {
|
|
|
|
auto val = m_ptree.get_optional<T>(build_path(section, key));
|
|
|
|
|
|
|
|
if (val == boost::none)
|
2016-10-11 06:18:25 +00:00
|
|
|
throw key_error("Missing parameter [" + section + "." + key + "]");
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
auto str_val = m_ptree.get<string>(build_path(section, key));
|
|
|
|
|
|
|
|
return dereference_var<T>(section, key, str_val, val.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value of a variable by section and parameter name
|
|
|
|
* with a default value in case the parameter isn't defined
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
T get(string section, string key, T default_value) const {
|
|
|
|
auto val = m_ptree.get_optional<T>(build_path(section, key));
|
|
|
|
auto str_val = m_ptree.get_optional<string>(build_path(section, key));
|
|
|
|
|
|
|
|
return dereference_var<T>(
|
|
|
|
section, key, str_val.get_value_or(""), val.get_value_or(default_value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of values for the current bar by name
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2016-11-12 11:56:39 +00:00
|
|
|
vector<T> get_list(string key) const {
|
2016-06-15 03:32:35 +00:00
|
|
|
return get_list<T>(bar_section(), key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of values by section and parameter name
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
vector<T> get_list(string section, string key) const {
|
|
|
|
vector<T> vec;
|
|
|
|
optional<T> value;
|
|
|
|
|
|
|
|
while ((value = m_ptree.get_optional<T>(
|
|
|
|
build_path(section, key) + "-" + to_string(vec.size()))) != boost::none) {
|
|
|
|
auto str_val = m_ptree.get<string>(build_path(section, key) + "-" + to_string(vec.size()));
|
|
|
|
vec.emplace_back(dereference_var<T>(section, key, str_val, value.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.empty())
|
2016-10-11 06:18:25 +00:00
|
|
|
throw key_error("Missing parameter [" + section + "." + key + "-0]");
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of values by section and parameter name
|
|
|
|
* with a default list in case the list isn't defined
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
vector<T> get_list(string section, string key, vector<T> default_value) const {
|
|
|
|
vector<T> vec;
|
|
|
|
optional<T> value;
|
|
|
|
|
|
|
|
while ((value = m_ptree.get_optional<T>(
|
|
|
|
build_path(section, key) + "-" + to_string(vec.size()))) != boost::none) {
|
|
|
|
auto str_val = m_ptree.get<string>(build_path(section, key) + "-" + to_string(vec.size()));
|
|
|
|
vec.emplace_back(dereference_var<T>(section, key, str_val, value.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.empty())
|
|
|
|
return default_value;
|
|
|
|
else
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Find value of a config parameter defined as a reference
|
2016-10-28 11:36:53 +00:00
|
|
|
* variable using ${section.param}
|
2016-10-19 07:16:08 +00:00
|
|
|
*
|
2016-10-28 11:36:53 +00:00
|
|
|
* ${BAR.key} may be used to reference the current bar section
|
|
|
|
* ${self.key} may be used to reference the current section
|
|
|
|
* ${env:key} may be used to reference an environment variable
|
|
|
|
* ${xrdb:key} may be used to reference a variable in the X resource db
|
2016-06-15 03:32:35 +00:00
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
T dereference_var(string ref_section, string ref_key, string var, const T ref_val) const {
|
|
|
|
auto n = var.find("${");
|
|
|
|
auto m = var.find("}");
|
|
|
|
|
|
|
|
if (n != 0 || m != var.length() - 1)
|
|
|
|
return ref_val;
|
|
|
|
|
|
|
|
auto path = var.substr(2, m - 2);
|
|
|
|
|
|
|
|
if (path.find("env:") == 0) {
|
|
|
|
if (has_env(path.substr(4).c_str()))
|
|
|
|
return boost::lexical_cast<T>(read_env(path.substr(4).c_str()));
|
|
|
|
return ref_val;
|
|
|
|
}
|
|
|
|
|
2016-10-19 07:16:08 +00:00
|
|
|
if (path.find("xrdb:") == 0) {
|
2016-10-19 08:06:29 +00:00
|
|
|
if (std::is_same<string, T>::value)
|
2016-10-19 07:16:08 +00:00
|
|
|
return boost::lexical_cast<T>(m_xrm.get_string(path.substr(5)));
|
2016-10-19 08:06:29 +00:00
|
|
|
else if (std::is_same<float, T>::value)
|
2016-10-19 07:16:08 +00:00
|
|
|
return boost::lexical_cast<T>(m_xrm.get_float(path.substr(5)));
|
2016-10-19 08:06:29 +00:00
|
|
|
else if (std::is_same<int, T>::value)
|
2016-10-19 07:16:08 +00:00
|
|
|
return boost::lexical_cast<T>(m_xrm.get_int(path.substr(5)));
|
2016-10-19 08:06:29 +00:00
|
|
|
return ref_val;
|
2016-10-19 07:16:08 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
auto ref_path = build_path(ref_section, ref_key);
|
|
|
|
|
|
|
|
if ((n = path.find(".")) == string::npos)
|
2016-10-11 06:18:25 +00:00
|
|
|
throw value_error("Invalid reference defined at [" + ref_path + "]");
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-19 07:16:08 +00:00
|
|
|
auto section = path.substr(0, n);
|
|
|
|
|
|
|
|
section = string_util::replace(section, "BAR", bar_section());
|
2016-10-28 11:36:53 +00:00
|
|
|
section = string_util::replace(section, "self", ref_section);
|
2016-10-19 07:16:08 +00:00
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
auto key = path.substr(n + 1, path.length() - n - 1);
|
|
|
|
auto val = m_ptree.get_optional<T>(build_path(section, key));
|
|
|
|
|
|
|
|
if (val == boost::none)
|
2016-10-11 06:18:25 +00:00
|
|
|
throw value_error("Unexisting reference defined at [" + ref_path + "]");
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
auto str_val = m_ptree.get<string>(build_path(section, key));
|
|
|
|
|
|
|
|
return dereference_var<T>(section, key, str_val, val.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const logger& m_logger;
|
2016-10-19 07:16:08 +00:00
|
|
|
const xresource_manager& m_xrm;
|
2016-06-15 03:32:35 +00:00
|
|
|
ptree m_ptree;
|
|
|
|
string m_file;
|
|
|
|
string m_current_bar;
|
|
|
|
};
|
|
|
|
|
2016-10-24 23:55:59 +00:00
|
|
|
namespace {
|
|
|
|
/**
|
|
|
|
* Configure injection module
|
|
|
|
*/
|
|
|
|
template <typename T = const config&>
|
|
|
|
di::injector<T> configure_config() {
|
|
|
|
return di::make_injector(configure_logger(), configure_xresource_manager());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
LEMONBUDDY_NS_END
|