NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
config-file.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "config-file.hpp"
27 #include "logger.hpp"
28 
29 #include <boost/property_tree/info_parser.hpp>
30 #include <fstream>
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  std::string msg = "Error processing configuration file ";
46  msg += filename;
47  msg += ": no module subscribed for section \"" + sectionName + "\"";
48 
49  BOOST_THROW_EXCEPTION(ConfigFile::Error(msg));
50 }
51 
52 void
53 ConfigFile::ignoreUnknownSection(const std::string& filename,
54  const std::string& sectionName,
55  const ConfigSection& section,
56  bool isDryRun)
57 {
58  // do nothing
59 }
60 
61 bool
62 ConfigFile::parseYesNo(const ConfigSection& node, const std::string& key,
63  const std::string& sectionName)
64 {
65  auto value = node.get_value<std::string>();
66 
67  if (value == "yes") {
68  return true;
69  }
70  else if (value == "no") {
71  return false;
72  }
73 
74  BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" +
75  key + "\" in \"" + sectionName + "\" section"));
76 }
77 
78 void
79 ConfigFile::addSectionHandler(const std::string& sectionName,
80  ConfigSectionHandler subscriber)
81 {
82  m_subscriptions[sectionName] = subscriber;
83 }
84 
85 void
86 ConfigFile::parse(const std::string& filename, bool isDryRun)
87 {
88  std::ifstream inputFile(filename);
89  if (!inputFile.good() || !inputFile.is_open()) {
90  BOOST_THROW_EXCEPTION(Error("Failed to read configuration file: " + filename));
91  }
92  parse(inputFile, isDryRun, filename);
93  inputFile.close();
94 }
95 
96 void
97 ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
98 {
99  std::istringstream inputStream(input);
100  parse(inputStream, isDryRun, filename);
101 }
102 
103 void
104 ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
105 {
106  try {
107  boost::property_tree::read_info(input, m_global);
108  }
109  catch (const boost::property_tree::info_parser_error& error) {
110  std::stringstream msg;
111  msg << "Failed to parse configuration file";
112  msg << " " << filename;
113  msg << " " << error.message() << " line " << error.line();
114  BOOST_THROW_EXCEPTION(Error(msg.str()));
115  }
116 
117  process(isDryRun, filename);
118 }
119 
120 void
121 ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename)
122 {
123  m_global = config;
124  process(isDryRun, filename);
125 }
126 
127 void
128 ConfigFile::process(bool isDryRun, const std::string& filename) const
129 {
130  BOOST_ASSERT(!filename.empty());
131 
132  if (m_global.begin() == m_global.end()) {
133  std::string msg = "Error processing configuration file: ";
134  msg += filename;
135  msg += " no data";
136  BOOST_THROW_EXCEPTION(Error(msg));
137  }
138 
139  for (const auto& i : m_global) {
140  try {
141  const ConfigSectionHandler& subscriber = m_subscriptions.at(i.first);
142  subscriber(i.second, isDryRun, filename);
143  }
144  catch (const std::out_of_range&) {
145  m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
146  }
147  }
148 }
149 
150 } // namespace nfd
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:62
function< void(const ConfigSection &, bool, const std::string &)> ConfigSectionHandler
callback for config file sections
Definition: config-file.hpp:40
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:40
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:79
function< void(const std::string &, const std::string &, const ConfigSection &, bool)> UnknownConfigSectionHandler
callback for config file sections without a subscribed handler
Definition: config-file.hpp:46
boost::property_tree::ptree ConfigSection
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
Definition: config-file.cpp:34
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:53
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:86