Mercurial > irccd
changeset 187:7ef1aab52c1a
Irccd: update xdg module
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 31 May 2016 22:29:33 +0200 |
parents | b55435ce84be |
children | 0de84b31842b |
files | lib/irccd/CMakeSources.cmake lib/irccd/xdg.cpp lib/irccd/xdg.hpp |
diffstat | 3 files changed, 112 insertions(+), 164 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/irccd/CMakeSources.cmake Wed Jun 01 14:03:23 2016 +0200 +++ b/lib/irccd/CMakeSources.cmake Tue May 31 22:29:33 2016 +0200 @@ -153,5 +153,4 @@ if (NOT IRCCD_SYSTEM_WINDOWS) list(APPEND HEADERS ${CMAKE_CURRENT_LIST_DIR}/xdg.hpp) - list(APPEND SOURCES ${CMAKE_CURRENT_LIST_DIR}/xdg.cpp) endif ()
--- a/lib/irccd/xdg.cpp Wed Jun 01 14:03:23 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,129 +0,0 @@ -/* - * xdg.cpp -- XDG directory specifications - * - * Copyright (c) 2013, 2014, 2015 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 <stdexcept> -#include <sstream> - -#include "xdg.hpp" - -namespace irccd { - -namespace { - -bool isabsolute(const std::string &path) -{ - return path.length() > 0 && path[0] == '/'; -} - -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, ':')) - if (isabsolute(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 || !isabsolute(value)) { - 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; - - // No valid item at all? Use defaults - auto result = split(value); - - return (result.size() == 0) ? list : result; -} - -} // !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 && isabsolute(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; -} - -} // !irccd
--- a/lib/irccd/xdg.hpp Wed Jun 01 14:03:23 2016 +0200 +++ b/lib/irccd/xdg.hpp Tue May 31 22:29:33 2016 +0200 @@ -1,7 +1,7 @@ /* - * xdg.h -- XDG directory specifications + * xdg.hpp -- XDG directory specifications * - * Copyright (c) 2013, 2014, 2015 David Demelier <markand@malikania.fr> + * Copyright (c) 2013-2016 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 @@ -16,40 +16,86 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#ifndef IRCCD_XDG_H -#define IRCCD_XDG_H +#ifndef IRCCD_XDG_HPP +#define IRCCD_XDG_HPP /** * \file xdg.hpp - * \brief Read XDG standard specifications + * \brief XDG directory specifications. + * \author David Demelier <markand@malikana.fr> */ +#include <cstdlib> +#include <sstream> +#include <stdexcept> +#include <string> #include <vector> -#include <string> namespace irccd { /** * \class Xdg - * \brief XDG specifications + * \brief XDG directory specifications. * - * Read and get XDG directories. This file contains exports thingies so it can - * compiles successfully on Windows but its usage is discouraged. + * Read and get XDG directories. + * + * This file should compiles on Windows to facilitate portability but its functions must not be used. */ class Xdg { -public: - /** - * list of directories. - */ - using List = std::vector<std::string>; +private: + std::string m_configHome; + std::string m_dataHome; + std::string m_cacheHome; + std::string m_runtimeDir; + std::vector<std::string> m_configDirs; + std::vector<std::string> m_dataDirs; + + bool isabsolute(const std::string &path) const noexcept + { + return path.length() > 0 && path[0] == '/'; + } + + std::vector<std::string> split(const std::string &arg) const + { + std::stringstream iss(arg); + std::string item; + std::vector<std::string> elems; + + while (std::getline(iss, item, ':')) + if (isabsolute(item)) + elems.push_back(item); + + return elems; + } -private: - std::string m_configHome; - std::string m_dataHome; - std::string m_cacheHome; - std::string m_runtimeDir; - List m_configDirs; - List m_dataDirs; + std::string envOrHome(const std::string &var, const std::string &repl) const + { + auto value = std::getenv(var.c_str()); + + if (value == nullptr || !isabsolute(value)) { + auto home = std::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) const + { + auto value = std::getenv(var.c_str()); + + if (!value) + return list; + + // No valid item at all? Use defaults. + auto result = split(value); + + return (result.size() == 0) ? list : result; + } public: /** @@ -57,56 +103,88 @@ * * \throw std::runtime_error on failures */ - 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 = std::getenv("XDG_RUNTIME_DIR"); + if (runtime && isabsolute(runtime)) + m_runtimeDir = runtime; + } /** * Get the config directory. ${XDG_CONFIG_HOME} or ${HOME}/.config * * \return the config directory */ - const std::string &configHome() const noexcept; + inline const std::string &configHome() const noexcept + { + return m_configHome; + } /** * Get the data directory. ${XDG_DATA_HOME} or ${HOME}/.local/share * * \return the data directory */ - const std::string &dataHome() const noexcept; + inline const std::string &dataHome() const noexcept + { + return m_dataHome; + } /** * Get the cache directory. ${XDG_CACHE_HOME} or ${HOME}/.cache * * \return the cache directory */ - const std::string &cacheHome() const noexcept; + inline const std::string &cacheHome() const noexcept + { + return m_cacheHome; + } /** - * Get the runtime directory. ${XDG_RUNTIME_DIR} must be set, - * if not, it throws an exception. + * Get the runtime directory. * - * The XDG standard says that application should handle XDG_RUNTIME_DIR by - * themselves. + * There is no replacement for XDG_RUNTIME_DIR, if it is not set, an empty valus is returned and the user is + * responsible of using something else. * * \return the runtime directory - * \throw std::runtime_error on error */ - const std::string &runtimeDir() const; + inline const std::string &runtimeDir() const noexcept + { + return m_runtimeDir; + } /** * Get the standard config directories. ${XDG_CONFIG_DIRS} or { "/etc/xdg" } * * \return the list of config directories */ - const List &configDirs() const noexcept; + inline const std::vector<std::string> &configDirs() const noexcept + { + return m_configDirs; + } /** * Get the data directories. ${XDG_DATA_DIRS} or { "/usr/local/share", "/usr/share" } * * \return the list of data directories */ - const List &dataDirs() const noexcept; + inline const std::vector<std::string> &dataDirs() const noexcept + { + return m_dataDirs; + } }; } // !irccd -#endif // !IRCCD_XDG_H +#endif // !IRCCD_XDG_HPP