29 #include <boost/property_tree/info_parser.hpp> 35 : m_unknownSectionCallback(unknownSectionCallback)
41 const std::string& sectionName,
45 std::string msg =
"Error processing configuration file ";
47 msg +=
": no module subscribed for section \"" + sectionName +
"\"";
54 const std::string& sectionName,
63 const std::string& sectionName)
65 auto value = node.get_value<std::string>();
70 else if (value ==
"no") {
74 BOOST_THROW_EXCEPTION(
Error(
"Invalid value \"" + value +
"\" for option \"" +
75 key +
"\" in \"" + sectionName +
"\" section"));
82 m_subscriptions[sectionName] = subscriber;
88 std::ifstream inputFile(filename);
89 if (!inputFile.good() || !inputFile.is_open()) {
90 BOOST_THROW_EXCEPTION(
Error(
"Failed to read configuration file: " + filename));
92 parse(inputFile, isDryRun, filename);
99 std::istringstream inputStream(input);
100 parse(inputStream, isDryRun, filename);
107 boost::property_tree::read_info(input, m_global);
109 catch (
const boost::property_tree::info_parser_error& error) {
110 std::stringstream msg;
111 msg <<
"Failed to parse configuration file";
112 msg <<
" " << filename;
113 msg <<
" " << error.message() <<
" line " << error.line();
114 BOOST_THROW_EXCEPTION(
Error(msg.str()));
117 process(isDryRun, filename);
124 process(isDryRun, filename);
128 ConfigFile::process(
bool isDryRun,
const std::string& filename)
const 130 BOOST_ASSERT(!filename.empty());
132 if (m_global.begin() == m_global.end()) {
133 std::string msg =
"Error processing configuration file: ";
136 BOOST_THROW_EXCEPTION(
Error(msg));
139 for (
const auto& i : m_global) {
142 subscriber(i.second, isDryRun, filename);
144 catch (
const std::out_of_range&) {
145 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 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)
Copyright (c) 2011-2015 Regents of the University of California.
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)
static void ignoreUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
void parse(const std::string &filename, bool isDryRun)