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, |
| 23 | OnConfig subscriber) |
| 24 | { |
| 25 | m_subscriptions[sectionName] = subscriber; |
| 26 | } |
| 27 | |
| 28 | void |
| 29 | ConfigFile::parse(const char* filename, bool isDryRun) |
| 30 | { |
| 31 | std::ifstream inputFile; |
| 32 | inputFile.open(filename); |
| 33 | if (!inputFile.is_open()) |
| 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 |
| 44 | ConfigFile::parse(const std::string& input, bool isDryRun, const char* filename) |
| 45 | { |
| 46 | std::istringstream inputStream(input); |
| 47 | parse(inputStream, isDryRun, filename); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void |
| 52 | ConfigFile::parse(std::istream& input, bool isDryRun, const char* filename) |
| 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"; |
| 62 | if (filename != 0) |
| 63 | { |
| 64 | msg << " " << filename; |
| 65 | } |
| 66 | msg << " " << error.message() << " line " << error.line(); |
| 67 | throw Error(msg.str()); |
| 68 | } |
| 69 | |
| 70 | process(isDryRun, filename); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | ConfigFile::process(bool isDryRun, const char* filename) |
| 75 | { |
| 76 | // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):(""))); |
| 77 | |
| 78 | if (m_global.begin() == m_global.end()) |
| 79 | { |
| 80 | std::string msg = "Error processing configuration file"; |
| 81 | if (filename != 0) |
| 82 | { |
| 83 | msg += ": "; |
| 84 | msg += filename; |
| 85 | } |
| 86 | msg += " no data"; |
| 87 | throw Error(msg); |
| 88 | } |
| 89 | |
| 90 | for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i) |
| 91 | { |
| 92 | const std::string& sectionName = i->first; |
| 93 | const ConfigSection& section = i->second; |
| 94 | |
| 95 | SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName); |
| 96 | if (subscriberIt != m_subscriptions.end()) |
| 97 | { |
| 98 | OnConfig subscriber = subscriberIt->second; |
| 99 | subscriber(section, isDryRun); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | std::string msg = "Error processing configuration file"; |
| 104 | if (filename != 0) |
| 105 | { |
| 106 | msg += " "; |
| 107 | msg += filename; |
| 108 | } |
| 109 | msg += " no module subscribed for section: " + sectionName; |
| 110 | throw Error(msg); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | |