config-file.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, 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  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include "common/config-file.hpp"
27 
28 #include <boost/property_tree/info_parser.hpp>
29 
30 #include <fstream>
31 #include <sstream>
32 
33 namespace nfd {
34 
36  : m_unknownSectionCallback(unknownSectionCallback)
37 {
38 }
39 
40 void
41 ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
42  const std::string& sectionName,
43  const ConfigSection& section,
44  bool isDryRun)
45 {
46  NDN_THROW(Error("Error processing configuration file " + filename +
47  ": no module subscribed for section '" + sectionName + "'"));
48 }
49 
50 void
51 ConfigFile::ignoreUnknownSection(const std::string& filename,
52  const std::string& sectionName,
53  const ConfigSection& section,
54  bool isDryRun)
55 {
56  // do nothing
57 }
58 
59 bool
60 ConfigFile::parseYesNo(const ConfigSection& node, const std::string& key,
61  const std::string& sectionName)
62 {
63  auto value = node.get_value<std::string>();
64 
65  if (value == "yes") {
66  return true;
67  }
68  else if (value == "no") {
69  return false;
70  }
71 
72  NDN_THROW(Error("Invalid value '" + value + "' for option '" +
73  key + "' in section '" + sectionName + "'"));
74 }
75 
76 void
77 ConfigFile::addSectionHandler(const std::string& sectionName,
78  ConfigSectionHandler subscriber)
79 {
80  m_subscriptions[sectionName] = subscriber;
81 }
82 
83 void
84 ConfigFile::parse(const std::string& filename, bool isDryRun)
85 {
86  std::ifstream inputFile(filename);
87  if (!inputFile.good() || !inputFile.is_open()) {
88  NDN_THROW(Error("Failed to read configuration file " + filename));
89  }
90  parse(inputFile, isDryRun, filename);
91  inputFile.close();
92 }
93 
94 void
95 ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
96 {
97  std::istringstream inputStream(input);
98  parse(inputStream, isDryRun, filename);
99 }
100 
101 void
102 ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
103 {
104  try {
105  boost::property_tree::read_info(input, m_global);
106  }
107  catch (const boost::property_tree::info_parser_error& error) {
108  NDN_THROW(Error("Failed to parse configuration file " + filename +
109  ": " + error.message() + " on line " + to_string(error.line())));
110  }
111 
112  process(isDryRun, filename);
113 }
114 
115 void
116 ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename)
117 {
118  m_global = config;
119  process(isDryRun, filename);
120 }
121 
122 void
123 ConfigFile::process(bool isDryRun, const std::string& filename) const
124 {
125  BOOST_ASSERT(!filename.empty());
126 
127  for (const auto& i : m_global) {
128  try {
129  const ConfigSectionHandler& subscriber = m_subscriptions.at(i.first);
130  subscriber(i.second, isDryRun, filename);
131  }
132  catch (const std::out_of_range&) {
133  m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
134  }
135  }
136 }
137 
138 } // namespace nfd
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:77
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string &sectionName)
parse a config option that can be either "yes" or "no"
Definition: config-file.cpp:60
std::function< void(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)> UnknownConfigSectionHandler
callback to process a config file section without a ConfigSectionHandler
Definition: config-file.hpp:53
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
Definition: config-file.cpp:35
boost::property_tree::ptree ConfigSection
a config file section
Definition: config-file.hpp:37
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:84
std::function< void(const ConfigSection &section, bool isDryRun, const std::string &filename)> ConfigSectionHandler
callback to process a config file section
Definition: config-file.hpp:46
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:41
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:51