From 27e92a2c4b888f0b8b263a0b1a474ae4a8978dfb Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 24 Aug 2021 21:39:47 +0200 Subject: [PATCH] Add helpers::get_config_file_path(). And use it everywhere. --- helpers.cpp | 22 +++++++++++++++++++++- helpers.hpp | 4 ++++ statusip.cpp | 15 +-------------- statustemp.cpp | 15 +-------------- statusweather.cpp | 16 ++-------------- 5 files changed, 29 insertions(+), 43 deletions(-) diff --git a/helpers.cpp b/helpers.cpp index df3ddfe..cf62d6d 100644 --- a/helpers.cpp +++ b/helpers.cpp @@ -7,7 +7,7 @@ namespace helpers { -std::string get_env(std::string_view name) +std::string get_env(const std::string_view name) { const char *env{std::getenv(name.data())}; // NOLINT(concurrency-mt-unsafe) if (env != nullptr) @@ -18,4 +18,24 @@ std::string get_env(std::string_view name) return {}; } +std::string get_config_file_path(const std::string_view filename) +{ + auto path{helpers::get_env("XDG_CONFIG_HOME")}; + if (path.empty()) + { + path = helpers::get_env("HOME"); + if (!path.empty()) + { + path += "/.config"; + } + } + if (!path.empty()) + { + path += "/"; + } + path += filename; + + return path; +} + } // namespace helpers diff --git a/helpers.hpp b/helpers.hpp index 964e9f3..7b7fddc 100644 --- a/helpers.hpp +++ b/helpers.hpp @@ -7,8 +7,12 @@ namespace helpers { +//! returns environment variable or empty string. std::string get_env(std::string_view name); +//! Returns the full path for a config file. +std::string get_config_file_path(std::string_view filename); + } // namespace helpers #endif // CPPSCRIPTS_HELPERS_HPP diff --git a/statusip.cpp b/statusip.cpp index c6d39af..78fe3e4 100644 --- a/statusip.cpp +++ b/statusip.cpp @@ -39,20 +39,7 @@ std::string get_url(int argc, char *argv[]) po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(options).run(), vm); - const auto path{[]() - { - auto path{helpers::get_env("XDG_CONFIG_HOME")}; - if (path.empty()) - { - path = helpers::get_env("HOME"); - if (!path.empty()) - { - path += "/.config"; - } - } - return path += "/statusip.cfg"; - }()}; - std::ifstream configfile(path); + std::ifstream configfile(helpers::get_config_file_path("statusip.cfg")); po::store(po::parse_config_file(configfile, options, true), vm); configfile.close(); diff --git a/statustemp.cpp b/statustemp.cpp index 43d99d4..87ce354 100644 --- a/statustemp.cpp +++ b/statustemp.cpp @@ -37,20 +37,7 @@ std::string get_file_path(int argc, char *argv[]) po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(options).run(), vm); - const auto path{[]() - { - auto path{helpers::get_env("XDG_CONFIG_HOME")}; - if (path.empty()) - { - path = helpers::get_env("HOME"); - if (!path.empty()) - { - path += "/.config"; - } - } - return path += "/statustemp.cfg"; - }()}; - std::ifstream configfile(path); + std::ifstream configfile(helpers::get_config_file_path("statustemp.cfg")); po::store(po::parse_config_file(configfile, options, true), vm); configfile.close(); diff --git a/statusweather.cpp b/statusweather.cpp index 47fcd29..79da865 100644 --- a/statusweather.cpp +++ b/statusweather.cpp @@ -88,20 +88,8 @@ std::tuple get_options() // clang-format on po::variables_map vm; - const auto path{[]() - { - auto path{helpers::get_env("XDG_CONFIG_HOME")}; - if (path.empty()) - { - path = helpers::get_env("HOME"); - if (!path.empty()) - { - path += "/.config"; - } - } - return path += "/statusweather.cfg"; - }()}; - std::ifstream configfile(path); + std::ifstream configfile( + helpers::get_config_file_path("statusweather.cfg")); po::store(po::parse_config_file(configfile, options, true), vm); configfile.close();