add some methods to Poco::Path for make it easy to follow XDG Base Directory Specification

This commit is contained in:
Björn Schramke
2014-10-09 09:46:21 +02:00
committed by Alex Fabijanic
parent 275275baa9
commit b59329034f
7 changed files with 96 additions and 50 deletions

View File

@@ -301,6 +301,11 @@ public:
/// On Unix systems, this is the '~/.local/share/'. On Windows systems, /// On Unix systems, this is the '~/.local/share/'. On Windows systems,
/// this is '%APPDATA%'. /// this is '%APPDATA%'.
static std::string tempHome();
/// Returns the user's temp directory.
///
/// On Unix systems, this is the '~/.local/temp/'.
static std::string cacheHome(); static std::string cacheHome();
/// Returns the user's cache directory. /// Returns the user's cache directory.
/// ///
@@ -310,6 +315,11 @@ public:
static std::string temp(); static std::string temp();
/// Returns the temporary directory. /// Returns the temporary directory.
static std::string config();
/// Returns the systemwide config directory.
///
/// On Unix systems, this is the '/etc/'.
static std::string null(); static std::string null();
/// Returns the name of the null device. /// Returns the name of the null device.

View File

@@ -32,8 +32,10 @@ public:
static std::string homeImpl(); static std::string homeImpl();
static std::string configHomeImpl(); static std::string configHomeImpl();
static std::string dataHomeImpl(); static std::string dataHomeImpl();
static std::string tempHomeImpl();
static std::string cacheHomeImpl(); static std::string cacheHomeImpl();
static std::string tempImpl(); static std::string tempImpl();
static std::string configImpl();
static std::string nullImpl(); static std::string nullImpl();
static std::string expandImpl(const std::string& path); static std::string expandImpl(const std::string& path);
static void listRootsImpl(std::vector<std::string>& roots); static void listRootsImpl(std::vector<std::string>& roots);

View File

@@ -569,9 +569,39 @@ std::string Path::home()
} }
std::string Path::configHome()
{
#if defined(POCO_OS_FAMILY_UNIX)
return PathImpl::configHomeImpl();
#else
return PathImpl::homeImpl();
#endif
}
std::string Path::dataHome()
{
#if defined(POCO_OS_FAMILY_UNIX)
return PathImpl::dataHomeImpl();
#else
return PathImpl::homeImpl();
#endif
}
std::string Path::tempHome()
{
#if defined(POCO_OS_FAMILY_UNIX)
return PathImpl::tempHomeImpl();
#else
return PathImpl::tempImpl();
#endif
}
std::string Path::cacheHome() std::string Path::cacheHome()
{ {
#if defined(POCO_OS_FAMILY_UNIX) || defined(POCO_OS_FAMILY_WINDOWS) #if defined(POCO_OS_FAMILY_UNIX)
return PathImpl::cacheHomeImpl(); return PathImpl::cacheHomeImpl();
#else #else
return PathImpl::homeImpl(); return PathImpl::homeImpl();
@@ -585,6 +615,15 @@ std::string Path::temp()
} }
std::string Path::config()
{
#if defined(POCO_OS_FAMILY_UNIX)
return PathImpl::configImpl();
#else
return PathImpl::currentImpl();
#endif
}
std::string Path::null() std::string Path::null()
{ {
return PathImpl::nullImpl(); return PathImpl::nullImpl();

View File

@@ -78,24 +78,10 @@ std::string PathImpl::configHomeImpl()
{ {
#if defined(POCO_VXWORKS) #if defined(POCO_VXWORKS)
return PathImpl::homeImpl(); return PathImpl::homeImpl();
#elif POCO_OS == POCO_OS_MAC_OS_X #else
std::string path = PathImpl::homeImpl(); std::string path = PathImpl::homeImpl();
std::string::size_type n = path.size(); std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/') if (n > 0 && path[n - 1] == '/') path.append(".config/");
path.append("Library/Preferences/");
return path;
#else
std::string path;
if (EnvironmentImpl::hasImpl("XDG_CONFIG_HOME"))
path = EnvironmentImpl::getImpl("XDG_CONFIG_HOME");
if (!path.empty())
return path;
path = PathImpl::homeImpl();
std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/')
path.append(".config/");
return path; return path;
#endif #endif
} }
@@ -105,24 +91,10 @@ std::string PathImpl::dataHomeImpl()
{ {
#if defined(POCO_VXWORKS) #if defined(POCO_VXWORKS)
return PathImpl::homeImpl(); return PathImpl::homeImpl();
#elif POCO_OS == POCO_OS_MAC_OS_X #else
std::string path = PathImpl::homeImpl(); std::string path = PathImpl::homeImpl();
std::string::size_type n = path.size(); std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/') if (n > 0 && path[n - 1] == '/') path.append(".local/share/");
path.append("Library/Application Support/");
return path;
#else
std::string path;
if (EnvironmentImpl::hasImpl("XDG_DATA_HOME"))
path = EnvironmentImpl::getImpl("XDG_DATA_HOME");
if (!path.empty())
return path;
path = PathImpl::homeImpl();
std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/')
path.append(".local/share/");
return path; return path;
#endif #endif
} }
@@ -132,24 +104,23 @@ std::string PathImpl::cacheHomeImpl()
{ {
#if defined(POCO_VXWORKS) #if defined(POCO_VXWORKS)
return PathImpl::tempImpl(); return PathImpl::tempImpl();
#elif POCO_OS == POCO_OS_MAC_OS_X #else
std::string path = PathImpl::homeImpl(); std::string path = PathImpl::homeImpl();
std::string::size_type n = path.size(); std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/') if (n > 0 && path[n - 1] == '/') path.append(".cache/");
path.append("Library/Caches/");
return path; return path;
#endif
}
std::string PathImpl::tempHomeImpl()
{
#if defined(POCO_VXWORKS)
return PathImpl::tempImpl();
#else #else
std::string path; std::string path = PathImpl::homeImpl();
if (EnvironmentImpl::hasImpl("XDG_CACHE_HOME"))
path = EnvironmentImpl::getImpl("XDG_CACHE_HOME");
if (!path.empty())
return path;
path = PathImpl::homeImpl();
std::string::size_type n = path.size(); std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/') if (n > 0 && path[n - 1] == '/') path.append(".local/tmp/");
path.append(".cache/");
return path; return path;
#endif #endif
} }
@@ -173,6 +144,14 @@ std::string PathImpl::tempImpl()
} }
std::string PathImpl::configImpl()
{
std::string path;
path = "/etc/";
return path;
}
std::string PathImpl::nullImpl() std::string PathImpl::nullImpl()
{ {
#if defined(POCO_VXWORKS) #if defined(POCO_VXWORKS)

View File

@@ -80,7 +80,9 @@ private:
static const std::string CONFIGHOMEDIR; static const std::string CONFIGHOMEDIR;
static const std::string CACHEHOMEDIR; static const std::string CACHEHOMEDIR;
static const std::string DATAHOMEDIR; static const std::string DATAHOMEDIR;
static const std::string TEMPHOMEDIR;
static const std::string TEMPDIR; static const std::string TEMPDIR;
static const std::string CONFIGDIR;
static const std::string DATETIME; static const std::string DATETIME;
#if !defined(POCO_VXWORKS) #if !defined(POCO_VXWORKS)
static const std::string PID; static const std::string PID;

View File

@@ -24,6 +24,7 @@
#endif #endif
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include <cstdio> #include <cstdio>
#include "../../Foundation/include/Poco/Path.h"
using Poco::Environment; using Poco::Environment;
@@ -44,7 +45,9 @@ const std::string SystemConfiguration::HOMEDIR = "system.homeDir";
const std::string SystemConfiguration::CONFIGHOMEDIR = "system.configHomeDir"; const std::string SystemConfiguration::CONFIGHOMEDIR = "system.configHomeDir";
const std::string SystemConfiguration::CACHEHOMEDIR = "system.cacheHomeDir"; const std::string SystemConfiguration::CACHEHOMEDIR = "system.cacheHomeDir";
const std::string SystemConfiguration::DATAHOMEDIR = "system.dataHomeDir"; const std::string SystemConfiguration::DATAHOMEDIR = "system.dataHomeDir";
const std::string SystemConfiguration::TEMPHOMEDIR = "system.tempHomeDir";
const std::string SystemConfiguration::TEMPDIR = "system.tempDir"; const std::string SystemConfiguration::TEMPDIR = "system.tempDir";
const std::string SystemConfiguration::CONFIGDIR = "system.configDir";
const std::string SystemConfiguration::DATETIME = "system.dateTime"; const std::string SystemConfiguration::DATETIME = "system.dateTime";
#if !defined(POCO_VXWORKS) #if !defined(POCO_VXWORKS)
const std::string SystemConfiguration::PID = "system.pid"; const std::string SystemConfiguration::PID = "system.pid";
@@ -121,10 +124,19 @@ bool SystemConfiguration::getRaw(const std::string& key, std::string& value) con
{ {
value = Path::dataHome(); value = Path::dataHome();
} }
else if (key == TEMPHOMEDIR)
{
value = Path::tempHome();
}
else if (key == TEMPDIR) else if (key == TEMPDIR)
{ {
value = Path::temp(); value = Path::temp();
} }
else if (key == CONFIGDIR)
{
value = Path::config();
}
else if (key == DATETIME) else if (key == DATETIME)
{ {
value = Poco::DateTimeFormatter::format(Poco::DateTime(), Poco::DateTimeFormat::ISO8601_FORMAT); value = Poco::DateTimeFormatter::format(Poco::DateTime(), Poco::DateTimeFormat::ISO8601_FORMAT);
@@ -169,7 +181,9 @@ void SystemConfiguration::enumerate(const std::string& key, Keys& range) const
range.push_back("configHomeDir"); range.push_back("configHomeDir");
range.push_back("cacheHomeDir"); range.push_back("cacheHomeDir");
range.push_back("dataHomeDir"); range.push_back("dataHomeDir");
range.push_back("tempHomeDir");
range.push_back("tempDir"); range.push_back("tempDir");
range.push_back("configDir");
range.push_back("dateTime"); range.push_back("dateTime");
#if !defined(POCO_VXWORKS) #if !defined(POCO_VXWORKS)
range.push_back("pid"); range.push_back("pid");

View File

@@ -84,7 +84,7 @@ void SystemConfigurationTest::testKeys()
#if defined(POCO_VXWORKS) #if defined(POCO_VXWORKS)
assert (keys.size() == 10); assert (keys.size() == 10);
#else #else
assert (keys.size() == 11); assert (keys.size() == 16);
#endif #endif
assert (std::find(keys.begin(), keys.end(), "osName") != keys.end()); assert (std::find(keys.begin(), keys.end(), "osName") != keys.end());
assert (std::find(keys.begin(), keys.end(), "osVersion") != keys.end()); assert (std::find(keys.begin(), keys.end(), "osVersion") != keys.end());