From 1a20debf5680f8377c23db3f9d5a7c8d69b351a7 Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Tue, 19 Oct 2021 09:49:44 +0200 Subject: [PATCH] pathut: add path_cachedir --- src/utils/pathut.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ src/utils/pathut.h | 3 +++ 2 files changed, 45 insertions(+) diff --git a/src/utils/pathut.cpp b/src/utils/pathut.cpp index 2f81649a..52421039 100644 --- a/src/utils/pathut.cpp +++ b/src/utils/pathut.cpp @@ -684,6 +684,48 @@ string path_home() #endif } +string path_cachedir() +{ +#ifdef _WIN32 + string dir; + wchar_t *cp; + SHGetKnownFolderPath(FOLDERID_InternetCache, 0, nullptr, &cp); + if (cp != 0) { + wchartoutf8(cp, dir); + } + if (dir.empty()) { + cp = _wgetenv(L"HOMEDRIVE"); + wchartoutf8(cp, dir); + if (cp != 0) { + string dir1; + const wchar_t *cp1 = _wgetenv(L"HOMEPATH"); + wchartoutf8(cp1, dir1); + if (cp1 != 0) { + dir = path_cat(dir, dir1); + } + } + } + if (dir.empty()) { + dir = "C:/"; + } + dir = path_canon(dir); + path_catslash(dir); + return dir; +#else + static string xdgcache; + if (xdgcache.empty()) { + const char *cp = getenv("XDG_CACHE_HOME"); + if (cp == 0) { + xdgcache = path_cat(path_home(), ".cache"); + } else { + xdgcache = string(cp); + } + path_catslash(xdgcache); + } + return xdgcache; +#endif +} + string path_tildexpand(const string& s) { if (s.empty() || s[0] != '~') { diff --git a/src/utils/pathut.h b/src/utils/pathut.h index 195c2989..e19203d9 100644 --- a/src/utils/pathut.h +++ b/src/utils/pathut.h @@ -56,6 +56,9 @@ bool path_samefile(const std::string& p1, const std::string& p2); /// Get the current user's home directory extern std::string path_home(); +/// Get the top location for cached data +extern std::string path_cachedir(); + /// Expand ~ at the beginning of std::string extern std::string path_tildexpand(const std::string& s); /// Use getcwd() to make absolute path if needed. Beware: ***this can fail***