From f4d9880d3c9555b48affad727589ef5c093b1841 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 16 Sep 2022 23:28:09 +0200 Subject: source: clean up structure --- source/lib/src/xdg.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 source/lib/src/xdg.cpp (limited to 'source/lib/src/xdg.cpp') diff --git a/source/lib/src/xdg.cpp b/source/lib/src/xdg.cpp new file mode 100644 index 0000000..cfd1719 --- /dev/null +++ b/source/lib/src/xdg.cpp @@ -0,0 +1,46 @@ +#include "wanda/xdg.hpp" + +#include + +namespace wanda +{ + std::string xdg_variable(xdg_directory directory) + { + switch (directory) + { + case xdg_directory::data_home: + return "XDG_DATA_HOME"; + case xdg_directory::config_home: + return "XDG_CONFIG_HOME"; + case xdg_directory::cache_home: + return "XDG_CACHE_HOME"; + case xdg_directory::runtime_dir: + return "XDG_RUNTIME_DIR"; + } + return "XDG_INVALID_PATH"; + } + + std::filesystem::path xdg_path_for(xdg_directory directory, environment const & environment) + { + if (auto path = environment[xdg_variable(directory)]; !path.empty()) + { + return path; + } + + auto home = std::filesystem::path{environment["HOME"]}; + switch (directory) + { + case xdg_directory::data_home: + return home / ".local/share"; + case xdg_directory::config_home: + return home / ".config"; + case xdg_directory::cache_home: + return home / ".cache"; + case xdg_directory::runtime_dir: + return std::filesystem::path{"/run/user"} / std::to_string(::getuid()); + } + + return ""; + } + +} // namespace wanda \ No newline at end of file -- cgit v1.2.3