blob: 636a69ef6a0e9687c5bea53868e4a632be10f0f4 [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev08d18742018-03-15 16:31:28 -04002/*
Davide Pesavento948c50c2020-12-26 21:30:45 -05003 * Copyright (c) 2014-2020, Regents of the University of California,
Alexander Afanasyev08d18742018-03-15 16:31:28 -04004 * 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 Jiangcde28712014-10-19 21:17:20 -070010 *
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 Jiangcde28712014-10-19 21:17:20 -070026#include "config-file.hpp"
27#include "logger.hpp"
28
29#include <boost/property_tree/info_parser.hpp>
30#include <fstream>
31
Shock Jiangcde28712014-10-19 21:17:20 -070032namespace ndn {
33namespace ndns {
34
Alexander Afanasyev08d18742018-03-15 16:31:28 -040035ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback)
36 : m_unknownSectionCallback(unknownSectionCallback)
37{
38}
Shock Jiangcde28712014-10-19 21:17:20 -070039
40void
41ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
42 const std::string& sectionName,
43 const ConfigSection& section,
44 bool isDryRun)
45{
Davide Pesavento948c50c2020-12-26 21:30:45 -050046 NDN_THROW(Error("Error processing configuration file " + filename +
47 ": no module subscribed for section \"" + sectionName + "\""));
Shock Jiangcde28712014-10-19 21:17:20 -070048}
49
50void
51ConfigFile::ignoreUnknownSection(const std::string& filename,
52 const std::string& sectionName,
53 const ConfigSection& section,
54 bool isDryRun)
55{
56 // do nothing
57}
58
Alexander Afanasyev08d18742018-03-15 16:31:28 -040059bool
60ConfigFile::parseYesNo(const ConfigSection& node, const std::string& key,
61 const std::string& sectionName)
Shock Jiangcde28712014-10-19 21:17:20 -070062{
Alexander Afanasyev08d18742018-03-15 16:31:28 -040063 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 Pesavento948c50c2020-12-26 21:30:45 -050072 NDN_THROW(Error("Invalid value \"" + value + "\" for option \"" +
73 key + "\" in \"" + sectionName + "\" section"));
Shock Jiangcde28712014-10-19 21:17:20 -070074}
75
76void
77ConfigFile::addSectionHandler(const std::string& sectionName,
78 ConfigSectionHandler subscriber)
79{
80 m_subscriptions[sectionName] = subscriber;
81}
82
83void
84ConfigFile::parse(const std::string& filename, bool isDryRun)
85{
Alexander Afanasyev08d18742018-03-15 16:31:28 -040086 std::ifstream inputFile(filename);
87 if (!inputFile.good() || !inputFile.is_open()) {
Davide Pesavento948c50c2020-12-26 21:30:45 -050088 NDN_THROW(Error("Failed to read configuration file " + filename));
Alexander Afanasyev08d18742018-03-15 16:31:28 -040089 }
Shock Jiangcde28712014-10-19 21:17:20 -070090 parse(inputFile, isDryRun, filename);
91 inputFile.close();
92}
93
94void
95ConfigFile::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 Jiangcde28712014-10-19 21:17:20 -0700101void
102ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
103{
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400104 try {
105 boost::property_tree::read_info(input, m_global);
106 }
107 catch (const boost::property_tree::info_parser_error& error) {
Davide Pesavento948c50c2020-12-26 21:30:45 -0500108 NDN_THROW_NESTED(Error("Failed to parse configuration file " + filename +
109 ": " + error.message() + " on line " + to_string(error.line())));
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400110 }
Shock Jiangcde28712014-10-19 21:17:20 -0700111
112 process(isDryRun, filename);
113}
114
115void
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400116ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename)
Shock Jiangcde28712014-10-19 21:17:20 -0700117{
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400118 m_global = config;
119 process(isDryRun, filename);
120}
121
122void
123ConfigFile::process(bool isDryRun, const std::string& filename) const
124{
Shock Jiangcde28712014-10-19 21:17:20 -0700125 BOOST_ASSERT(!filename.empty());
Shock Jiangcde28712014-10-19 21:17:20 -0700126
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400127 for (const auto& i : m_global) {
128 try {
129 const ConfigSectionHandler& subscriber = m_subscriptions.at(i.first);
130 subscriber(i.second, isDryRun, filename);
Shock Jiangcde28712014-10-19 21:17:20 -0700131 }
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400132 catch (const std::out_of_range&) {
133 m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
Shock Jiangcde28712014-10-19 21:17:20 -0700134 }
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400135 }
Shock Jiangcde28712014-10-19 21:17:20 -0700136}
137
138} // namespace ndns
139} // namespace ndn