Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, Regents of the University of California, |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [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. |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 12 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 13 | * |
| 14 | * NDNS 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 | * NDNS 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 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 26 | #include "config-file.hpp" |
| 27 | #include "logger.hpp" |
| 28 | |
| 29 | #include <boost/property_tree/info_parser.hpp> |
| 30 | #include <fstream> |
| 31 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 32 | namespace ndn { |
| 33 | namespace ndns { |
| 34 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 35 | ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback) |
| 36 | : m_unknownSectionCallback(unknownSectionCallback) |
| 37 | { |
| 38 | } |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 39 | |
| 40 | void |
| 41 | ConfigFile::throwErrorOnUnknownSection(const std::string& filename, |
| 42 | const std::string& sectionName, |
| 43 | const ConfigSection& section, |
| 44 | bool isDryRun) |
| 45 | { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 46 | NDN_THROW(Error("Error processing configuration file " + filename + |
| 47 | ": no module subscribed for section \"" + sectionName + "\"")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
| 51 | ConfigFile::ignoreUnknownSection(const std::string& filename, |
| 52 | const std::string& sectionName, |
| 53 | const ConfigSection& section, |
| 54 | bool isDryRun) |
| 55 | { |
| 56 | // do nothing |
| 57 | } |
| 58 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 59 | bool |
| 60 | ConfigFile::parseYesNo(const ConfigSection& node, const std::string& key, |
| 61 | const std::string& sectionName) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 62 | { |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 63 | auto value = node.get_value<std::string>(); |
| 64 | |
| 65 | if (value == "yes") { |
| 66 | return true; |
| 67 | } |
| 68 | else if (value == "no") { |
| 69 | return false; |
| 70 | } |
| 71 | |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 72 | NDN_THROW(Error("Invalid value \"" + value + "\" for option \"" + |
| 73 | key + "\" in \"" + sectionName + "\" section")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
| 77 | ConfigFile::addSectionHandler(const std::string& sectionName, |
| 78 | ConfigSectionHandler subscriber) |
| 79 | { |
| 80 | m_subscriptions[sectionName] = subscriber; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | ConfigFile::parse(const std::string& filename, bool isDryRun) |
| 85 | { |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 86 | std::ifstream inputFile(filename); |
| 87 | if (!inputFile.good() || !inputFile.is_open()) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 88 | NDN_THROW(Error("Failed to read configuration file " + filename)); |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 89 | } |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 90 | parse(inputFile, isDryRun, filename); |
| 91 | inputFile.close(); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename) |
| 96 | { |
| 97 | std::istringstream inputStream(input); |
| 98 | parse(inputStream, isDryRun, filename); |
| 99 | } |
| 100 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 101 | void |
| 102 | ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename) |
| 103 | { |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 104 | try { |
| 105 | boost::property_tree::read_info(input, m_global); |
| 106 | } |
| 107 | catch (const boost::property_tree::info_parser_error& error) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 108 | NDN_THROW_NESTED(Error("Failed to parse configuration file " + filename + |
| 109 | ": " + error.message() + " on line " + to_string(error.line()))); |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 110 | } |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 111 | |
| 112 | process(isDryRun, filename); |
| 113 | } |
| 114 | |
| 115 | void |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 116 | ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 117 | { |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 118 | m_global = config; |
| 119 | process(isDryRun, filename); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | ConfigFile::process(bool isDryRun, const std::string& filename) const |
| 124 | { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 125 | BOOST_ASSERT(!filename.empty()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 127 | for (const auto& i : m_global) { |
| 128 | try { |
| 129 | const ConfigSectionHandler& subscriber = m_subscriptions.at(i.first); |
| 130 | subscriber(i.second, isDryRun, filename); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 131 | } |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 132 | catch (const std::out_of_range&) { |
| 133 | m_unknownSectionCallback(filename, i.first, i.second, isDryRun); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 134 | } |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 135 | } |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | } // namespace ndns |
| 139 | } // namespace ndn |