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
parent ef34c21e91
commit ce56190f9f
6 changed files with 178 additions and 0 deletions

View File

@ -291,9 +291,37 @@ public:
static std::string home();
/// Returns the user's home directory.
static std::string configHome();
/// Returns the user's config directory.
///
/// On Unix systems, this is the '~/.config/'. On Windows systems,
/// this is '%APPDATA%'.
static std::string dataHome();
/// Returns the user's data directory.
///
/// On Unix systems, this is the '~/.local/share/'. On Windows systems,
/// 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();
/// Returns the user's cache directory.
///
/// On Unix systems, this is the '~/.cache/'. On Windows systems,
/// this is '%APPDATA%'.
static std::string temp();
/// Returns the temporary directory.
static std::string config();
/// Returns the systemwide config directory.
///
/// On Unix systems, this is the '/etc/'.
static std::string null();
/// Returns the name of the null device.

View File

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

View File

@ -595,11 +595,60 @@ 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()
{
#if defined(POCO_OS_FAMILY_UNIX)
return PathImpl::cacheHomeImpl();
#else
return PathImpl::homeImpl();
#endif
}
std::string Path::temp()
{
return PathImpl::tempImpl();
}
std::string Path::config()
{
#if defined(POCO_OS_FAMILY_UNIX)
return PathImpl::configImpl();
#else
return PathImpl::currentImpl();
#endif
}
std::string Path::null()
{

View File

@ -76,6 +76,58 @@ std::string PathImpl::homeImpl()
}
std::string PathImpl::configHomeImpl()
{
#if defined(POCO_VXWORKS)
return PathImpl::homeImpl();
#else
std::string path = PathImpl::homeImpl();
std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/') path.append(".config/");
return path;
#endif
}
std::string PathImpl::dataHomeImpl()
{
#if defined(POCO_VXWORKS)
return PathImpl::homeImpl();
#else
std::string path = PathImpl::homeImpl();
std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/') path.append(".local/share/");
return path;
#endif
}
std::string PathImpl::cacheHomeImpl()
{
#if defined(POCO_VXWORKS)
return PathImpl::tempImpl();
#else
std::string path = PathImpl::homeImpl();
std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/') path.append(".cache/");
return path;
#endif
}
std::string PathImpl::tempHomeImpl()
{
#if defined(POCO_VXWORKS)
return PathImpl::tempImpl();
#else
std::string path = PathImpl::homeImpl();
std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/') path.append(".local/tmp/");
return path;
#endif
}
std::string PathImpl::tempImpl()
{
std::string path;
@ -94,6 +146,14 @@ std::string PathImpl::tempImpl()
}
std::string PathImpl::configImpl()
{
std::string path;
path = "/etc/";
return path;
}
std::string PathImpl::nullImpl()
{
#if defined(POCO_VXWORKS)

View File

@ -76,7 +76,12 @@ private:
static const std::string NODEID;
static const std::string CURRENTDIR;
static const std::string HOMEDIR;
static const std::string CONFIGHOMEDIR;
static const std::string CACHEHOMEDIR;
static const std::string DATAHOMEDIR;
static const std::string TEMPHOMEDIR;
static const std::string TEMPDIR;
static const std::string CONFIGDIR;
static const std::string DATETIME;
#if !defined(POCO_VXWORKS)
static const std::string PID;

View File

@ -26,6 +26,7 @@
#endif
#include "Poco/Exception.h"
#include <cstdio>
#include "../../Foundation/include/Poco/Path.h"
using Poco::Environment;
@ -43,7 +44,12 @@ const std::string SystemConfiguration::NODENAME = "system.nodeName";
const std::string SystemConfiguration::NODEID = "system.nodeId";
const std::string SystemConfiguration::CURRENTDIR = "system.currentDir";
const std::string SystemConfiguration::HOMEDIR = "system.homeDir";
const std::string SystemConfiguration::CONFIGHOMEDIR = "system.configHomeDir";
const std::string SystemConfiguration::CACHEHOMEDIR = "system.cacheHomeDir";
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::CONFIGDIR = "system.configDir";
const std::string SystemConfiguration::DATETIME = "system.dateTime";
#if !defined(POCO_VXWORKS)
const std::string SystemConfiguration::PID = "system.pid";
@ -108,10 +114,30 @@ bool SystemConfiguration::getRaw(const std::string& key, std::string& value) con
{
value = Path::home();
}
else if (key == CONFIGHOMEDIR)
{
value = Path::configHome();
}
else if (key == CACHEHOMEDIR)
{
value = Path::cacheHome();
}
else if (key == DATAHOMEDIR)
{
value = Path::dataHome();
}
else if (key == TEMPHOMEDIR)
{
value = Path::tempHome();
}
else if (key == TEMPDIR)
{
value = Path::temp();
}
else if (key == CONFIGDIR)
{
value = Path::config();
}
else if (key == DATETIME)
{
value = Poco::DateTimeFormatter::format(Poco::DateTime(), Poco::DateTimeFormat::ISO8601_FORMAT);
@ -153,7 +179,12 @@ void SystemConfiguration::enumerate(const std::string& key, Keys& range) const
range.push_back("nodeId");
range.push_back("currentDir");
range.push_back("homeDir");
range.push_back("configHomeDir");
range.push_back("cacheHomeDir");
range.push_back("dataHomeDir");
range.push_back("tempHomeDir");
range.push_back("tempDir");
range.push_back("configDir");
range.push_back("dateTime");
#if !defined(POCO_VXWORKS)
range.push_back("pid");