From b63e25d6e7988e57f4e548c1d32dc74d5e845b3f Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Wed, 1 Jun 2016 16:59:03 +0200 Subject: [PATCH] fix(core): Nullptr checks --- include/utils/config.hpp | 2 +- src/lemonbuddy.cpp | 27 +++++++++++++++------------ src/utils/config.cpp | 4 ++-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/include/utils/config.hpp b/include/utils/config.hpp index 0ed3d95a..f689c9bf 100644 --- a/include/utils/config.hpp +++ b/include/utils/config.hpp @@ -48,7 +48,7 @@ namespace config void set_bar_path(const std::string& path); void load(const std::string& path) throw(UnexistingFileError, ParseError); - void load(char *dir, const std::string& path); + void load(const char *dir, const std::string& path); void reload() throw(ParseError); boost::property_tree::ptree get_tree(); diff --git a/src/lemonbuddy.cpp b/src/lemonbuddy.cpp index 17a41e9a..62b209c8 100644 --- a/src/lemonbuddy.cpp +++ b/src/lemonbuddy.cpp @@ -93,19 +93,22 @@ int main(int argc, char **argv) /** * Load configuration file */ - if (cli::has_option("config")) { - config::load(string::replace(cli::get_option_value("config"), "~", std::getenv("HOME"))); - } else { - auto xdg_config_home = std::getenv("XDG_CONFIG_HOME"); - auto home = std::getenv("HOME"); + const char *env_home = std::getenv("HOME"); + const char *env_xdg_config_home = std::getenv("XDG_CONFIG_HOME"); - if (xdg_config_home != nullptr) - config::load(xdg_config_home, "lemonbuddy/config"); - else if (home != nullptr) - config::load(home, ".config/lemonbuddy/config"); - else - throw ApplicationError("Could not find config file. Specify the location using --config=PATH"); - } + if (cli::has_option("config")) { + auto config_file = cli::get_option_value("config"); + + if (env_home != nullptr) + config_file = string::replace(cli::get_option_value("config"), "~", std::string(env_home)); + + config::load(config_file); + } else if (env_xdg_config_home != nullptr) + config::load(env_xdg_config_home, "lemonbuddy/config"); + else if (env_home != nullptr) + config::load(env_home, ".config/lemonbuddy/config"); + else + throw ApplicationError("Could not find config file. Specify the location using --config=PATH"); /** * Check if the specified bar exist diff --git a/src/utils/config.cpp b/src/utils/config.cpp index 390beeed..832c9d51 100644 --- a/src/utils/config.cpp +++ b/src/utils/config.cpp @@ -34,8 +34,8 @@ namespace config file_path = path; } - void load(char *dir, const std::string& path) { - load(std::string(dir) +"/"+ path); + void load(const char *dir, const std::string& path) { + load(std::string(dir != nullptr ? dir : "") +"/"+ path); } void reload() throw(ParseError)