blob: ef1eed03c3c6594e612fa2ab1424e3bce31709e0 [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa997d292017-08-24 20:16:59 -04002/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Davide Pesavento1d7e7af2015-10-10 23:54:08 +02004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Pesavento1d7e7af2015-10-10 23:54:08 +020024 */
Steve DiBenedettobb75b552014-02-08 12:12:11 -070025
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040026#include "common/config-file.hpp"
Steve DiBenedettobb75b552014-02-08 12:12:11 -070027
28#include <boost/property_tree/info_parser.hpp>
Davide Pesavento19779d82019-02-14 13:40:04 -050029
Davide Pesavento52a18f92014-04-10 00:55:01 +020030#include <fstream>
Davide Pesaventoa997d292017-08-24 20:16:59 -040031#include <sstream>
Steve DiBenedettobb75b552014-02-08 12:12:11 -070032
33namespace nfd {
34
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040035ConfigFile::ConfigFile(UnknownConfigSectionHandler callback)
36 : m_unknownSectionCallback(std::move(callback))
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020037{
38}
39
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060040void
41ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
42 const std::string& sectionName,
43 const ConfigSection& section,
44 bool isDryRun)
45{
Davide Pesavento19779d82019-02-14 13:40:04 -050046 NDN_THROW(Error("Error processing configuration file " + filename +
47 ": no module subscribed for section '" + sectionName + "'"));
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060048}
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
Yanbiao Li698f4fe2015-08-19 16:30:16 -070059bool
Junxiao Shi0cc125c2016-08-25 21:50:04 +000060ConfigFile::parseYesNo(const ConfigSection& node, const std::string& key,
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020061 const std::string& sectionName)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070062{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000063 auto value = node.get_value<std::string>();
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020064
Yanbiao Li698f4fe2015-08-19 16:30:16 -070065 if (value == "yes") {
66 return true;
67 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020068 else if (value == "no") {
Yanbiao Li698f4fe2015-08-19 16:30:16 -070069 return false;
70 }
71
Davide Pesavento19779d82019-02-14 13:40:04 -050072 NDN_THROW(Error("Invalid value '" + value + "' for option '" +
73 key + "' in section '" + sectionName + "'"));
Steve DiBenedettobb75b552014-02-08 12:12:11 -070074}
75
76void
77ConfigFile::addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060078 ConfigSectionHandler subscriber)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070079{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040080 m_subscriptions[sectionName] = std::move(subscriber);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070081}
82
83void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060084ConfigFile::parse(const std::string& filename, bool isDryRun)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070085{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000086 std::ifstream inputFile(filename);
87 if (!inputFile.good() || !inputFile.is_open()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050088 NDN_THROW(Error("Failed to read configuration file " + filename));
Junxiao Shi0cc125c2016-08-25 21:50:04 +000089 }
Steve DiBenedettobb75b552014-02-08 12:12:11 -070090 parse(inputFile, isDryRun, filename);
91 inputFile.close();
92}
93
94void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060095ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070096{
97 std::istringstream inputStream(input);
98 parse(inputStream, isDryRun, filename);
99}
100
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700101void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600102ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700103{
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000104 try {
105 boost::property_tree::read_info(input, m_global);
106 }
107 catch (const boost::property_tree::info_parser_error& error) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500108 NDN_THROW(Error("Failed to parse configuration file " + filename +
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500109 ": " + error.message() + " on line " + std::to_string(error.line())));
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000110 }
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700111
112 process(isDryRun, filename);
113}
114
115void
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800116ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename)
117{
118 m_global = config;
119 process(isDryRun, filename);
120}
121
122void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200123ConfigFile::process(bool isDryRun, const std::string& filename) const
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700124{
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600125 BOOST_ASSERT(!filename.empty());
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700126
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200127 for (const auto& i : m_global) {
128 try {
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000129 const ConfigSectionHandler& subscriber = m_subscriptions.at(i.first);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200130 subscriber(i.second, isDryRun, filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700131 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200132 catch (const std::out_of_range&) {
133 m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700134 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200135 }
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700136}
137
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000138} // namespace nfd