diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2018-11-30 10:48:44 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2018-11-30 10:48:44 +0100 |
| commit | 5b2974cd16b5d2841b72c7d0cc4a34469a8ded5b (patch) | |
| tree | 27fe6f0b428a1e02f7fe7c677795bdd989e0498c /src/xdg.cpp | |
| parent | 543e91dc65d839864b42114b353d0c30db4dcdd1 (diff) | |
| download | wanda-5b2974cd16b5d2841b72c7d0cc4a34469a8ded5b.tar.xz wanda-5b2974cd16b5d2841b72c7d0cc4a34469a8ded5b.zip | |
core: implement basic XDG Base Specification support
Diffstat (limited to 'src/xdg.cpp')
| -rw-r--r-- | src/xdg.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/xdg.cpp b/src/xdg.cpp new file mode 100644 index 0000000..9293151 --- /dev/null +++ b/src/xdg.cpp @@ -0,0 +1,47 @@ +#include "xdg.hpp" + +#include <unistd.h> + +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 |
