28 #include <boost/property_tree/info_parser.hpp>
36 : m_unknownSectionCallback(std::move(callback))
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] = std::move(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 " + std::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);
static void throwErrorOnUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
void parse(const std::string &filename, bool isDryRun)
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
static void ignoreUnknownSection(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.
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 configuration file section without a ConfigSectionHandler.
boost::property_tree::ptree ConfigSection
A configuration file section.
std::function< void(const ConfigSection §ion, bool isDryRun, const std::string &filename)> ConfigSectionHandler
Callback to process a configuration file section.