Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | #include "config-file.hpp" |
| 9 | |
| 10 | #include <boost/property_tree/info_parser.hpp> |
| 11 | |
| 12 | namespace nfd { |
| 13 | |
| 14 | NFD_LOG_INIT("ConfigFile"); |
| 15 | |
| 16 | ConfigFile::ConfigFile() |
| 17 | { |
| 18 | |
| 19 | } |
| 20 | |
| 21 | void |
| 22 | ConfigFile::addSectionHandler(const std::string& sectionName, |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame^] | 23 | ConfigSectionHandler subscriber) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 24 | { |
| 25 | m_subscriptions[sectionName] = subscriber; |
| 26 | } |
| 27 | |
| 28 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 29 | ConfigFile::parse(const std::string& filename, bool isDryRun) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 30 | { |
| 31 | std::ifstream inputFile; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 32 | inputFile.open(filename.c_str()); |
| 33 | if (!inputFile.good() || !inputFile.is_open()) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 34 | { |
| 35 | std::string msg = "Failed to read configuration file: "; |
| 36 | msg += filename; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 37 | throw Error(msg); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 38 | } |
| 39 | parse(inputFile, isDryRun, filename); |
| 40 | inputFile.close(); |
| 41 | } |
| 42 | |
| 43 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 44 | ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 45 | { |
| 46 | std::istringstream inputStream(input); |
| 47 | parse(inputStream, isDryRun, filename); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 52 | ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 53 | { |
| 54 | try |
| 55 | { |
| 56 | boost::property_tree::read_info(input, m_global); |
| 57 | } |
| 58 | catch (const boost::property_tree::info_parser_error& error) |
| 59 | { |
| 60 | std::stringstream msg; |
| 61 | msg << "Failed to parse configuration file"; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 62 | msg << " " << filename; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 63 | msg << " " << error.message() << " line " << error.line(); |
| 64 | throw Error(msg.str()); |
| 65 | } |
| 66 | |
| 67 | process(isDryRun, filename); |
| 68 | } |
| 69 | |
| 70 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 71 | ConfigFile::process(bool isDryRun, const std::string& filename) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 72 | { |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 73 | BOOST_ASSERT(!filename.empty()); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 74 | // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):(""))); |
| 75 | |
| 76 | if (m_global.begin() == m_global.end()) |
| 77 | { |
| 78 | std::string msg = "Error processing configuration file"; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 79 | msg += ": "; |
| 80 | msg += filename; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 81 | msg += " no data"; |
| 82 | throw Error(msg); |
| 83 | } |
| 84 | |
| 85 | for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i) |
| 86 | { |
| 87 | const std::string& sectionName = i->first; |
| 88 | const ConfigSection& section = i->second; |
| 89 | |
| 90 | SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName); |
| 91 | if (subscriberIt != m_subscriptions.end()) |
| 92 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame^] | 93 | ConfigSectionHandler subscriber = subscriberIt->second; |
| 94 | subscriber(section, isDryRun, filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 95 | } |
| 96 | else |
| 97 | { |
| 98 | std::string msg = "Error processing configuration file"; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 99 | msg += " "; |
| 100 | msg += filename; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 101 | msg += " no module subscribed for section: " + sectionName; |
| 102 | throw Error(msg); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | |