NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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::value_type& option,
63  const std::string& sectionName)
64 {
65  auto value = option.second.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  option.first + "\" 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;
89  inputFile.open(filename.c_str());
90  if (!inputFile.good() || !inputFile.is_open())
91  {
92  std::string msg = "Failed to read configuration file: ";
93  msg += filename;
94  BOOST_THROW_EXCEPTION(Error(msg));
95  }
96  parse(inputFile, isDryRun, filename);
97  inputFile.close();
98 }
99 
100 void
101 ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
102 {
103  std::istringstream inputStream(input);
104  parse(inputStream, isDryRun, filename);
105 }
106 
107 void
108 ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
109 {
110  try
111  {
112  boost::property_tree::read_info(input, m_global);
113  }
114  catch (const boost::property_tree::info_parser_error& error)
115  {
116  std::stringstream msg;
117  msg << "Failed to parse configuration file";
118  msg << " " << filename;
119  msg << " " << error.message() << " line " << error.line();
120  BOOST_THROW_EXCEPTION(Error(msg.str()));
121  }
122 
123  process(isDryRun, filename);
124 }
125 
126 void
127 ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename)
128 {
129  m_global = config;
130  process(isDryRun, filename);
131 }
132 
133 void
134 ConfigFile::process(bool isDryRun, const std::string& filename) const
135 {
136  BOOST_ASSERT(!filename.empty());
137 
138  if (m_global.begin() == m_global.end()) {
139  std::string msg = "Error processing configuration file: ";
140  msg += filename;
141  msg += " no data";
142  BOOST_THROW_EXCEPTION(Error(msg));
143  }
144 
145  for (const auto& i : m_global) {
146  try {
147  ConfigSectionHandler subscriber = m_subscriptions.at(i.first);
148  subscriber(i.second, isDryRun, filename);
149  }
150  catch (const std::out_of_range&) {
151  m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
152  }
153  }
154 }
155 
156 }
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
static bool parseYesNo(const ConfigSection::value_type &option, const std::string &sectionName)
parse a config option that can be either "yes" or "no"
Definition: config-file.cpp:62
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