blob: 15eca05ed18f84b397a899739505b76b8d65bb72 [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- 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
14namespace nfd {
15
16typedef boost::property_tree::ptree ConfigSection;
17
18/// \brief callback for config file sections
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060019typedef function<void(const ConfigSection&, bool, const std::string&)> ConfigSectionHandler;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070020
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060021class ConfigFile : noncopyable
Steve DiBenedettobb75b552014-02-08 12:12:11 -070022{
23public:
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,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060040 ConfigSectionHandler subscriber);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070041
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 DiBenedetto84da5bf2014-03-11 14:51:29 -060050 parse(const std::string& filename, bool isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070051
52 /**
53 * \param input configuration (as a string) to parse
54 * \param isDryRun true if performing a dry run of configuration, false otherwise
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060055 * \param filename optional convenience argument to provide more detailed error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -070056 * \throws ConfigFile::Error if file not found
57 * \throws ConfigFile::Error if parse error
58 */
59 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060060 parse(const std::string& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070061
62 /**
63 * \param input stream to parse
64 * \param isDryRun true if performing a dry run of configuration, false otherwise
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060065 * \param filename optional convenience argument to provide more detailed error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -070066 * \throws ConfigFile::Error if parse error
67 */
68 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060069 parse(std::istream& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070070
71private:
72
73 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060074 process(bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070075
76private:
77
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060078 typedef std::map<std::string, ConfigSectionHandler> SubscriptionTable;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070079
80 SubscriptionTable m_subscriptions;
81
82 ConfigSection m_global;
83};
84
85} // namespace nfd
86
87
88#endif // NFD_MGMT_CONFIG_FILE_HPP
89