Add a tool's function to get the data directory.

The data directory is where kiwix application should store data.
This commit is contained in:
Matthieu Gautier 2018-08-29 12:04:40 +02:00
parent 9b516ac35d
commit f4846c1ac8
2 changed files with 27 additions and 0 deletions

View File

@ -59,5 +59,6 @@ bool copyFile(const string& sourcePath, const string& destPath);
string getLastPathElement(const string& path); string getLastPathElement(const string& path);
string getExecutablePath(); string getExecutablePath();
string getCurrentDirectory(); string getCurrentDirectory();
string getDataDirectory();
bool writeTextFile(const string& path, const string& content); bool writeTextFile(const string& path, const string& content);
#endif #endif

View File

@ -310,3 +310,29 @@ string getCurrentDirectory()
free(a_cwd); free(a_cwd);
return s_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");
}