From d45fd76dcdaecbb426becb2d7741131e8a8a2f30 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 13 Dec 2016 18:24:01 +0100 Subject: [PATCH] refactor(config): Drop boost::property_tree --- include/components/config.hpp | 158 ++++++++++++++---------- include/modules/cpu.hpp | 2 - include/modules/memory.hpp | 2 - src/components/bar.cpp | 2 +- src/components/config.cpp | 220 ++++++++++++++++++++++++++-------- src/drawtypes/progressbar.cpp | 4 +- src/modules/cpu.cpp | 3 + src/modules/memory.cpp | 3 + 8 files changed, 277 insertions(+), 117 deletions(-) diff --git a/include/components/config.hpp b/include/components/config.hpp index 0981f8c0..e6bd7d13 100644 --- a/include/components/config.hpp +++ b/include/components/config.hpp @@ -1,9 +1,7 @@ #pragma once -#include #include -#include -#include +#include #include "common.hpp" #include "components/logger.hpp" @@ -14,42 +12,44 @@ POLYBAR_NS +using boost::optional; +using boost::none; + #define GET_CONFIG_VALUE(section, var, name) var = m_conf.get(section, name, var) #define REQ_CONFIG_VALUE(section, var, name) var = m_conf.get(section, name) -using ptree = boost::property_tree::ptree; - DEFINE_ERROR(value_error); DEFINE_ERROR(key_error); class config { public: + using valuemap_t = std::unordered_map; + using sectionmap_t = std::unordered_map; + static constexpr const char* KEY_INHERIT{"inherit"}; explicit config(const logger& logger, const xresource_manager& xrm) : m_logger(logger), m_xrm(xrm) {} void load(string file, string barname); - void copy_inherited(); string filepath() const; string bar_section() const; vector defined_bars() const; - string build_path(const string& section, const string& key) const; void warn_deprecated(const string& section, const string& key, string replacement) const; /** * Returns true if a given parameter exists */ template - bool has(string section, string key) const { - auto val = m_ptree.get_optional(build_path(section, key)); - return (val != boost::none); + bool has(const string& section, const string& key) const { + auto it = m_sections.find(section); + return it != m_sections.end() && it->second.find(key) != it->second.end(); } /** * Get parameter for the current bar by name */ template - T get(string key) const { + T get(const string& key) const { return get(bar_section(), key); } @@ -57,15 +57,20 @@ class config { * Get value of a variable by section and parameter name */ template - T get(string section, string key) const { - auto val = m_ptree.get_optional(build_path(section, key)); + T get(const string& section, const string& key) const { + optional value{opt(section, key)}; - if (val == boost::none) + if (value == none) { throw key_error("Missing parameter [" + section + "." + key + "]"); + } - auto str_val = m_ptree.get(build_path(section, key)); + string string_value{opt(section, key).get()}; - return dereference(section, key, str_val, val.get()); + if (!string_value.empty()) { + return dereference(section, key, opt(section, key).get(), value.get()); + } + + return move(value.get()); } /** @@ -73,18 +78,27 @@ class config { * with a default value in case the parameter isn't defined */ template - T get(string section, string key, T default_value) const { - auto val = m_ptree.get_optional(build_path(section, key)); - auto str_val = m_ptree.get_optional(build_path(section, key)); + T get(const string& section, const string& key, const T& default_value) const { + optional value{opt(section, key)}; - return dereference(section, key, str_val.get_value_or(""), val.get_value_or(default_value)); + if (value == none) { + return default_value; + } + + string string_value{opt(section, key).get()}; + + if (!string_value.empty()) { + return dereference(section, key, opt(section, key).get(), value.get()); + } + + return move(value.get()); } /** * Get list of values for the current bar by name */ template - vector get_list(string key) const { + vector get_list(const string& key) const { return get_list(bar_section(), key); } @@ -92,13 +106,18 @@ class config { * Get list of values by section and parameter name */ template - vector get_list(string section, string key) const { + vector get_list(const string& section, const string& key) const { vector vec; - boost::optional value; + optional value; - while ((value = m_ptree.get_optional(build_path(section, key) + "-" + to_string(vec.size()))) != boost::none) { - auto str_val = m_ptree.get(build_path(section, key) + "-" + to_string(vec.size())); - vec.emplace_back(dereference(section, key, str_val, value.get())); + while ((value = opt(section, key + "-" + to_string(vec.size()))) != none) { + string string_value{get(section, key + "-" + to_string(vec.size()))}; + + if (!string_value.empty()) { + vec.emplace_back(dereference(section, key, move(string_value), move(value.get()))); + } else { + vec.emplace_back(move(value.get())); + } } if (vec.empty()) @@ -112,13 +131,18 @@ class config { * with a default list in case the list isn't defined */ template - vector get_list(string section, string key, vector default_value) const { + vector get_list(const string& section, const string& key, const vector& default_value) const { vector vec; - boost::optional value; + optional value; - while ((value = m_ptree.get_optional(build_path(section, key) + "-" + to_string(vec.size()))) != boost::none) { - auto str_val = m_ptree.get(build_path(section, key) + "-" + to_string(vec.size())); - vec.emplace_back(dereference(section, key, str_val, value.get())); + while ((value = opt(section, key + "-" + to_string(vec.size()))) != none) { + string string_value{get(section, key + "-" + to_string(vec.size()))}; + + if (!string_value.empty()) { + vec.emplace_back(dereference(section, key, move(string_value), move(value.get()))); + } else { + vec.emplace_back(move(value.get())); + } } if (vec.empty()) @@ -128,26 +152,44 @@ class config { } protected: + void read(); + void copy_inherited(); + + template + T convert(string&& value) const; + + template + const optional opt(const string& section, const string& key) const { + sectionmap_t::const_iterator s; + valuemap_t::const_iterator v; + + if ((s = m_sections.find(section)) != m_sections.end() && (v = s->second.find(key)) != s->second.end()) { + return {convert(string{v->second})}; + } + + return none; + } + /** * Dereference value reference */ template - T dereference(string section, string key, string var, const T value) const { + T dereference(const string& section, const string& key, const string& var, T fallback) const { if (var.substr(0, 2) != "${" || var.substr(var.length() - 1) != "}") { - return value; + return fallback; } auto path = var.substr(2, var.length() - 3); size_t pos; - if (path.find("env:") == 0) { - return dereference_env(path.substr(4), value); - } else if (path.find("xrdb:") == 0) { - return dereference_xrdb(path.substr(5), value); + if (path.compare(0, 4, "env:") == 0) { + return dereference_env(path.substr(4), fallback); + } else if (path.compare(0, 5, "xrdb:") == 0) { + return dereference_xrdb(path.substr(5), fallback); } else if ((pos = path.find(".")) != string::npos) { return dereference_local(path.substr(0, pos), path.substr(pos + 1), section); } else { - throw value_error("Invalid reference defined at [" + build_path(section, key) + "]"); + throw value_error("Invalid reference defined at [" + section + "." + key + "]"); } } @@ -158,21 +200,22 @@ class config { * ${section.key} */ template - T dereference_local(string section, string key, string current_section) const { - if (section == "BAR") + T dereference_local(string section, const string& key, const string& current_section) const { + if (section == "BAR") { m_logger.warn("${BAR.key} is deprecated. Use ${root.key} instead"); + } section = string_util::replace(section, "BAR", bar_section(), 0, 3); section = string_util::replace(section, "root", bar_section(), 0, 4); section = string_util::replace(section, "self", current_section, 0, 4); - auto path = build_path(section, key); - auto result = m_ptree.get_optional(path); + optional result{opt(section, key)}; - if (result == boost::none) - throw value_error("Unexisting reference defined [" + path + "]"); + if (result == none) { + throw value_error("Unexisting reference defined [" + section + "." + key + "]"); + } - return dereference(section, key, m_ptree.get(path), result.get()); + return dereference(section, key, opt(section, key).get(), result.get()); } /** @@ -185,14 +228,15 @@ class config { size_t pos; if ((pos = var.find(":")) != string::npos) { - fallback = boost::lexical_cast(var.substr(pos + 1)); + fallback = convert(var.substr(pos + 1)); var.erase(pos); } - if (env_util::has(var.c_str())) - return boost::lexical_cast(env_util::get(var.c_str())); - - return fallback; + if (env_util::has(var.c_str())) { + return convert(env_util::get(var.c_str())); + } else { + return fallback; + } } /** @@ -205,25 +249,19 @@ class config { size_t pos; if ((pos = var.find(":")) != string::npos) { - fallback = boost::lexical_cast(var.substr(pos + 1)); - var.erase(pos); + return convert(m_xrm.get_string(var.substr(0, pos), var.substr(pos + 1))); } - if (std::is_same::value) - return boost::lexical_cast(m_xrm.get_string(var, boost::lexical_cast(fallback))); - else if (std::is_same::value) - return boost::lexical_cast(m_xrm.get_float(var, boost::lexical_cast(fallback))); - else if (std::is_same::value) - return boost::lexical_cast(m_xrm.get_int(var, boost::lexical_cast(fallback))); - return fallback; + string str{m_xrm.get_string(var, "")}; + return str.empty() ? fallback : convert(move(str)); } private: const logger& m_logger; const xresource_manager& m_xrm; - ptree m_ptree; string m_file; string m_current_bar; + sectionmap_t m_sections; }; namespace { diff --git a/include/modules/cpu.hpp b/include/modules/cpu.hpp index 412269c7..d2b157e0 100644 --- a/include/modules/cpu.hpp +++ b/include/modules/cpu.hpp @@ -1,7 +1,5 @@ #pragma once -#include - #include "config.hpp" #include "modules/meta/timer_module.hpp" diff --git a/include/modules/memory.hpp b/include/modules/memory.hpp index 285d8aa3..99a54b28 100644 --- a/include/modules/memory.hpp +++ b/include/modules/memory.hpp @@ -1,7 +1,5 @@ #pragma once -#include - #include "config.hpp" #include "modules/meta/timer_module.hpp" diff --git a/src/components/bar.cpp b/src/components/bar.cpp index fcc04a22..954fc913 100644 --- a/src/components/bar.cpp +++ b/src/components/bar.cpp @@ -276,7 +276,7 @@ void bar::bootstrap_tray() { settings.orig_y = m_opts.pos.y + m_opts.borders.at(edge::TOP).size; // Apply user-defined scaling - auto scale = m_conf.get(bs, "tray-scale", 1.0); + auto scale = m_conf.get(bs, "tray-scale", 1.0f); settings.width *= scale; settings.height_fill *= scale; diff --git a/src/components/config.cpp b/src/components/config.cpp index c002a1ac..3eb39ef9 100644 --- a/src/components/config.cpp +++ b/src/components/config.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include "components/config.hpp" @@ -20,11 +22,8 @@ void config::load(string file, string barname) { throw application_error("Could not find config file: " + file); } - try { - boost::property_tree::read_ini(file, m_ptree); - } catch (const std::exception& e) { - throw application_error(e.what()); - } + // Read values + read(); auto bars = defined_bars(); if (std::find(bars.begin(), bars.end(), m_current_bar) == bars.end()) { @@ -44,43 +43,6 @@ void config::load(string file, string barname) { copy_inherited(); } -/** - * Look for sections set up to inherit from a base section - * and copy the missing parameters - * - * [sub/seciton] - * inherit = base/section - */ -void config::copy_inherited() { - for (auto&& section : m_ptree) { - for (auto&& param : section.second) { - if (param.first.compare(KEY_INHERIT) == 0) { - // Get name of base section - auto inherit = param.second.get_value(); - if ((inherit = dereference(section.first, param.first, inherit, inherit)).empty()) { - throw value_error("[" + section.first + "." + KEY_INHERIT + "] requires a value"); - } - - // Find and validate base section - auto base_section = m_ptree.get_child_optional(inherit); - if (base_section == boost::none) { - throw value_error("[" + section.first + "." + KEY_INHERIT + "] invalid reference \"" + inherit + "\""); - } - - m_logger.trace("config: Copying missing params (sub=\"%s\", base=\"%s\")", section.first, inherit); - - // Iterate the base and copy the parameters - // that hasn't been defined for the sub-section - for (auto&& base_param : *base_section) { - if (!section.second.get_child_optional(base_param.first)) { - section.second.put_child(base_param.first, base_param.second); - } - } - } - } - } -} - /** * Get path of loaded file */ @@ -101,7 +63,7 @@ string config::bar_section() const { vector config::defined_bars() const { vector bars; - for (auto&& p : m_ptree) { + for (auto&& p : m_sections) { if (p.first.compare(0, 4, "bar/") == 0) { bars.emplace_back(p.first.substr(4)); } @@ -110,13 +72,6 @@ vector config::defined_bars() const { return bars; } -/** - * Build path used to find a parameter in the given section - */ -string config::build_path(const string& section, const string& key) const { - return section + "." + key; -} - /** * Print a deprecation warning if the given parameter is set */ @@ -128,4 +83,169 @@ void config::warn_deprecated(const string& section, const string& key, string re } } +void config::read() { + std::ifstream in(m_file.c_str()); + string line; + string section; + + while (std::getline(in, line)) { + if (line.empty()) { + continue; + } + + // New section + if (line[0] == '[' && line[line.length() - 1] == ']') { + section = line.substr(1, line.length() - 2); + continue; + } + + size_t equal_pos; + + // Check for key-value pair equal sign + if ((equal_pos = line.find('=')) == string::npos) { + continue; + } + + string key{string_util::trim(line.substr(0, equal_pos), ' ')}; + string value{string_util::trim(string_util::trim(line.substr(equal_pos + 1), ' '), '"')}; + + m_sections[section][key] = move(value); + } +} + +/** + * Look for sections set up to inherit from a base section + * and copy the missing parameters + * + * [sub/seciton] + * inherit = base/section + */ +void config::copy_inherited() { + for (auto&& section : m_sections) { + for (auto&& param : section.second) { + if (param.first.compare(KEY_INHERIT) == 0) { + // Get name of base section + auto inherit = param.second; + if ((inherit = dereference(section.first, param.first, inherit, inherit)).empty()) { + throw value_error("[" + section.first + "." + KEY_INHERIT + "] requires a value"); + } + + // Find and validate base section + auto base_section = m_sections.find(inherit); + if (base_section == m_sections.end()) { + throw value_error("[" + section.first + "." + KEY_INHERIT + "] invalid reference \"" + inherit + "\""); + } + + m_logger.trace("config: Copying missing params (sub=\"%s\", base=\"%s\")", section.first, inherit); + + // Iterate the base and copy the parameters + // that hasn't been defined for the sub-section + for (auto&& base_param : base_section->second) { + valuemap_t::const_iterator iter; + + if ((iter = section.second.find(base_param.first)) == section.second.end()) { + section.second.emplace_hint(iter, base_param.first, base_param.second); + } + } + } + } + } +} + +template <> +string config::convert(string&& value) const { + return forward(value); +} + +template <> +const char* config::convert(string&& value) const { + return value.c_str(); +} + +template <> +char config::convert(string&& value) const { + return value.c_str()[0]; +} + +template <> +int config::convert(string&& value) const { + return std::atoi(value.c_str()); +} + +template <> +short config::convert(string&& value) const { + return static_cast(std::atoi(value.c_str())); +} + +template <> +bool config::convert(string&& value) const { + return std::atoi(value.c_str()) != 0; +} + +template <> +float config::convert(string&& value) const { + return std::atof(value.c_str()); +} + +template <> +double config::convert(string&& value) const { + return std::atof(value.c_str()); +} + +template <> +long config::convert(string&& value) const { + return std::atol(value.c_str()); +} + +template <> +long long config::convert(string&& value) const { + return std::atoll(value.c_str()); +} + +template <> +unsigned char config::convert(string&& value) const { + int conv{convert(forward(value))}; + if (conv < std::numeric_limits::min()) { + return std::numeric_limits::min(); + } else if (conv > std::numeric_limits::max()) { + return std::numeric_limits::max(); + } else { + return static_cast(conv); + } +} + +template <> +unsigned short config::convert(string&& value) const { + int conv{convert(forward(value))}; + if (conv < std::numeric_limits::min()) { + return std::numeric_limits::min(); + } else if (conv > std::numeric_limits::max()) { + return std::numeric_limits::max(); + } else { + return static_cast(conv); + } +} + +template <> +unsigned int config::convert(string&& value) const { + long conv{convert(forward(value))}; + if (conv < std::numeric_limits::min()) { + return std::numeric_limits::min(); + } else if (conv > std::numeric_limits::max()) { + return std::numeric_limits::max(); + } else { + return static_cast(conv); + } +} + +template <> +unsigned long config::convert(string&& value) const { + return std::strtoul(value.c_str(), nullptr, 10); +} + +template <> +unsigned long long config::convert(string&& value) const { + return std::strtoull(value.c_str(), nullptr, 10); +} + POLYBAR_NS_END diff --git a/src/drawtypes/progressbar.cpp b/src/drawtypes/progressbar.cpp index 37814851..c6a7e047 100644 --- a/src/drawtypes/progressbar.cpp +++ b/src/drawtypes/progressbar.cpp @@ -94,10 +94,10 @@ namespace drawtypes { unsigned int width; if ((format = conf.get(section, name + "-format", format)).empty()) { - throw application_error("Invalid format defined at [" + conf.build_path(section, name) + "]"); + throw application_error("Invalid format defined at [" + section + "." + name + "]"); } if ((width = conf.get(section, name + "-width")) < 1) { - throw application_error("Invalid width defined at [" + conf.build_path(section, name) + "]"); + throw application_error("Invalid width defined at [" + section + "." + name + "]"); } progressbar_t progressbar{new progressbar_t::element_type(bar, width, format)}; diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index 4a363f19..4d293d8d 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "modules/cpu.hpp" #include "drawtypes/label.hpp" diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index ad3d98f1..b36238be 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "modules/memory.hpp" #include "drawtypes/label.hpp"