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 |
| 50 | parse(const char* filename, bool isDryRun=false); |
| 51 | |
| 52 | /** |
| 53 | * \param input configuration (as a string) to parse |
| 54 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 55 | * \param filename optional convenience argument to provide more detailed error messages (if available) |
| 56 | * \throws ConfigFile::Error if file not found |
| 57 | * \throws ConfigFile::Error if parse error |
| 58 | */ |
| 59 | void |
| 60 | parse(const std::string& input, bool isDryRun=false, const char* filename=0); |
| 61 | |
| 62 | /** |
| 63 | * \param input stream to parse |
| 64 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 65 | * \param filename optional convenience argument to provide more detailed error messages (if available) |
| 66 | * \throws ConfigFile::Error if parse error |
| 67 | */ |
| 68 | void |
| 69 | parse(std::istream& input, bool isDryRun=false, const char* filename=0); |
| 70 | |
| 71 | private: |
| 72 | |
| 73 | void |
| 74 | process(bool isDryRun, const char* filename); |
| 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 | |