28 #include <boost/property_tree/info_parser.hpp> 35 const std::string& sectionName,
39 std::string msg =
"Error processing configuration file ";
41 msg +=
": no module subscribed for section \"" + sectionName +
"\"";
48 const std::string& sectionName,
56 : m_unknownSectionCallback(unknownSectionCallback)
64 m_subscriptions[sectionName] = subscriber;
70 std::ifstream inputFile;
71 inputFile.open(filename.c_str());
72 if (!inputFile.good() || !inputFile.is_open())
74 std::string msg =
"Failed to read configuration file: ";
76 BOOST_THROW_EXCEPTION(
Error(msg));
78 parse(inputFile, isDryRun, filename);
85 std::istringstream inputStream(input);
86 parse(inputStream, isDryRun, filename);
95 boost::property_tree::read_info(input, m_global);
97 catch (
const boost::property_tree::info_parser_error& error)
99 std::stringstream msg;
100 msg <<
"Failed to parse configuration file";
101 msg <<
" " << filename;
102 msg <<
" " << error.message() <<
" line " << error.line();
103 BOOST_THROW_EXCEPTION(
Error(msg.str()));
106 process(isDryRun, filename);
113 process(isDryRun, filename);
117 ConfigFile::process(
bool isDryRun,
const std::string& filename)
119 BOOST_ASSERT(!filename.empty());
122 if (m_global.begin() == m_global.end())
124 std::string msg =
"Error processing configuration file: ";
127 BOOST_THROW_EXCEPTION(
Error(msg));
130 for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i)
132 const std::string& sectionName = i->first;
136 if (subscriberIt != m_subscriptions.end())
139 subscriber(section, isDryRun, filename);
143 m_unknownSectionCallback(filename, sectionName, section, isDryRun);
function< void(const ConfigSection &, bool, const std::string &)> ConfigSectionHandler
callback for config file sections
Table::const_iterator iterator
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)