config_parser: Pass barname as argument to parse() (#2765)

Doesn't need to be a class field
This commit is contained in:
Patrick Ziegler 2022-07-09 13:12:37 +02:00 committed by GitHub
parent 841b799299
commit f4d0ba9186
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 22 deletions

View File

@ -90,12 +90,12 @@ struct line_t {
class config_parser { class config_parser {
public: public:
config_parser(const logger& logger, string&& file, string&& bar); config_parser(const logger& logger, string&& file);
/** /**
* This prevents passing a temporary logger to the constructor because that would be UB, as the temporary would be * This prevents passing a temporary logger to the constructor because that would be UB, as the temporary would be
* destroyed once the constructor returns. * destroyed once the constructor returns.
*/ */
config_parser(logger&& logger, string&& file, string&& bar) = delete; config_parser(logger&& logger, string&& file) = delete;
/** /**
* @brief Performs the parsing of the main config file m_file * @brief Performs the parsing of the main config file m_file
@ -105,7 +105,7 @@ class config_parser {
* @throws syntax_error If there was any kind of syntax error * @throws syntax_error If there was any kind of syntax error
* @throws parser_error If aynthing else went wrong * @throws parser_error If aynthing else went wrong
*/ */
config::make_type parse(); config::make_type parse(string barname);
protected: protected:
/** /**
@ -231,11 +231,6 @@ class config_parser {
*/ */
string m_config; string m_config;
/**
* Is used to resolve ${root...} references
*/
string m_barname;
/** /**
* @brief List of all the lines in the config (with included files) * @brief List of all the lines in the config (with included files)
* *

View File

@ -10,10 +10,10 @@
POLYBAR_NS POLYBAR_NS
config_parser::config_parser(const logger& logger, string&& file, string&& bar) config_parser::config_parser(const logger& logger, string&& file)
: m_log(logger), m_config(file_util::expand(file)), m_barname(move(bar)) {} : m_log(logger), m_config(file_util::expand(file)) {}
config::make_type config_parser::parse() { config::make_type config_parser::parse(string barname) {
m_log.notice("Parsing config file: %s", m_config); m_log.notice("Parsing config file: %s", m_config);
parse_file(m_config, {}); parse_file(m_config, {});
@ -21,21 +21,21 @@ config::make_type config_parser::parse() {
sectionmap_t sections = create_sectionmap(); sectionmap_t sections = create_sectionmap();
vector<string> bars = get_bars(sections); vector<string> bars = get_bars(sections);
if (m_barname.empty()) { if (barname.empty()) {
if (bars.size() == 1) { if (bars.size() == 1) {
m_barname = bars[0]; barname = bars[0];
} else if (bars.empty()) { } else if (bars.empty()) {
throw application_error("The config file contains no bar."); throw application_error("The config file contains no bar.");
} else { } else {
throw application_error("The config file contains multiple bars, but no bar name was given. Available bars: " + throw application_error("The config file contains multiple bars, but no bar name was given. Available bars: " +
string_util::join(bars, ", ")); string_util::join(bars, ", "));
} }
} else if (sections.find("bar/" + m_barname) == sections.end()) { } else if (sections.find("bar/" + barname) == sections.end()) {
if (bars.empty()) { if (bars.empty()) {
throw application_error("Undefined bar: " + m_barname + ". The config file contains no bar."); throw application_error("Undefined bar: " + barname + ". The config file contains no bar.");
} else { } else {
throw application_error( throw application_error(
"Undefined bar: " + m_barname + ". Available bars: " + string_util::join(get_bars(sections), ", ")); "Undefined bar: " + barname + ". Available bars: " + string_util::join(get_bars(sections), ", "));
} }
} }
@ -45,7 +45,7 @@ config::make_type config_parser::parse() {
* second element onwards for the included list * second element onwards for the included list
*/ */
file_list included(m_files.begin() + 1, m_files.end()); file_list included(m_files.begin() + 1, m_files.end());
config::make_type result = config::make(m_config, m_barname); config::make_type result = config::make(m_config, barname);
// Cast to non-const to set sections, included and xrm // Cast to non-const to set sections, included and xrm
config& m_conf = const_cast<config&>(result); config& m_conf = const_cast<config&>(result);

View File

@ -127,8 +127,8 @@ int main(int argc, char** argv) {
barname = cli->get(0); barname = cli->get(0);
} }
config_parser parser{logger, move(confpath), move(barname)}; config_parser parser{logger, move(confpath)};
config::make_type conf = parser.parse(); config::make_type conf = parser.parse(move(barname));
//================================================== //==================================================
// Dump requested data // Dump requested data

View File

@ -13,8 +13,8 @@ class TestableConfigParser : public config_parser {
using config_parser::config_parser; using config_parser::config_parser;
public: public:
TestableConfigParser(const logger& logger, string&& file, string&& bar) TestableConfigParser(const logger& logger, string&& file)
: config_parser(logger, move(file), move(bar)) { : config_parser(logger, move(file)) {
m_files.push_back("test_config"); m_files.push_back("test_config");
} }
@ -43,7 +43,7 @@ class TestableConfigParser : public config_parser {
class ConfigParser : public ::testing::Test { class ConfigParser : public ::testing::Test {
protected: protected:
const logger l = logger(loglevel::NONE); const logger l = logger(loglevel::NONE);
unique_ptr<TestableConfigParser> parser = make_unique<TestableConfigParser>(l, "/dev/zero", "TEST"); unique_ptr<TestableConfigParser> parser = make_unique<TestableConfigParser>(l, "/dev/zero");
}; };
// ParseLineTest {{{ // ParseLineTest {{{