Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 25 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 26 | #include "config-file.hpp" |
| 27 | |
| 28 | #include <boost/property_tree/info_parser.hpp> |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 29 | #include <fstream> |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 30 | #include <sstream> |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 34 | ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback) |
| 35 | : m_unknownSectionCallback(unknownSectionCallback) |
| 36 | { |
| 37 | } |
| 38 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 39 | void |
| 40 | ConfigFile::throwErrorOnUnknownSection(const std::string& filename, |
| 41 | const std::string& sectionName, |
| 42 | const ConfigSection& section, |
| 43 | bool isDryRun) |
| 44 | { |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 45 | BOOST_THROW_EXCEPTION(Error("Error processing configuration file " + filename + |
| 46 | ": no module subscribed for section \"" + sectionName + "\"")); |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void |
| 50 | ConfigFile::ignoreUnknownSection(const std::string& filename, |
| 51 | const std::string& sectionName, |
| 52 | const ConfigSection& section, |
| 53 | bool isDryRun) |
| 54 | { |
| 55 | // do nothing |
| 56 | } |
| 57 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 58 | bool |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 59 | ConfigFile::parseYesNo(const ConfigSection& node, const std::string& key, |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 60 | const std::string& sectionName) |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 61 | { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 62 | auto value = node.get_value<std::string>(); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 63 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 64 | if (value == "yes") { |
| 65 | return true; |
| 66 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 67 | else if (value == "no") { |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 71 | BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" + |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 72 | key + "\" in \"" + sectionName + "\" section")); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
| 76 | ConfigFile::addSectionHandler(const std::string& sectionName, |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 77 | ConfigSectionHandler subscriber) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 78 | { |
| 79 | m_subscriptions[sectionName] = subscriber; |
| 80 | } |
| 81 | |
| 82 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 83 | ConfigFile::parse(const std::string& filename, bool isDryRun) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 84 | { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 85 | std::ifstream inputFile(filename); |
| 86 | if (!inputFile.good() || !inputFile.is_open()) { |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 87 | BOOST_THROW_EXCEPTION(Error("Failed to read configuration file " + filename)); |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 88 | } |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 89 | parse(inputFile, isDryRun, filename); |
| 90 | inputFile.close(); |
| 91 | } |
| 92 | |
| 93 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 94 | ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 95 | { |
| 96 | std::istringstream inputStream(input); |
| 97 | parse(inputStream, isDryRun, filename); |
| 98 | } |
| 99 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 100 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 101 | ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 102 | { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 103 | try { |
| 104 | boost::property_tree::read_info(input, m_global); |
| 105 | } |
| 106 | catch (const boost::property_tree::info_parser_error& error) { |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 107 | BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + filename + |
| 108 | ": " + error.message() + " on line " + to_string(error.line()))); |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 109 | } |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 110 | |
| 111 | process(isDryRun, filename); |
| 112 | } |
| 113 | |
| 114 | void |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 115 | ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename) |
| 116 | { |
| 117 | m_global = config; |
| 118 | process(isDryRun, filename); |
| 119 | } |
| 120 | |
| 121 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 122 | ConfigFile::process(bool isDryRun, const std::string& filename) const |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 123 | { |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 124 | BOOST_ASSERT(!filename.empty()); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 125 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 126 | if (m_global.begin() == m_global.end()) { |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 127 | BOOST_THROW_EXCEPTION(Error("Error processing configuration file " + filename + ": no data")); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | for (const auto& i : m_global) { |
| 131 | try { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 132 | const ConfigSectionHandler& subscriber = m_subscriptions.at(i.first); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 133 | subscriber(i.second, isDryRun, filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 134 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 135 | catch (const std::out_of_range&) { |
| 136 | m_unknownSectionCallback(filename, i.first, i.second, isDryRun); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 137 | } |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 138 | } |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 141 | } // namespace nfd |