NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
config-file.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
25 #include "config-file.hpp"
26 #include "logger.hpp"
27 
28 #include <boost/property_tree/info_parser.hpp>
29 #include <fstream>
30 
31 namespace nfd {
32 
33 NFD_LOG_INIT("ConfigFile");
34 
35 void
36 ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
37  const std::string& sectionName,
38  const ConfigSection& section,
39  bool isDryRun)
40 {
41  std::string msg = "Error processing configuration file ";
42  msg += filename;
43  msg += ": no module subscribed for section \"" + sectionName + "\"";
44 
45  throw ConfigFile::Error(msg);
46 }
47 
48 void
49 ConfigFile::ignoreUnknownSection(const std::string& filename,
50  const std::string& sectionName,
51  const ConfigSection& section,
52  bool isDryRun)
53 {
54  // do nothing
55 }
56 
58  : m_unknownSectionCallback(unknownSectionCallback)
59 {
60 }
61 
62 void
63 ConfigFile::addSectionHandler(const std::string& sectionName,
64  ConfigSectionHandler subscriber)
65 {
66  m_subscriptions[sectionName] = subscriber;
67 }
68 
69 void
70 ConfigFile::parse(const std::string& filename, bool isDryRun)
71 {
72  std::ifstream inputFile;
73  inputFile.open(filename.c_str());
74  if (!inputFile.good() || !inputFile.is_open())
75  {
76  std::string msg = "Failed to read configuration file: ";
77  msg += filename;
78  throw Error(msg);
79  }
80  parse(inputFile, isDryRun, filename);
81  inputFile.close();
82 }
83 
84 void
85 ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
86 {
87  std::istringstream inputStream(input);
88  parse(inputStream, isDryRun, filename);
89 }
90 
91 
92 void
93 ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
94 {
95  try
96  {
97  boost::property_tree::read_info(input, m_global);
98  }
99  catch (const boost::property_tree::info_parser_error& error)
100  {
101  std::stringstream msg;
102  msg << "Failed to parse configuration file";
103  msg << " " << filename;
104  msg << " " << error.message() << " line " << error.line();
105  throw Error(msg.str());
106  }
107 
108  process(isDryRun, filename);
109 }
110 
111 void
112 ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename)
113 {
114  m_global = config;
115  process(isDryRun, filename);
116 }
117 
118 void
119 ConfigFile::process(bool isDryRun, const std::string& filename)
120 {
121  BOOST_ASSERT(!filename.empty());
122  // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):("")));
123 
124  if (m_global.begin() == m_global.end())
125  {
126  std::string msg = "Error processing configuration file: ";
127  msg += filename;
128  msg += " no data";
129  throw Error(msg);
130  }
131 
132  for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i)
133  {
134  const std::string& sectionName = i->first;
135  const ConfigSection& section = i->second;
136 
137  SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName);
138  if (subscriberIt != m_subscriptions.end())
139  {
140  ConfigSectionHandler subscriber = subscriberIt->second;
141  subscriber(section, isDryRun, filename);
142  }
143  else
144  {
145  m_unknownSectionCallback(filename, sectionName, section, isDryRun);
146  }
147  }
148 }
149 
150 }
function< void(const ConfigSection &, bool, const std::string &)> ConfigSectionHandler
callback for config file sections
Definition: config-file.hpp:39
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:36
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:63
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:45
boost::property_tree::ptree ConfigSection
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
Definition: config-file.cpp:57
#define NFD_LOG_INIT(name)
Definition: logger.hpp:33
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:49
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:70