From f4846c1ac8903bc491037b2b5877242158142a1c Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 29 Aug 2018 12:04:40 +0200 Subject: [PATCH] Add a tool's function to get the data directory. The data directory is where kiwix application should store data. --- include/common/pathTools.h | 1 + src/common/pathTools.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/common/pathTools.h b/include/common/pathTools.h index c63fad275..1ea7ae4e0 100644 --- a/include/common/pathTools.h +++ b/include/common/pathTools.h @@ -59,5 +59,6 @@ bool copyFile(const string& sourcePath, const string& destPath); string getLastPathElement(const string& path); string getExecutablePath(); string getCurrentDirectory(); +string getDataDirectory(); bool writeTextFile(const string& path, const string& content); #endif diff --git a/src/common/pathTools.cpp b/src/common/pathTools.cpp index 876263c04..cb76475e1 100644 --- a/src/common/pathTools.cpp +++ b/src/common/pathTools.cpp @@ -310,3 +310,29 @@ string getCurrentDirectory() free(a_cwd); return s_cwd; } + +string getDataDirectory() +{ +#ifdef _WIN32 + char* cDataDir = ::getenv("APPDATA"); +#else + char* cDataDir = ::getenv("KIWIX_DATA_DIR"); +#endif + std::string dataDir = cDataDir==nullptr ? "" : cDataDir; + if (!dataDir.empty()) + return dataDir; +#ifdef _WIN32 + cDataDir = ::getenv("USERPROFILE"); + dataDir = cDataDir==nullptr ? getCurrentDirectory() : cDataDir; +#else + cDataDir = ::getenv("XDG_DATA_HOME"); + dataDir = cDataDir==nullptr ? "" : cDataDir; + if (dataDir.empty()) { + cDataDir = ::getenv("HOME"); + dataDir = cDataDir==nullptr ? getCurrentDirectory() : cDataDir; + dataDir = appendToDirectory(dataDir, ".local"); + dataDir = appendToDirectory(dataDir, "share"); + } +#endif + return appendToDirectory(dataDir, "kiwix"); +}