blob: 548d351ec9f649263ff2e124ee31affe63aa7f8e [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 Pesavento85244372018-02-03 19:10:55 -05003 * Copyright (c) 2014-2018, 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
Steve DiBenedettobb75b552014-02-08 12:12:11 -070026#include "config-file.hpp"
27
28#include <boost/property_tree/info_parser.hpp>
Davide Pesavento52a18f92014-04-10 00:55:01 +020029#include <fstream>
Davide Pesaventoa997d292017-08-24 20:16:59 -040030#include <sstream>
Steve DiBenedettobb75b552014-02-08 12:12:11 -070031
32namespace nfd {
33
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020034ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback)
35 : m_unknownSectionCallback(unknownSectionCallback)
36{
37}
38
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060039void
40ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
41 const std::string& sectionName,
42 const ConfigSection& section,
43 bool isDryRun)
44{
Davide Pesaventoa997d292017-08-24 20:16:59 -040045 BOOST_THROW_EXCEPTION(Error("Error processing configuration file " + filename +
46 ": no module subscribed for section \"" + sectionName + "\""));
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060047}
48
49void
50ConfigFile::ignoreUnknownSection(const std::string& filename,
51 const std::string& sectionName,
52 const ConfigSection& section,
53 bool isDryRun)
54{
55 // do nothing
56}
57
Yanbiao Li698f4fe2015-08-19 16:30:16 -070058bool
Junxiao Shi0cc125c2016-08-25 21:50:04 +000059ConfigFile::parseYesNo(const ConfigSection& node, const std::string& key,
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020060 const std::string& sectionName)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070061{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000062 auto value = node.get_value<std::string>();
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020063
Yanbiao Li698f4fe2015-08-19 16:30:16 -070064 if (value == "yes") {
65 return true;
66 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020067 else if (value == "no") {
Yanbiao Li698f4fe2015-08-19 16:30:16 -070068 return false;
69 }
70
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020071 BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" +
Junxiao Shi0cc125c2016-08-25 21:50:04 +000072 key + "\" in \"" + sectionName + "\" section"));
Steve DiBenedettobb75b552014-02-08 12:12:11 -070073}
74
75void
76ConfigFile::addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060077 ConfigSectionHandler subscriber)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070078{
79 m_subscriptions[sectionName] = subscriber;
80}
81
82void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060083ConfigFile::parse(const std::string& filename, bool isDryRun)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070084{
Junxiao Shi0cc125c2016-08-25 21:50:04 +000085 std::ifstream inputFile(filename);
86 if (!inputFile.good() || !inputFile.is_open()) {
Davide Pesaventoa997d292017-08-24 20:16:59 -040087 BOOST_THROW_EXCEPTION(Error("Failed to read configuration file " + filename));
Junxiao Shi0cc125c2016-08-25 21:50:04 +000088 }
Steve DiBenedettobb75b552014-02-08 12:12:11 -070089 parse(inputFile, isDryRun, filename);
90 inputFile.close();
91}
92
93void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060094ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070095{
96 std::istringstream inputStream(input);
97 parse(inputStream, isDryRun, filename);
98}
99
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700100void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600101ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700102{
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000103 try {
104 boost::property_tree::read_info(input, m_global);
105 }
106 catch (const boost::property_tree::info_parser_error& error) {
Davide Pesaventoa997d292017-08-24 20:16:59 -0400107 BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + filename +
108 ": " + error.message() + " on line " + to_string(error.line())));
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000109 }
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700110
111 process(isDryRun, filename);
112}
113
114void
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800115ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename)
116{
117 m_global = config;
118 process(isDryRun, filename);
119}
120
121void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200122ConfigFile::process(bool isDryRun, const std::string& filename) const
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700123{
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600124 BOOST_ASSERT(!filename.empty());
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700125
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200126 for (const auto& i : m_global) {
127 try {
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000128 const ConfigSectionHandler& subscriber = m_subscriptions.at(i.first);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200129 subscriber(i.second, isDryRun, filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700130 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200131 catch (const std::out_of_range&) {
132 m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700133 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200134 }
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700135}
136
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000137} // namespace nfd