From eb6f0f710c9089cf327fbeb967542a59c8a0299e Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 14 Apr 2020 12:12:34 +0200 Subject: [PATCH] Correctly detetect the dataDir on windows. We must use the wide version of the getenv to correctly handle the case we have accents in the user directory. This also change the default dataDirectory on windows from $APPDATA to $APPDATA/kiwix. --- src/tools/pathTools.cpp | 45 ++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/tools/pathTools.cpp b/src/tools/pathTools.cpp index b4381789a..1cf270611 100644 --- a/src/tools/pathTools.cpp +++ b/src/tools/pathTools.cpp @@ -364,30 +364,47 @@ std::string getCurrentDirectory() std::string getDataDirectory() { +// Try to get the dataDir from the `KIWIX_DATA_DIR` env var #ifdef _WIN32 - char* cDataDir = ::getenv("APPDATA"); + wchar_t* cDataDir = ::_wgetenv(L"KIWIX_DATA_DIR"); + if (cDataDir != nullptr) { + return WideToUtf8(cDataDir); + } #else char* cDataDir = ::getenv("KIWIX_DATA_DIR"); -#endif - std::string dataDir = cDataDir==nullptr ? "" : cDataDir; - if (!dataDir.empty()) { - return dataDir; + if (cDataDir != nullptr) { + return cDataDir; } +#endif + +// Compute the dataDir from the user directory. + std::string dataDir; #ifdef _WIN32 - cDataDir = ::getenv("USERPROFILE"); - dataDir = cDataDir==nullptr ? getCurrentDirectory() : cDataDir; + cDataDir = ::_wgetenv(L"APPDATA"); + if (cDataDir == nullptr) + cDataDir = ::_wgetenv(L"USERPROFILE"); + if (cDataDir != nullptr) + dataDir = WideToUtf8(cDataDir); #else cDataDir = ::getenv("XDG_DATA_HOME"); - dataDir = cDataDir==nullptr ? "" : cDataDir; - if (dataDir.empty()) { + if (cDataDir != nullptr) { + dataDir = cDataDir; + } else { cDataDir = ::getenv("HOME"); - dataDir = cDataDir==nullptr ? getCurrentDirectory() : cDataDir; - dataDir = appendToDirectory(dataDir, ".local"); - dataDir = appendToDirectory(dataDir, "share"); + if (cDataDir != nullptr) { + dataDir = cDataDir; + dataDir = appendToDirectory(dataDir, ".local"); + dataDir = appendToDirectory(dataDir, "share"); + } } #endif - auto ret = appendToDirectory(dataDir, "kiwix"); - return ret; + if (!dataDir.empty()) { + dataDir = appendToDirectory(dataDir, "kiwix"); + return dataDir; + } + +// Let's use the currentDirectory + return getCurrentDirectory(); } static std::map extMimeTypes = {