From e5f9c7e62246d525aaee41c5f777ac411b85896f Mon Sep 17 00:00:00 2001 From: vinhig Date: Tue, 11 Oct 2022 19:46:36 +0200 Subject: [PATCH 1/4] Fixing config manager that doesn't error out on bad keyword. --- src/config/ConfigManager.cpp | 41 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index d87f8eb..16188b1 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -2,14 +2,15 @@ #include "../Hyprpaper.hpp" CConfigManager::CConfigManager() { - // init the entire thing + // Initialize the configuration + // Read file from default location + // or from an explicit location given by user std::string configPath; if (g_pHyprpaper->m_szExplicitConfigPath.empty()) { - const char *const ENVHOME = getenv("HOME"); - configPath = ENVHOME + (std::string) "/.config/hypr/hyprpaper.conf"; - } - else { + const char* const ENVHOME = getenv("HOME"); + configPath = ENVHOME + std::string("/.config/hypr/hyprpaper.conf"); + } else { configPath = g_pHyprpaper->m_szExplicitConfigPath; } @@ -17,7 +18,11 @@ CConfigManager::CConfigManager() { ifs.open(configPath); if (!ifs.good()) { - Debug::log(CRIT, "Hyprpaper was not provided a config!"); + if (g_pHyprpaper->m_szExplicitConfigPath.empty()) { + Debug::log(CRIT, "No config file provided. Default config file `/.config/hypr/hyprpaper.conf` couldn't be opened."); + } else { + Debug::log(CRIT, "No config file provided. Specified file `%s` couldn't be opened.", configPath); + } exit(1); } @@ -25,18 +30,15 @@ CConfigManager::CConfigManager() { int linenum = 1; if (ifs.is_open()) { while (std::getline(ifs, line)) { - // Read line by line. - try { - parseLine(line); - } catch (...) { - Debug::log(ERR, "Error reading line from config. Line:"); - Debug::log(NONE, "%s", line.c_str()); + // Read line by line + // The cause of the error is + // written in this->parseError + parseLine(line); - parseError += "Config error at line " + std::to_string(linenum) + ": Line parsing error."; - } - - if (!parseError.empty() && parseError.find("Config error at line") != 0) { + if (!parseError.empty()) { + // If an error there is, user may want the precise line where it occured parseError = "Config error at line " + std::to_string(linenum) + ": " + parseError; + break; } ++linenum; @@ -65,16 +67,16 @@ std::string CConfigManager::removeBeginEndSpacesTabs(std::string str) { } void CConfigManager::parseLine(std::string& line) { - // first check if its not a comment + // First check if it's not a comment const auto COMMENTSTART = line.find_first_of('#'); if (COMMENTSTART == 0) return; - // now, cut the comment off + // Remove comment from string if (COMMENTSTART != std::string::npos) line = line.substr(0, COMMENTSTART); - // remove shit at the beginning + // Strip line while (line[0] == ' ' || line[0] == '\t') { line = line.substr(1); } @@ -88,7 +90,6 @@ void CConfigManager::parseLine(std::string& line) { const auto COMMAND = removeBeginEndSpacesTabs(line.substr(0, EQUALSPLACE)); const auto VALUE = removeBeginEndSpacesTabs(line.substr(EQUALSPLACE + 1)); - // parseKeyword(COMMAND, VALUE); } From 93e27e5a28ece9861aaa8143b03be2d8f9ae7ed2 Mon Sep 17 00:00:00 2001 From: Vincent Higginson Date: Wed, 12 Oct 2022 15:24:29 +0200 Subject: [PATCH 2/4] Revert some changes: try/catch block, and comments --- src/config/ConfigManager.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 16188b1..2f62c46 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -31,9 +31,14 @@ CConfigManager::CConfigManager() { if (ifs.is_open()) { while (std::getline(ifs, line)) { // Read line by line - // The cause of the error is - // written in this->parseError - parseLine(line); + try { + parseLine(line); + } catch (...) { + Debug::log(ERR, "Error reading line from config. Line:"); + Debug::log(NONE, "%s", line.c_str()); + + parseError += "Config error at line " + std::to_string(linenum) + ": Line parsing error."; + } if (!parseError.empty()) { // If an error there is, user may want the precise line where it occured @@ -67,12 +72,12 @@ std::string CConfigManager::removeBeginEndSpacesTabs(std::string str) { } void CConfigManager::parseLine(std::string& line) { - // First check if it's not a comment + // first check if its not a comment const auto COMMENTSTART = line.find_first_of('#'); if (COMMENTSTART == 0) return; - // Remove comment from string + // now, cut the comment off if (COMMENTSTART != std::string::npos) line = line.substr(0, COMMENTSTART); From a9e896118f42b9639a6a16e1fd023c1cc9e37c1a Mon Sep 17 00:00:00 2001 From: Vincent Higginson Date: Wed, 12 Oct 2022 17:11:57 +0200 Subject: [PATCH 3/4] Oopsie --- src/config/ConfigManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 2f62c46..9e51c6e 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -19,7 +19,7 @@ CConfigManager::CConfigManager() { if (!ifs.good()) { if (g_pHyprpaper->m_szExplicitConfigPath.empty()) { - Debug::log(CRIT, "No config file provided. Default config file `/.config/hypr/hyprpaper.conf` couldn't be opened."); + Debug::log(CRIT, "No config file provided. Default config file `~/.config/hypr/hyprpaper.conf` couldn't be opened."); } else { Debug::log(CRIT, "No config file provided. Specified file `%s` couldn't be opened.", configPath); } From 099ca5399a342dd5b700ac45592382d6f2b10ae0 Mon Sep 17 00:00:00 2001 From: Vaxry <43317083+vaxerski@users.noreply.github.com> Date: Thu, 27 Oct 2022 09:48:42 +0100 Subject: [PATCH 4/4] Update ConfigManager.cpp --- src/config/ConfigManager.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 9e51c6e..1a1f492 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -41,7 +41,6 @@ CConfigManager::CConfigManager() { } if (!parseError.empty()) { - // If an error there is, user may want the precise line where it occured parseError = "Config error at line " + std::to_string(linenum) + ": " + parseError; break; }