Mercurial > code
changeset 239:9b69f810d3b9
Add Xdg, a replacement for XDG standard directories
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 12 Aug 2014 16:45:33 +0200 |
parents | 887d43215b90 |
children | f6d9fdb5eeeb |
files | C++/Xdg.cpp C++/Xdg.h |
diffstat | 2 files changed, 224 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Xdg.cpp Tue Aug 12 16:45:33 2014 +0200 @@ -0,0 +1,115 @@ +/* + * Xdg.cpp -- XDG directory specifications + * + * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <cstdlib> +#include <sstream> + +#include "xdg.h" + +namespace { + +std::vector<std::string> split(const std::string &arg) +{ + std::stringstream iss(arg); + std::string item; + std::vector<std::string> elems; + + while (std::getline(iss, item, ':')) + elems.push_back(item); + + return elems; +} + +std::string envOrHome(const std::string &var, const std::string &repl) +{ + auto value = getenv(var.c_str()); + + if (value == nullptr) { + auto home = getenv("HOME"); + + if (home == nullptr) + throw std::runtime_error("could not get home directory"); + + return std::string(home) + "/" + repl; + } + + return value; +} + +std::vector<std::string> listOrDefaults(const std::string &var, const std::vector<std::string> &list) +{ + auto value = getenv(var.c_str()); + + if (!value) + return list; + + return split(value); +} + +} // !namespace + +Xdg::Xdg() +{ + m_configHome = envOrHome("XDG_CONFIG_HOME", ".config"); + m_dataHome = envOrHome("XDG_DATA_HOME", ".local/share"); + m_cacheHome = envOrHome("XDG_CACHE_HOME", ".cache"); + + m_configDirs = listOrDefaults("XDG_CONFIG_DIRS", { "/etc/xdg" }); + m_dataDirs = listOrDefaults("XDG_DATA_DIRS", { "/usr/local/share", "/usr/share" }); + + /* + * Runtime directory is a special case and does not have a replacement, the + * application should manage this by itself. + */ + auto runtime = getenv("XDG_RUNTIME_DIR"); + if (runtime) + m_runtimeDir = runtime; +} + +const std::string &Xdg::configHome() const noexcept +{ + return m_configHome; +} + +const std::string &Xdg::dataHome() const noexcept +{ + return m_dataHome; +} + +const std::string &Xdg::cacheHome() const noexcept +{ + return m_cacheHome; +} + +const std::string &Xdg::runtimeDir() const +{ + if (m_runtimeDir.size() == 0) + throw std::runtime_error("XDG_RUNTIME_DIR is not set"); + + return m_runtimeDir; +} + +const Xdg::List &Xdg::configDirs() const noexcept +{ + return m_configDirs; +} + +const Xdg::List &Xdg::dataDirs() const noexcept +{ + return m_dataDirs; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Xdg.h Tue Aug 12 16:45:33 2014 +0200 @@ -0,0 +1,109 @@ +/* + * Xdg.h -- XDG directory specifications + * + * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _XDG_H_ +#define _XDG_H_ + +#include <vector> +#include <string> + +#if defined(_WIN32) +# if defined(BUILDING_DLL) +# define EXPORT __declspec(dllexport) +# else +# define EXPORT __declspec(dllimport) +# endif +#else +# define EXPORT +#endif + +/** + * @class Xdg + * @brief XDG specifications + * + * Read and get XDG directories. + */ +class EXPORT Xdg { +public: + using List = std::vector<std::string>; + +private: + std::string m_configHome; + std::string m_dataHome; + std::string m_cacheHome; + std::string m_runtimeDir; + List m_configDirs; + List m_dataDirs; + +public: + /** + * Open an xdg instance and load directories. + * + * @throw std::runtime_error on failures + */ + Xdg(); + + /** + * Get the config directory. ${XDG_CONFIG_HOME} or ${HOME}/.config + * + * @return the config directory + */ + const std::string &configHome() const noexcept; + + /** + * Get the data directory. ${XDG_DATA_HOME} or ${HOME}/.local/share + * + * @return the data directory + */ + const std::string &dataHome() const noexcept; + + /** + * Get the cache directory. ${XDG_CACHE_HOME} or ${HOME}/.cache + * + * @return the cache directory + */ + const std::string &cacheHome() const noexcept; + + /** + * Get the runtime directory. ${XDG_RUNTIME_DIR} must be set, + * if not, it throws an exception. + * + * The XDG standard says that application should handle XDG_RUNTIME_DIR by + * themselves. + * + * @return the runtime directory + * @throw std::runtime_error on error + */ + const std::string &runtimeDir() const; + + /** + * Get the standard config directories. ${XDG_CONFIG_DIRS} or { "/etc/xdg" } + * + * @return the list of config directories + */ + const List &configDirs() const noexcept; + + /** + * Get the data directories. ${XDG_DATA_DIRS} or { "/usr/local/share", "/usr/share" } + * + * @return the list of data directories + */ + const List &dataDirs() const noexcept; +}; + +#endif // !_XDG_H_