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 = option.second.get_value<std::string>();
70 else if (value ==
"no") {
74 BOOST_THROW_EXCEPTION(
Error(
"Invalid value \"" + value +
"\" for option \"" +
75 option.first +
"\" in \"" + sectionName +
"\" section"));
82 m_subscriptions[sectionName] = subscriber;
88 std::ifstream inputFile;
89 inputFile.open(filename.c_str());
90 if (!inputFile.good() || !inputFile.is_open())
92 std::string msg =
"Failed to read configuration file: ";
94 BOOST_THROW_EXCEPTION(
Error(msg));
96 parse(inputFile, isDryRun, filename);
103 std::istringstream inputStream(input);
104 parse(inputStream, isDryRun, filename);
112 boost::property_tree::read_info(input, m_global);
114 catch (
const boost::property_tree::info_parser_error& error)
116 std::stringstream msg;
117 msg <<
"Failed to parse configuration file";
118 msg <<
" " << filename;
119 msg <<
" " << error.message() <<
" line " << error.line();
120 BOOST_THROW_EXCEPTION(
Error(msg.str()));
123 process(isDryRun, filename);
130 process(isDryRun, filename);
134 ConfigFile::process(
bool isDryRun,
const std::string& filename)
const 136 BOOST_ASSERT(!filename.empty());
138 if (m_global.begin() == m_global.end()) {
139 std::string msg =
"Error processing configuration file: ";
142 BOOST_THROW_EXCEPTION(
Error(msg));
145 for (
const auto& i : m_global) {
148 subscriber(i.second, isDryRun, filename);
150 catch (
const std::out_of_range&) {
151 m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
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
static bool parseYesNo(const ConfigSection::value_type &option, const std::string §ionName)
parse a config option that can be either "yes" or "no"
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)