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-2017, 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 "config-file.hpp"
27 
28 #include <boost/property_tree/info_parser.hpp>
29 #include <fstream>
30 #include <sstream>
31 
32 namespace nfd {
33 
35  : m_unknownSectionCallback(unknownSectionCallback)
36 {
37 }
38 
39 void
40 ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
41  const std::string& sectionName,
42  const ConfigSection& section,
43  bool isDryRun)
44 {
45  BOOST_THROW_EXCEPTION(Error("Error processing configuration file " + filename +
46  ": no module subscribed for section \"" + sectionName + "\""));
47 }
48 
49 void
50 ConfigFile::ignoreUnknownSection(const std::string& filename,
51  const std::string& sectionName,
52  const ConfigSection& section,
53  bool isDryRun)
54 {
55  // do nothing
56 }
57 
58 bool
59 ConfigFile::parseYesNo(const ConfigSection& node, const std::string& key,
60  const std::string& sectionName)
61 {
62  auto value = node.get_value<std::string>();
63 
64  if (value == "yes") {
65  return true;
66  }
67  else if (value == "no") {
68  return false;
69  }
70 
71  BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" +
72  key + "\" in \"" + sectionName + "\" section"));
73 }
74 
75 void
76 ConfigFile::addSectionHandler(const std::string& sectionName,
77  ConfigSectionHandler subscriber)
78 {
79  m_subscriptions[sectionName] = subscriber;
80 }
81 
82 void
83 ConfigFile::parse(const std::string& filename, bool isDryRun)
84 {
85  std::ifstream inputFile(filename);
86  if (!inputFile.good() || !inputFile.is_open()) {
87  BOOST_THROW_EXCEPTION(Error("Failed to read configuration file " + filename));
88  }
89  parse(inputFile, isDryRun, filename);
90  inputFile.close();
91 }
92 
93 void
94 ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
95 {
96  std::istringstream inputStream(input);
97  parse(inputStream, isDryRun, filename);
98 }
99 
100 void
101 ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
102 {
103  try {
104  boost::property_tree::read_info(input, m_global);
105  }
106  catch (const boost::property_tree::info_parser_error& error) {
107  BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + filename +
108  ": " + error.message() + " on line " + to_string(error.line())));
109  }
110 
111  process(isDryRun, filename);
112 }
113 
114 void
115 ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename)
116 {
117  m_global = config;
118  process(isDryRun, filename);
119 }
120 
121 void
122 ConfigFile::process(bool isDryRun, const std::string& filename) const
123 {
124  BOOST_ASSERT(!filename.empty());
125 
126  if (m_global.begin() == m_global.end()) {
127  BOOST_THROW_EXCEPTION(Error("Error processing configuration file " + filename + ": no data"));
128  }
129 
130  for (const auto& i : m_global) {
131  try {
132  const ConfigSectionHandler& subscriber = m_subscriptions.at(i.first);
133  subscriber(i.second, isDryRun, filename);
134  }
135  catch (const std::out_of_range&) {
136  m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
137  }
138  }
139 }
140 
141 } // namespace nfd
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:76
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:59
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:54
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
Definition: config-file.cpp:34
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:83
boost::property_tree::ptree ConfigSection
a config file section
Definition: config-file.hpp:37
function< void(const ConfigSection &section, bool isDryRun, const std::string &filename)> ConfigSectionHandler
callback to process a config file section
Definition: config-file.hpp:47
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:40
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:50