diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2018-12-11 07:42:05 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2018-12-11 07:42:05 +0100 |
| commit | ed419140280553b070943b7ba539120a26ff5686 (patch) | |
| tree | 3d134977a6dfae71a45c318305166d044b50320d /src/wanda/xdg.cpp | |
| parent | d3e691c9200b7b782c8acf17468068a699588a73 (diff) | |
| download | wanda-ed419140280553b070943b7ba539120a26ff5686.tar.xz wanda-ed419140280553b070943b7ba539120a26ff5686.zip | |
wanda: restructure directory hierarchy
Diffstat (limited to 'src/wanda/xdg.cpp')
| -rw-r--r-- | src/wanda/xdg.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/wanda/xdg.cpp b/src/wanda/xdg.cpp new file mode 100644 index 0000000..d49e53f --- /dev/null +++ b/src/wanda/xdg.cpp @@ -0,0 +1,46 @@ +#include <wanda/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 |
