Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_CORE_CONFIG_FILE_HPP |
| 27 | #define NFD_CORE_CONFIG_FILE_HPP |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 28 | |
| 29 | #include "common.hpp" |
| 30 | |
| 31 | #include <boost/property_tree/ptree.hpp> |
| 32 | |
| 33 | namespace nfd { |
| 34 | |
| 35 | typedef boost::property_tree::ptree ConfigSection; |
| 36 | |
| 37 | /// \brief callback for config file sections |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 38 | typedef function<void(const ConfigSection& /*section*/, |
| 39 | bool /*isDryRun*/, |
| 40 | const std::string& /*filename*/)> ConfigSectionHandler; |
| 41 | |
| 42 | /// \brief callback for config file sections without a subscribed handler |
| 43 | typedef function<void(const std::string& /*filename*/, |
| 44 | const std::string& /*sectionName*/, |
| 45 | const ConfigSection& /*section*/, |
| 46 | bool /*isDryRun*/)> UnknownConfigSectionHandler; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 47 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame^] | 48 | /** \brief configuration file parsing utility |
| 49 | */ |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 50 | class ConfigFile : noncopyable |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 51 | { |
| 52 | public: |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 53 | class Error : public std::runtime_error |
| 54 | { |
| 55 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 56 | explicit |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 57 | Error(const std::string& what) |
| 58 | : std::runtime_error(what) |
| 59 | { |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 60 | } |
| 61 | }; |
| 62 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 63 | explicit |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 64 | ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection); |
| 65 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame^] | 66 | public: // unknown section handlers |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 67 | static void |
| 68 | throwErrorOnUnknownSection(const std::string& filename, |
| 69 | const std::string& sectionName, |
| 70 | const ConfigSection& section, |
| 71 | bool isDryRun); |
| 72 | |
| 73 | static void |
| 74 | ignoreUnknownSection(const std::string& filename, |
| 75 | const std::string& sectionName, |
| 76 | const ConfigSection& section, |
| 77 | bool isDryRun); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame^] | 79 | public: // parse helpers |
| 80 | /** \brief parse a config option that can be either "yes" or "no" |
| 81 | * \retval true "yes" |
| 82 | * \retval false "no" |
| 83 | * \throw Error the value is neither "yes" nor "no" |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 84 | */ |
| 85 | static bool |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame^] | 86 | parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName); |
| 87 | |
| 88 | static bool |
| 89 | parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName) |
| 90 | { |
| 91 | return parseYesNo(option.second, option.first, sectionName); |
| 92 | } |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 93 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 94 | /** |
| 95 | * \brief parse a numeric (integral or floating point) config option |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame^] | 96 | * \tparam T an arithmetic type |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 97 | * |
| 98 | * \return the numeric value of the parsed option |
| 99 | * \throw Error the value cannot be converted to the specified type |
| 100 | */ |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame^] | 101 | template<typename T> |
| 102 | static T |
| 103 | parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName) |
| 104 | { |
| 105 | static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type"); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 106 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame^] | 107 | boost::optional<T> value = node.get_value_optional<T>(); |
| 108 | if (value) { |
| 109 | return *value; |
| 110 | } |
| 111 | BOOST_THROW_EXCEPTION(Error("Invalid value \"" + node.get_value<std::string>() + |
| 112 | "\" for option \"" + key + "\" in \"" + sectionName + "\" section")); |
| 113 | } |
| 114 | |
| 115 | template <typename T> |
| 116 | static T |
| 117 | parseNumber(const ConfigSection::value_type& option, const std::string& sectionName) |
| 118 | { |
| 119 | return parseNumber<T>(option.second, option.first, sectionName); |
| 120 | } |
| 121 | |
| 122 | public: // setup and parsing |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 123 | /// \brief setup notification of configuration file sections |
| 124 | void |
| 125 | addSectionHandler(const std::string& sectionName, |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 126 | ConfigSectionHandler subscriber); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 127 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 128 | /** |
| 129 | * \param filename file to parse |
| 130 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 131 | * \throws ConfigFile::Error if file not found |
| 132 | * \throws ConfigFile::Error if parse error |
| 133 | */ |
| 134 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 135 | parse(const std::string& filename, bool isDryRun); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * \param input configuration (as a string) to parse |
| 139 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 140 | * \param filename logical filename of the config file, can appear in error messages |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 141 | * \throws ConfigFile::Error if file not found |
| 142 | * \throws ConfigFile::Error if parse error |
| 143 | */ |
| 144 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 145 | parse(const std::string& input, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * \param input stream to parse |
| 149 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 150 | * \param filename logical filename of the config file, can appear in error messages |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 151 | * \throws ConfigFile::Error if parse error |
| 152 | */ |
| 153 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 154 | parse(std::istream& input, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 155 | |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 156 | /** |
| 157 | * \param config ConfigSection that needs to be processed |
| 158 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 159 | * \param filename logical filename of the config file, can appear in error messages |
| 160 | * \throws ConfigFile::Error if parse error |
| 161 | */ |
| 162 | void |
| 163 | parse(const ConfigSection& config, bool isDryRun, const std::string& filename); |
| 164 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 165 | private: |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 166 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 167 | process(bool isDryRun, const std::string& filename) const; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 168 | |
| 169 | private: |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 170 | UnknownConfigSectionHandler m_unknownSectionCallback; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 171 | std::map<std::string, ConfigSectionHandler> m_subscriptions; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 172 | ConfigSection m_global; |
| 173 | }; |
| 174 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 175 | } // namespace nfd |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 176 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 177 | #endif // NFD_CORE_CONFIG_FILE_HPP |