Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 25 | #ifndef NFD_CORE_CONFIG_FILE_HPP |
| 26 | #define NFD_CORE_CONFIG_FILE_HPP |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 27 | |
| 28 | #include "common.hpp" |
| 29 | |
| 30 | #include <boost/property_tree/ptree.hpp> |
| 31 | |
| 32 | namespace nfd { |
| 33 | |
| 34 | typedef boost::property_tree::ptree ConfigSection; |
| 35 | |
| 36 | /// \brief callback for config file sections |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 37 | typedef function<void(const ConfigSection& /*section*/, |
| 38 | bool /*isDryRun*/, |
| 39 | const std::string& /*filename*/)> ConfigSectionHandler; |
| 40 | |
| 41 | /// \brief callback for config file sections without a subscribed handler |
| 42 | typedef function<void(const std::string& /*filename*/, |
| 43 | const std::string& /*sectionName*/, |
| 44 | const ConfigSection& /*section*/, |
| 45 | bool /*isDryRun*/)> UnknownConfigSectionHandler; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 46 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 47 | class ConfigFile : noncopyable |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 48 | { |
| 49 | public: |
| 50 | |
| 51 | class Error : public std::runtime_error |
| 52 | { |
| 53 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 54 | explicit |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 55 | Error(const std::string& what) |
| 56 | : std::runtime_error(what) |
| 57 | { |
| 58 | |
| 59 | } |
| 60 | }; |
| 61 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 62 | ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection); |
| 63 | |
| 64 | static void |
| 65 | throwErrorOnUnknownSection(const std::string& filename, |
| 66 | const std::string& sectionName, |
| 67 | const ConfigSection& section, |
| 68 | bool isDryRun); |
| 69 | |
| 70 | static void |
| 71 | ignoreUnknownSection(const std::string& filename, |
| 72 | const std::string& sectionName, |
| 73 | const ConfigSection& section, |
| 74 | bool isDryRun); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 75 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 76 | /** @brief parse a config option that can be either "yes" or "no" |
| 77 | * |
| 78 | * @throw ConfigFile::Error value is neither "yes" nor "no" |
| 79 | * @return true if "yes", false if "no" |
| 80 | */ |
| 81 | static bool |
| 82 | parseYesNo(const ConfigSection::const_iterator& i, |
| 83 | const std::string& optionName, |
| 84 | const std::string& sectionName); |
| 85 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 86 | /// \brief setup notification of configuration file sections |
| 87 | void |
| 88 | addSectionHandler(const std::string& sectionName, |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 89 | ConfigSectionHandler subscriber); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 90 | |
| 91 | |
| 92 | /** |
| 93 | * \param filename file to parse |
| 94 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 95 | * \throws ConfigFile::Error if file not found |
| 96 | * \throws ConfigFile::Error if parse error |
| 97 | */ |
| 98 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 99 | parse(const std::string& filename, bool isDryRun); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * \param input configuration (as a string) to parse |
| 103 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 104 | * \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] | 105 | * \throws ConfigFile::Error if file not found |
| 106 | * \throws ConfigFile::Error if parse error |
| 107 | */ |
| 108 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 109 | parse(const std::string& input, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * \param input stream to parse |
| 113 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 114 | * \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] | 115 | * \throws ConfigFile::Error if parse error |
| 116 | */ |
| 117 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 118 | parse(std::istream& input, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 120 | /** |
| 121 | * \param config ConfigSection that needs to be processed |
| 122 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 123 | * \param filename logical filename of the config file, can appear in error messages |
| 124 | * \throws ConfigFile::Error if parse error |
| 125 | */ |
| 126 | void |
| 127 | parse(const ConfigSection& config, bool isDryRun, const std::string& filename); |
| 128 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 129 | private: |
| 130 | |
| 131 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 132 | process(bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 133 | |
| 134 | private: |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 135 | UnknownConfigSectionHandler m_unknownSectionCallback; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 136 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 137 | typedef std::map<std::string, ConfigSectionHandler> SubscriptionTable; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 138 | |
| 139 | SubscriptionTable m_subscriptions; |
| 140 | |
| 141 | ConfigSection m_global; |
| 142 | }; |
| 143 | |
| 144 | } // namespace nfd |
| 145 | |
| 146 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 147 | #endif // NFD_CORE_CONFIG_FILE_HPP |