28 #include <boost/property_tree/info_parser.hpp> 35 : m_unknownSectionCallback(unknownSectionCallback)
41 const std::string& sectionName,
45 BOOST_THROW_EXCEPTION(
Error(
"Error processing configuration file " + filename +
46 ": no module subscribed for section \"" + sectionName +
"\""));
51 const std::string& sectionName,
60 const std::string& sectionName)
62 auto value = node.get_value<std::string>();
67 else if (value ==
"no") {
71 BOOST_THROW_EXCEPTION(
Error(
"Invalid value \"" + value +
"\" for option \"" +
72 key +
"\" in \"" + sectionName +
"\" section"));
79 m_subscriptions[sectionName] = subscriber;
85 std::ifstream inputFile(filename);
86 if (!inputFile.good() || !inputFile.is_open()) {
87 BOOST_THROW_EXCEPTION(
Error(
"Failed to read configuration file " + filename));
89 parse(inputFile, isDryRun, filename);
96 std::istringstream inputStream(input);
97 parse(inputStream, isDryRun, filename);
104 boost::property_tree::read_info(input, m_global);
106 catch (
const boost::property_tree::info_parser_error& error) {
107 BOOST_THROW_EXCEPTION(
Error(
"Failed to parse configuration file " + filename +
108 ": " + error.message() +
" on line " + to_string(error.line())));
111 process(isDryRun, filename);
118 process(isDryRun, filename);
122 ConfigFile::process(
bool isDryRun,
const std::string& filename)
const 124 BOOST_ASSERT(!filename.empty());
126 if (m_global.begin() == m_global.end()) {
127 BOOST_THROW_EXCEPTION(
Error(
"Error processing configuration file " + filename +
": no data"));
130 for (
const auto& i : m_global) {
133 subscriber(i.second, isDryRun, filename);
135 catch (
const std::out_of_range&) {
136 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"
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)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
void parse(const std::string &filename, bool isDryRun)
boost::property_tree::ptree ConfigSection
a config file section
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)