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 |
| 19 | typedef function<void(const ConfigSection&, bool)> OnConfig; |
| 20 | |
| 21 | class ConfigFile |
| 22 | { |
| 23 | public: |
| 24 | |
| 25 | class Error : public std::runtime_error |
| 26 | { |
| 27 | public: |
| 28 | Error(const std::string& what) |
| 29 | : std::runtime_error(what) |
| 30 | { |
| 31 | |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | ConfigFile(); |
| 36 | |
| 37 | /// \brief setup notification of configuration file sections |
| 38 | void |
| 39 | addSectionHandler(const std::string& sectionName, |
| 40 | OnConfig subscriber); |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * \param filename file to parse |
| 45 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 46 | * \throws ConfigFile::Error if file not found |
| 47 | * \throws ConfigFile::Error if parse error |
| 48 | */ |
| 49 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame^] | 50 | parse(const std::string& filename, bool isDryRun); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * \param input configuration (as a string) to parse |
| 54 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame^] | 55 | * \param filename optional convenience argument to provide more detailed error messages |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 56 | * \throws ConfigFile::Error if file not found |
| 57 | * \throws ConfigFile::Error if parse error |
| 58 | */ |
| 59 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame^] | 60 | parse(const std::string& input, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * \param input stream to parse |
| 64 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame^] | 65 | * \param filename optional convenience argument to provide more detailed error messages |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 66 | * \throws ConfigFile::Error if parse error |
| 67 | */ |
| 68 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame^] | 69 | parse(std::istream& input, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 70 | |
| 71 | private: |
| 72 | |
| 73 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame^] | 74 | process(bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | |
| 78 | typedef std::map<std::string, OnConfig> SubscriptionTable; |
| 79 | |
| 80 | SubscriptionTable m_subscriptions; |
| 81 | |
| 82 | ConfigSection m_global; |
| 83 | }; |
| 84 | |
| 85 | } // namespace nfd |
| 86 | |
| 87 | |
| 88 | #endif // NFD_MGMT_CONFIG_FILE_HPP |
| 89 | |