28 #include <boost/property_tree/info_parser.hpp> 36 : m_unknownSectionCallback(unknownSectionCallback)
42 const std::string& sectionName,
46 NDN_THROW(
Error(
"Error processing configuration file " + filename +
47 ": no module subscribed for section '" + sectionName +
"'"));
52 const std::string& sectionName,
61 const std::string& sectionName)
63 auto value = node.get_value<std::string>();
68 else if (value ==
"no") {
72 NDN_THROW(
Error(
"Invalid value '" + value +
"' for option '" +
73 key +
"' in section '" + sectionName +
"'"));
80 m_subscriptions[sectionName] = subscriber;
86 std::ifstream inputFile(filename);
87 if (!inputFile.good() || !inputFile.is_open()) {
88 NDN_THROW(
Error(
"Failed to read configuration file " + filename));
90 parse(inputFile, isDryRun, filename);
97 std::istringstream inputStream(input);
98 parse(inputStream, isDryRun, filename);
105 boost::property_tree::read_info(input, m_global);
107 catch (
const boost::property_tree::info_parser_error& error) {
108 NDN_THROW(
Error(
"Failed to parse configuration file " + filename +
109 ": " + error.message() +
" on line " + to_string(error.line())));
112 process(isDryRun, filename);
119 process(isDryRun, filename);
123 ConfigFile::process(
bool isDryRun,
const std::string& filename)
const 125 BOOST_ASSERT(!filename.empty());
127 for (
const auto& i : m_global) {
130 subscriber(i.second, isDryRun, filename);
132 catch (
const std::out_of_range&) {
133 m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string §ionName)
parse a config option that can be either "yes" or "no"
std::function< void(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)> UnknownConfigSectionHandler
callback to process a config file section without a ConfigSectionHandler
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
boost::property_tree::ptree ConfigSection
a config file section
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
void parse(const std::string &filename, bool isDryRun)
std::function< void(const ConfigSection §ion, bool isDryRun, const std::string &filename)> ConfigSectionHandler
callback to process a config file section
static void throwErrorOnUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
static void ignoreUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)