mirror of https://github.com/kiwix/libkiwix.git
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:
parent
9b516ac35d
commit
f4846c1ac8
|
@ -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
|
||||||
|
|
|
@ -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");
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue