blob: 6e974ddcd2cc0e299ef3eb6868e3198e755d137a [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 DiBenedettobb75b552014-02-08 12:12:11 -070024
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070025#ifndef NFD_CORE_CONFIG_FILE_HPP
26#define NFD_CORE_CONFIG_FILE_HPP
Steve DiBenedettobb75b552014-02-08 12:12:11 -070027
28#include "common.hpp"
29
30#include <boost/property_tree/ptree.hpp>
31
32namespace nfd {
33
34typedef boost::property_tree::ptree ConfigSection;
35
36/// \brief callback for config file sections
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060037typedef function<void(const ConfigSection& /*section*/,
38 bool /*isDryRun*/,
39 const std::string& /*filename*/)> ConfigSectionHandler;
40
41/// \brief callback for config file sections without a subscribed handler
42typedef function<void(const std::string& /*filename*/,
43 const std::string& /*sectionName*/,
44 const ConfigSection& /*section*/,
45 bool /*isDryRun*/)> UnknownConfigSectionHandler;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070046
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060047class ConfigFile : noncopyable
Steve DiBenedettobb75b552014-02-08 12:12:11 -070048{
49public:
50
51 class Error : public std::runtime_error
52 {
53 public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060054 explicit
Steve DiBenedettobb75b552014-02-08 12:12:11 -070055 Error(const std::string& what)
56 : std::runtime_error(what)
57 {
58
59 }
60 };
61
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060062 ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
63
64 static void
65 throwErrorOnUnknownSection(const std::string& filename,
66 const std::string& sectionName,
67 const ConfigSection& section,
68 bool isDryRun);
69
70 static void
71 ignoreUnknownSection(const std::string& filename,
72 const std::string& sectionName,
73 const ConfigSection& section,
74 bool isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070075
76 /// \brief setup notification of configuration file sections
77 void
78 addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060079 ConfigSectionHandler subscriber);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070080
81
82 /**
83 * \param filename file to parse
84 * \param isDryRun true if performing a dry run of configuration, false otherwise
85 * \throws ConfigFile::Error if file not found
86 * \throws ConfigFile::Error if parse error
87 */
88 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060089 parse(const std::string& filename, bool isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070090
91 /**
92 * \param input configuration (as a string) to parse
93 * \param isDryRun true if performing a dry run of configuration, false otherwise
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060094 * \param filename optional convenience argument to provide more detailed error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -070095 * \throws ConfigFile::Error if file not found
96 * \throws ConfigFile::Error if parse error
97 */
98 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060099 parse(const std::string& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700100
101 /**
102 * \param input stream to parse
103 * \param isDryRun true if performing a dry run of configuration, false otherwise
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600104 * \param filename optional convenience argument to provide more detailed error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700105 * \throws ConfigFile::Error if parse error
106 */
107 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600108 parse(std::istream& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700109
Alexander Afanasyev542a6232015-01-06 13:05:50 -0800110 /**
111 * \param config ConfigSection that needs to be processed
112 * \param isDryRun true if performing a dry run of configuration, false otherwise
113 * \param filename optional convenience argument to provide more detailed error messages
114 * \throws ConfigFile::Error if parse error
115 */
116 void
117 parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
118
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700119private:
120
121 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600122 process(bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700123
124private:
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600125 UnknownConfigSectionHandler m_unknownSectionCallback;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700126
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600127 typedef std::map<std::string, ConfigSectionHandler> SubscriptionTable;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700128
129 SubscriptionTable m_subscriptions;
130
131 ConfigSection m_global;
132};
133
134} // namespace nfd
135
136
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700137#endif // NFD_CORE_CONFIG_FILE_HPP