46 std::string config_home_;
47 std::string data_home_;
48 std::string cache_home_;
49 std::string runtime_dir_;
50 std::vector<std::string> config_dirs_;
51 std::vector<std::string> data_dirs_;
53 auto is_absolute(
const std::string& path)
const noexcept ->
bool
55 return path.length() > 0 && path[0] ==
'/';
58 auto split(
const std::string& arg)
const -> std::vector<std::string>
60 std::stringstream iss(arg);
62 std::vector<std::string> elems;
64 while (std::getline(iss, item,
':')) {
65 if (is_absolute(item))
66 elems.push_back(item);
72 auto env_or_home(
const std::string& var,
const std::string& repl)
const -> std::string
74 auto value = std::getenv(var.c_str());
76 if (value ==
nullptr || !is_absolute(value)) {
77 auto home = std::getenv(
"HOME");
80 throw std::runtime_error(
"could not get home directory");
82 return std::string(home) +
"/" + repl;
88 auto list_or_defaults(
const std::string& var,
89 const std::vector<std::string>& list)
const -> std::vector<std::string>
91 const auto value = std::getenv(var.c_str());
97 if (
const auto result = split(value); !result.empty())
110 : config_home_(env_or_home(
"XDG_CONFIG_HOME",
".config"))
111 , data_home_(env_or_home(
"XDG_DATA_HOME",
".local/share"))
112 , cache_home_(env_or_home(
"XDG_CACHE_HOME",
".cache"))
113 , config_dirs_(list_or_defaults(
"XDG_CONFIG_DIRS", {
"/etc/xdg" }))
114 , data_dirs_(list_or_defaults(
"XDG_DATA_DIRS", {
"/usr/local/share",
"/usr/share" }))
121 if (
const auto runtime = std::getenv(
"XDG_RUNTIME_DIR"); runtime && is_absolute(runtime))
122 runtime_dir_ = runtime;
XDG directory specifications.
Definition: xdg.hpp:44
auto get_runtime_dir() const noexcept -> const std::string &
Definition: xdg.hpp:163
auto get_data_home() const noexcept -> const std::string &
Definition: xdg.hpp:140
auto get_data_dirs() const noexcept -> const std::vector< std::string > &
Definition: xdg.hpp:184
auto get_cache_home() const noexcept -> const std::string &
Definition: xdg.hpp:150
auto get_config_dirs() const noexcept -> const std::vector< std::string > &
Definition: xdg.hpp:173
xdg()
Definition: xdg.hpp:109
auto get_config_home() const noexcept -> const std::string &
Definition: xdg.hpp:130
Parent namespace.
Definition: acceptor.hpp:43