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 | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 37 | typedef function<void(const ConfigSection&, bool, const std::string&)> ConfigSectionHandler; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 38 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 39 | class ConfigFile : noncopyable |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 40 | { |
| 41 | public: |
| 42 | |
| 43 | class Error : public std::runtime_error |
| 44 | { |
| 45 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 46 | explicit |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 47 | Error(const std::string& what) |
| 48 | : std::runtime_error(what) |
| 49 | { |
| 50 | |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | ConfigFile(); |
| 55 | |
| 56 | /// \brief setup notification of configuration file sections |
| 57 | void |
| 58 | addSectionHandler(const std::string& sectionName, |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 59 | ConfigSectionHandler subscriber); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 60 | |
| 61 | |
| 62 | /** |
| 63 | * \param filename file to parse |
| 64 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 65 | * \throws ConfigFile::Error if file not found |
| 66 | * \throws ConfigFile::Error if parse error |
| 67 | */ |
| 68 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 69 | parse(const std::string& filename, bool isDryRun); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * \param input configuration (as a string) to parse |
| 73 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 74 | * \param filename optional convenience argument to provide more detailed error messages |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 75 | * \throws ConfigFile::Error if file not found |
| 76 | * \throws ConfigFile::Error if parse error |
| 77 | */ |
| 78 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 79 | parse(const std::string& input, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * \param input stream to parse |
| 83 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 84 | * \param filename optional convenience argument to provide more detailed error messages |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 85 | * \throws ConfigFile::Error if parse error |
| 86 | */ |
| 87 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 88 | parse(std::istream& input, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 89 | |
| 90 | private: |
| 91 | |
| 92 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 93 | process(bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 94 | |
| 95 | private: |
| 96 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 97 | typedef std::map<std::string, ConfigSectionHandler> SubscriptionTable; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 98 | |
| 99 | SubscriptionTable m_subscriptions; |
| 100 | |
| 101 | ConfigSection m_global; |
| 102 | }; |
| 103 | |
| 104 | } // namespace nfd |
| 105 | |
| 106 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame^] | 107 | #endif // NFD_CORE_CONFIG_FILE_HPP |