Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NFD_MGMT_CONFIG_FILE_HPP |
| 8 | #define NFD_MGMT_CONFIG_FILE_HPP |
| 9 | |
| 10 | #include "common.hpp" |
| 11 | |
| 12 | #include <boost/property_tree/ptree.hpp> |
| 13 | |
| 14 | namespace nfd { |
| 15 | |
| 16 | typedef boost::property_tree::ptree ConfigSection; |
| 17 | |
| 18 | /// \brief callback for config file sections |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 19 | typedef function<void(const ConfigSection&, bool, const std::string&)> ConfigSectionHandler; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 20 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 21 | class ConfigFile : noncopyable |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 22 | { |
| 23 | public: |
| 24 | |
| 25 | class Error : public std::runtime_error |
| 26 | { |
| 27 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 28 | explicit |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 29 | Error(const std::string& what) |
| 30 | : std::runtime_error(what) |
| 31 | { |
| 32 | |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | ConfigFile(); |
| 37 | |
| 38 | /// \brief setup notification of configuration file sections |
| 39 | void |
| 40 | addSectionHandler(const std::string& sectionName, |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 41 | ConfigSectionHandler subscriber); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 42 | |
| 43 | |
| 44 | /** |
| 45 | * \param filename file to parse |
| 46 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 47 | * \throws ConfigFile::Error if file not found |
| 48 | * \throws ConfigFile::Error if parse error |
| 49 | */ |
| 50 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 51 | parse(const std::string& filename, bool isDryRun); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * \param input configuration (as a string) to parse |
| 55 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 56 | * \param filename optional convenience argument to provide more detailed error messages |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 57 | * \throws ConfigFile::Error if file not found |
| 58 | * \throws ConfigFile::Error if parse error |
| 59 | */ |
| 60 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 61 | parse(const std::string& input, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * \param input stream to parse |
| 65 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 66 | * \param filename optional convenience argument to provide more detailed error messages |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 67 | * \throws ConfigFile::Error if parse error |
| 68 | */ |
| 69 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 70 | parse(std::istream& input, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | |
| 74 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 75 | process(bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 76 | |
| 77 | private: |
| 78 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 79 | typedef std::map<std::string, ConfigSectionHandler> SubscriptionTable; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 80 | |
| 81 | SubscriptionTable m_subscriptions; |
| 82 | |
| 83 | ConfigSection m_global; |
| 84 | }; |
| 85 | |
| 86 | } // namespace nfd |
| 87 | |
| 88 | |
| 89 | #endif // NFD_MGMT_CONFIG_FILE_HPP |