28 #include <boost/property_tree/info_parser.hpp>
37 const std::string& sectionName,
41 std::string msg =
"Error processing configuration file ";
43 msg +=
": no module subscribed for section \"" + sectionName +
"\"";
50 const std::string& sectionName,
58 : m_unknownSectionCallback(unknownSectionCallback)
66 m_subscriptions[sectionName] = subscriber;
72 std::ifstream inputFile;
73 inputFile.open(filename.c_str());
74 if (!inputFile.good() || !inputFile.is_open())
76 std::string msg =
"Failed to read configuration file: ";
80 parse(inputFile, isDryRun, filename);
87 std::istringstream inputStream(input);
88 parse(inputStream, isDryRun, filename);
97 boost::property_tree::read_info(input, m_global);
99 catch (
const boost::property_tree::info_parser_error& error)
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());
108 process(isDryRun, filename);
115 process(isDryRun, filename);
119 ConfigFile::process(
bool isDryRun,
const std::string& filename)
121 BOOST_ASSERT(!filename.empty());
124 if (m_global.begin() == m_global.end())
126 std::string msg =
"Error processing configuration file: ";
132 for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i)
134 const std::string& sectionName = i->first;
137 SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName);
138 if (subscriberIt != m_subscriptions.end())
141 subscriber(section, isDryRun, filename);
145 m_unknownSectionCallback(filename, sectionName, section, isDryRun);
function< void(const ConfigSection &, bool, const std::string &)> ConfigSectionHandler
callback for config file sections
static void throwErrorOnUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
function< void(const std::string &, const std::string &, const ConfigSection &, bool)> UnknownConfigSectionHandler
callback for config file sections without a subscribed handler
boost::property_tree::ptree ConfigSection
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
#define NFD_LOG_INIT(name)
static void ignoreUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
void parse(const std::string &filename, bool isDryRun)