Loading...
Searching...
No Matches
config-file.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2024, Regents of the University of California,
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.
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/>.
24 */
25
27
28#include <boost/property_tree/info_parser.hpp>
29
30#include <fstream>
31#include <sstream>
32
33namespace nfd {
34
36 : m_unknownSectionCallback(std::move(callback))
37{
38}
39
40void
41ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
42 const std::string& sectionName,
43 const ConfigSection& section,
44 bool isDryRun)
45{
46 NDN_THROW(Error("Error processing configuration file " + filename +
47 ": no module subscribed for section '" + sectionName + "'"));
48}
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
59bool
60ConfigFile::parseYesNo(const ConfigSection& node, const std::string& key,
61 const std::string& sectionName)
62{
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
72 NDN_THROW(Error("Invalid value '" + value + "' for option '" +
73 key + "' in section '" + sectionName + "'"));
74}
75
76void
77ConfigFile::addSectionHandler(const std::string& sectionName,
78 ConfigSectionHandler subscriber)
79{
80 m_subscriptions[sectionName] = std::move(subscriber);
81}
82
83void
84ConfigFile::parse(const std::string& filename, bool isDryRun)
85{
86 std::ifstream inputFile(filename);
87 if (!inputFile.good() || !inputFile.is_open()) {
88 NDN_THROW(Error("Failed to read configuration file " + filename));
89 }
90 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
101void
102ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
103{
104 try {
105 boost::property_tree::read_info(input, m_global);
106 }
107 catch (const boost::property_tree::info_parser_error& error) {
108 NDN_THROW(Error("Failed to parse configuration file " + filename +
109 ": " + error.message() + " on line " + std::to_string(error.line())));
110 }
111
112 process(isDryRun, filename);
113}
114
115void
116ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename)
117{
118 m_global = config;
119 process(isDryRun, filename);
120}
121
122void
123ConfigFile::process(bool isDryRun, const std::string& filename) const
124{
125 BOOST_ASSERT(!filename.empty());
126
127 for (const auto& i : m_global) {
128 try {
129 const ConfigSectionHandler& subscriber = m_subscriptions.at(i.first);
130 subscriber(i.second, isDryRun, filename);
131 }
132 catch (const std::out_of_range&) {
133 m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
134 }
135 }
136}
137
138} // namespace nfd
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
void parse(const std::string &filename, bool isDryRun)
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
Setup notification of configuration file sections.
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string &sectionName)
Parse a config option that can be either "yes" or "no".
-status-http-server
Definition common.hpp:71
std::function< void(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)> UnknownConfigSectionHandler
Callback to process a configuration file section without a ConfigSectionHandler.
boost::property_tree::ptree ConfigSection
A configuration file section.
std::function< void(const ConfigSection &section, bool isDryRun, const std::string &filename)> ConfigSectionHandler
Callback to process a configuration file section.