# HG changeset patch # User David Demelier # Date 1512673630 -3600 # Node ID 24959a94de966d821e53374f39e014a39e0eae9e # Parent 48c6a1167245a8e490500f32f5c51cee1e90dd43 Irccdctl: use config class, closes #749 diff -r 48c6a1167245 -r 24959a94de96 irccd/main.cpp --- a/irccd/main.cpp Wed Dec 06 21:47:11 2017 +0100 +++ b/irccd/main.cpp Thu Dec 07 20:07:10 2017 +0100 @@ -33,12 +33,12 @@ #include #include +#include #include #include #include #include -#include #include #include #include @@ -160,7 +160,12 @@ if (it != result.end() || (it = result.find("--config")) != result.end()) return config(it->second); - return config::find("irccd.conf"); + auto cfg = config::find("irccd.conf"); + + if (!cfg) + throw std::runtime_error("no configuration file could be found"); + + return *cfg; } } // !namespace diff -r 48c6a1167245 -r 24959a94de96 irccdctl/main.cpp --- a/irccdctl/main.cpp Wed Dec 06 21:47:11 2017 +0100 +++ b/irccdctl/main.cpp Thu Dec 07 20:07:10 2017 +0100 @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include #include @@ -257,28 +257,23 @@ return alias; } -void read(const std::string& path) +void read(const config& cfg) { - try { - ini::document doc = ini::read_file(path); - ini::document::const_iterator it; + ini::document::const_iterator it; - if (!ctl && (it = doc.find("connect")) != doc.end()) - read_connect(*it); - if ((it = doc.find("general")) != doc.end()) - read_general(*it); + if (!ctl && (it = cfg.doc().find("connect")) != cfg.doc().end()) + read_connect(*it); + if ((it = cfg.doc().find("general")) != cfg.doc().end()) + read_general(*it); - // [alias.*] sections. - for (const auto& sc : doc) { - if (sc.key().compare(0, 6, "alias.") == 0) { - auto name = sc.key().substr(6); - auto alias = read_alias(sc, name); + // [alias.*] sections. + for (const auto& sc : cfg.doc()) { + if (sc.key().compare(0, 6, "alias.") == 0) { + auto name = sc.key().substr(6); + auto alias = read_alias(sc, name); - aliases.emplace(std::move(name), std::move(alias)); - } + aliases.emplace(std::move(name), std::move(alias)); } - } catch (const std::exception &ex) { - std::cerr << path << ": " << ex.what() << std::endl; } } @@ -573,14 +568,8 @@ if (it != result.end() || (it = result.find("--config")) != result.end()) irccd::ctl::read(it->second); else { - for (const auto& path : irccd::sys::config_filenames("irccdctl.conf")) { - boost::system::error_code ec; - - if (boost::filesystem::exists(path, ec) && !ec) { - irccd::ctl::read(path); - break; - } - } + if (auto conf = irccd::config::find("irccdctl.conf")) + irccd::ctl::read(*conf); } } catch (const std::exception& ex) { std::cerr << "abort: " << ex.what() << std::endl; diff -r 48c6a1167245 -r 24959a94de96 libcommon/CMakeLists.txt --- a/libcommon/CMakeLists.txt Wed Dec 06 21:47:11 2017 +0100 +++ b/libcommon/CMakeLists.txt Thu Dec 07 20:07:10 2017 +0100 @@ -22,6 +22,7 @@ set( HEADERS + ${libcommon_SOURCE_DIR}/irccd/config.hpp ${libcommon_SOURCE_DIR}/irccd/fs_util.hpp ${libcommon_SOURCE_DIR}/irccd/ini.hpp ${libcommon_SOURCE_DIR}/irccd/json_util.hpp @@ -35,6 +36,7 @@ set( SOURCES + ${libcommon_SOURCE_DIR}/irccd/config.cpp ${libcommon_SOURCE_DIR}/irccd/ini.cpp ${libcommon_SOURCE_DIR}/irccd/json_util.cpp ${libcommon_SOURCE_DIR}/irccd/options.cpp diff -r 48c6a1167245 -r 24959a94de96 libcommon/irccd/config.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libcommon/irccd/config.cpp Thu Dec 07 20:07:10 2017 +0100 @@ -0,0 +1,39 @@ +/* + * config.cpp -- irccd configuration loader + * + * Copyright (c) 2013-2017 David Demelier + * + * 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 + +#include + +#include "config.hpp" + +namespace irccd { + +boost::optional config::find(const std::string& name) +{ + for (const auto& path : sys::config_filenames(name)) { + boost::system::error_code ec; + + if (boost::filesystem::exists(path, ec) && !ec) + return config(path); + } + + return boost::optional(); +} + +} // !irccd diff -r 48c6a1167245 -r 24959a94de96 libcommon/irccd/config.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libcommon/irccd/config.hpp Thu Dec 07 20:07:10 2017 +0100 @@ -0,0 +1,108 @@ +/* + * config.hpp -- irccd configuration loader + * + * Copyright (c) 2013-2017 David Demelier + * + * 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 IRCCD_CONFIG_HPP +#define IRCCD_CONFIG_HPP + +/** + * \file config.hpp + * \brief Read .ini configuration file for irccd + */ + +#include + +#include "ini.hpp" + +namespace irccd { + +/** + * \brief Read .ini configuration file for irccd + */ +class config { +private: + std::string path_; + ini::document document_; + +public: + /** + * Search the configuration file into the standard defined paths. + * + * \param name the file name + * \return the config or empty if not found + */ + static boost::optional find(const std::string& name); + + /** + * Load the configuration from the specified path. + * + * \param path the path + */ + inline config(std::string path = "") + : path_(std::move(path)) + { + if (!path_.empty()) + document_ = ini::read_file(path_); + } + + /** + * Get the underlying document. + * + * \return the document + */ + inline const ini::document& doc() const noexcept + { + return document_; + } + + /** + * Get the path to the configuration file. + * + * \return the path + */ + inline const std::string& path() const noexcept + { + return path_; + } + + /** + * Convenience function to access a section. + * + * \param section the section name + * \return the section or empty one + */ + inline ini::section section(const std::string& section) const noexcept + { + return document_.get(section); + } + + /** + * Convenience function to access an ini value. + * + * \param section the section name + * \param option the option name + * \return the value or empty string + */ + inline std::string value(const std::string& section, const std::string& option) const noexcept + { + return document_.get(section).get(option).value(); + } +}; + +} // !irccd + +#endif // !IRCCD_CONFIG_HPP diff -r 48c6a1167245 -r 24959a94de96 libirccd/CMakeLists.txt --- a/libirccd/CMakeLists.txt Wed Dec 06 21:47:11 2017 +0100 +++ b/libirccd/CMakeLists.txt Thu Dec 07 20:07:10 2017 +0100 @@ -26,7 +26,6 @@ ${libirccd_SOURCE_DIR}/irccd/daemon/basic_transport_server.hpp ${libirccd_SOURCE_DIR}/irccd/daemon/command.hpp ${libirccd_SOURCE_DIR}/irccd/daemon/command_service.hpp - ${libirccd_SOURCE_DIR}/irccd/daemon/config.hpp ${libirccd_SOURCE_DIR}/irccd/daemon/dynlib_plugin.hpp ${libirccd_SOURCE_DIR}/irccd/daemon/ip_transport_server.hpp ${libirccd_SOURCE_DIR}/irccd/daemon/irccd.hpp @@ -49,7 +48,6 @@ SOURCES ${libirccd_SOURCE_DIR}/irccd/daemon/command.cpp ${libirccd_SOURCE_DIR}/irccd/daemon/command_service.cpp - ${libirccd_SOURCE_DIR}/irccd/daemon/config.cpp ${libirccd_SOURCE_DIR}/irccd/daemon/dynlib_plugin.cpp ${libirccd_SOURCE_DIR}/irccd/daemon/irccd.cpp ${libirccd_SOURCE_DIR}/irccd/daemon/irc.cpp diff -r 48c6a1167245 -r 24959a94de96 libirccd/irccd/daemon/config.cpp --- a/libirccd/irccd/daemon/config.cpp Wed Dec 06 21:47:11 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ -/* - * config.cpp -- irccd configuration loader - * - * Copyright (c) 2013-2017 David Demelier - * - * 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 - -#include - -#include - -#include "config.hpp" - -namespace irccd { - -config config::find(const std::string& name) -{ - for (const auto& path : sys::config_filenames(name)) { - boost::system::error_code ec; - - if (boost::filesystem::exists(path, ec) && !ec) - return config(path); - } - - throw std::runtime_error("no configuration file found"); -} - -} // !irccd diff -r 48c6a1167245 -r 24959a94de96 libirccd/irccd/daemon/config.hpp --- a/libirccd/irccd/daemon/config.hpp Wed Dec 06 21:47:11 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,107 +0,0 @@ -/* - * config.hpp -- irccd configuration loader - * - * Copyright (c) 2013-2017 David Demelier - * - * 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 IRCCD_CONFIG_HPP -#define IRCCD_CONFIG_HPP - -/** - * \file config.hpp - * \brief Read .ini configuration file for irccd - */ - -#include "ini.hpp" - -namespace irccd { - -/** - * \brief Read .ini configuration file for irccd - */ -class config { -private: - std::string path_; - ini::document document_; - -public: - /** - * Search the configuration file into the standard defined paths. - * - * \param name the file name - * \return the config - * \throw std::exception on errors or if no config could be found - */ - static config find(const std::string& name); - - /** - * Load the configuration from the specified path. - * - * \param path the path - */ - inline config(std::string path = "") - : path_(std::move(path)) - { - if (!path_.empty()) - document_ = ini::read_file(path_); - } - - /** - * Get the underlying document. - * - * \return the document - */ - inline const ini::document& doc() const noexcept - { - return document_; - } - - /** - * Get the path to the configuration file. - * - * \return the path - */ - inline const std::string& path() const noexcept - { - return path_; - } - - /** - * Convenience function to access a section. - * - * \param section the section name - * \return the section or empty one - */ - inline ini::section section(const std::string& section) const noexcept - { - return document_.get(section); - } - - /** - * Convenience function to access an ini value. - * - * \param section the section name - * \param option the option name - * \return the value or empty string - */ - inline std::string value(const std::string& section, const std::string& option) const noexcept - { - return document_.get(section).get(option).value(); - } -}; - -} // !irccd - -#endif // !IRCCD_CONFIG_HPP