Add helpers::get_config_file_path().
And use it everywhere.
This commit is contained in:
parent
6d3d5b94ad
commit
27e92a2c4b
22
helpers.cpp
22
helpers.cpp
@ -7,7 +7,7 @@
|
|||||||
namespace helpers
|
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)
|
const char *env{std::getenv(name.data())}; // NOLINT(concurrency-mt-unsafe)
|
||||||
if (env != nullptr)
|
if (env != nullptr)
|
||||||
@ -18,4 +18,24 @@ std::string get_env(std::string_view name)
|
|||||||
return {};
|
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
|
} // namespace helpers
|
||||||
|
@ -7,8 +7,12 @@
|
|||||||
namespace helpers
|
namespace helpers
|
||||||
{
|
{
|
||||||
|
|
||||||
|
//! returns environment variable or empty string.
|
||||||
std::string get_env(std::string_view name);
|
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
|
} // namespace helpers
|
||||||
|
|
||||||
#endif // CPPSCRIPTS_HELPERS_HPP
|
#endif // CPPSCRIPTS_HELPERS_HPP
|
||||||
|
15
statusip.cpp
15
statusip.cpp
@ -39,20 +39,7 @@ std::string get_url(int argc, char *argv[])
|
|||||||
po::variables_map vm;
|
po::variables_map vm;
|
||||||
po::store(po::command_line_parser(argc, argv).options(options).run(), vm);
|
po::store(po::command_line_parser(argc, argv).options(options).run(), vm);
|
||||||
|
|
||||||
const auto path{[]()
|
std::ifstream configfile(helpers::get_config_file_path("statusip.cfg"));
|
||||||
{
|
|
||||||
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);
|
|
||||||
po::store(po::parse_config_file(configfile, options, true), vm);
|
po::store(po::parse_config_file(configfile, options, true), vm);
|
||||||
configfile.close();
|
configfile.close();
|
||||||
|
|
||||||
|
@ -37,20 +37,7 @@ std::string get_file_path(int argc, char *argv[])
|
|||||||
po::variables_map vm;
|
po::variables_map vm;
|
||||||
po::store(po::command_line_parser(argc, argv).options(options).run(), vm);
|
po::store(po::command_line_parser(argc, argv).options(options).run(), vm);
|
||||||
|
|
||||||
const auto path{[]()
|
std::ifstream configfile(helpers::get_config_file_path("statustemp.cfg"));
|
||||||
{
|
|
||||||
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);
|
|
||||||
po::store(po::parse_config_file(configfile, options, true), vm);
|
po::store(po::parse_config_file(configfile, options, true), vm);
|
||||||
configfile.close();
|
configfile.close();
|
||||||
|
|
||||||
|
@ -88,20 +88,8 @@ std::tuple<std::string, std::string> get_options()
|
|||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
po::variables_map vm;
|
po::variables_map vm;
|
||||||
const auto path{[]()
|
std::ifstream configfile(
|
||||||
{
|
helpers::get_config_file_path("statusweather.cfg"));
|
||||||
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);
|
|
||||||
po::store(po::parse_config_file(configfile, options, true), vm);
|
po::store(po::parse_config_file(configfile, options, true), vm);
|
||||||
configfile.close();
|
configfile.close();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user