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);
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
static void throwErrorOnUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
Copyright (c) 2011-2015 Regents of the University of California.
void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
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
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
static void ignoreUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
std::string to_string(const V &v)
void parse(const std::string &filename, bool isDryRun)