Last active
November 12, 2015 11:31
-
-
Save vosst/a8e29933e15a6e86044f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "xdg.h" | |
#include <cstdlib> | |
#include <stdexcept> | |
namespace | |
{ | |
std::string home_dir_or_throw() | |
{ | |
if (auto value = ::getenv("HOME")) | |
{ | |
return std::string{value}; | |
} | |
throw std::runtime_error{"Home directory is not set"}; | |
} | |
namespace env | |
{ | |
constexpr const char* xdg_data_home{"XDG_DATA_HOME"}; | |
constexpr const char* xdg_data_dirs{"XDG_DATA_DIRS"}; | |
constexpr const char* xdg_config_home{"XDG_CONFIG_HOME"}; | |
constexpr const char* xdg_config_dirs{"XDG_CONFIG_DIRS"}; | |
constexpr const char* xdg_cache_home{"XDG_CACHE_HOME"}; | |
constexpr const char* xdg_runtime_dir{"XDG_RUNTIME_DIR"}; | |
} | |
namespace impl | |
{ | |
class BaseDirSpecification : public xdg::BaseDirSpecification | |
{ | |
public: | |
static const BaseDirSpecification& instance() | |
{ | |
static const BaseDirSpecification spec; | |
return spec; | |
} | |
BaseDirSpecification() | |
{ | |
} | |
const xdg::Data& data() const override | |
{ | |
return data_; | |
} | |
const xdg::Config& config() const override | |
{ | |
return config_; | |
} | |
const xdg::Cache& cache() const override | |
{ | |
return cache_; | |
} | |
const xdg::Runtime& runtime() const override | |
{ | |
return runtime_; | |
} | |
private: | |
xdg::Data data_; | |
xdg::Config config_; | |
xdg::Cache cache_; | |
xdg::Runtime runtime_; | |
}; | |
} | |
} | |
std::string xdg::Data::home() const | |
{ | |
if (auto value = ::getenv(env::xdg_data_home)) | |
{ | |
} | |
} | |
std::string xdg::data::home() | |
{ | |
return impl::BaseDirSpecification::instance().data().home(); | |
} | |
std::vector<std::string> xdg::data::dirs() | |
{ | |
return impl::BaseDirSpecification::instance().data().dirs(); | |
} | |
std::string xdg::config::home() | |
{ | |
return impl::BaseDirSpecification::instance().config().home(); | |
} | |
std::vector<std::string> xdg::config::dirs() | |
{ | |
return impl::BaseDirSpecification::instance().config().dirs(); | |
} | |
std::string xdg::cache::home() | |
{ | |
return impl::BaseDirSpecification::instance().cache().home(); | |
} | |
std::string xdg::runtime::dir() | |
{ | |
return impl::BaseDirSpecification::instance().runtime().dir(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef XDG_H_ | |
#define XDG_H_ | |
#include <string> | |
#include <vector> | |
namespace xdg | |
{ | |
struct NotCopyable | |
{ | |
NotCopyable() = default; | |
NotCopyable(const NotCopyable&) = delete; | |
virtual ~NotCopyable() = default; | |
NotCopyable& operator=(const NotCopyable&) = delete; | |
}; | |
struct NotMoveable | |
{ | |
NotMoveable() = default; | |
NotMoveable(NotMoveable&&) = delete; | |
virtual ~NotMoveable() = default; | |
NotMoveable& operator=(NotMoveable&&) = delete; | |
}; | |
class Data : NotCopyable, NotMoveable | |
{ | |
public: | |
// home returns the base directory relative to which user specific | |
// data files should be stored. | |
virtual std::string home() const; | |
// dirs returns the preference-ordered set of base directories to | |
// search for data files in addition to the $XDG_DATA_HOME base | |
// directory. | |
virtual std::vector<std::string> dirs() const; | |
}; | |
class Config : NotCopyable, NotMoveable | |
{ | |
public: | |
// home returns the base directory relative to which user specific | |
// configuration files should be stored. | |
virtual std::string home() const; | |
// dirs returns the preference-ordered set of base directories to | |
// search for configuration files in addition to the | |
// $XDG_CONFIG_HOME base directory. | |
virtual std::vector<std::string> dirs() const; | |
}; | |
class Cache : NotCopyable, NotMoveable | |
{ | |
public: | |
// home returns the base directory relative to which user specific | |
// non-essential data files should be stored. | |
virtual std::string home() const; | |
}; | |
class Runtime : NotCopyable, NotMoveable | |
{ | |
public: | |
// home returns the base directory relative to which user-specific | |
// non-essential runtime files and other file objects (such as | |
// sockets, named pipes, ...) should be stored. | |
virtual std::string dir() const; | |
}; | |
// A BaseDirSpecification implements the XDG base dir specification: | |
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html | |
class BaseDirSpecification : NotCopyable, NotMoveable | |
{ | |
public: | |
// data returns an immutable Data instance. | |
virtual const Data& data() const = 0; | |
// config returns an immutable Config instance. | |
virtual const Config& config() const = 0; | |
// cache returns an immutable Cache instance. | |
virtual const Cache& cache() const = 0; | |
// runtime returns an immutable Runtime instance. | |
virtual const Runtime& runtime() const = 0; | |
protected: | |
BaseDirSpecification() = default; | |
}; | |
namespace data | |
{ | |
// home returns the base directory relative to which user specific | |
// data files should be stored. | |
std::string home(); | |
// dirs returns the preference-ordered set of base directories to | |
// search for data files in addition to the $XDG_DATA_HOME base | |
// directory. | |
std::vector<std::string> dirs(); | |
} | |
namespace config | |
{ | |
// home returns the base directory relative to which user specific | |
// configuration files should be stored. | |
std::string home(); | |
// dirs returns the preference-ordered set of base directories to | |
// search for configuration files in addition to the | |
// $XDG_CONFIG_HOME base directory. | |
std::vector<std::string> dirs(); | |
} | |
namespace cache | |
{ | |
// home returns the base directory relative to which user specific | |
// non-essential data files should be stored. | |
std::string home(); | |
} | |
namespace runtime | |
{ | |
// home returns the base directory relative to which user-specific | |
// non-essential runtime files and other file objects (such as | |
// sockets, named pipes, ...) should be stored. | |
std::string dir(); | |
} | |
} | |
#endif // XDG_H_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment