blob: 0b335f90c2a50a2cd34298cc1532ef14d3b3686d [file] [log] [blame]
Chengyu Fanb25835b2015-04-28 17:09:35 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
24
25#include "config-file.hpp"
26
27#include <boost/property_tree/info_parser.hpp>
28#include <fstream>
29namespace atmos {
30namespace util {
31
32void
33ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
34 const std::string& sectionName,
35 const ConfigSection& section,
36 bool isDryRun)
37{
38 std::string msg = "Error processing configuration file ";
39 msg += filename;
40 msg += ": no module subscribed for section \"" + sectionName + "\"";
41
42 throw ConfigFile::Error(msg);
43}
44
45void
46ConfigFile::ignoreUnknownSection(const std::string& filename,
47 const std::string& sectionName,
48 const ConfigSection& section,
49 bool isDryRun)
50{
51 // do nothing
52}
53
54ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback)
55 : m_unknownSectionCallback(unknownSectionCallback)
56{
57}
58
59void
60ConfigFile::addSectionHandler(const std::string& sectionName,
61 ConfigSectionHandler subscriber)
62{
63 m_subscriptions[sectionName] = subscriber;
64}
65
66void
67ConfigFile::parse(const std::string& filename, bool isDryRun)
68{
69 std::ifstream inputFile;
70 inputFile.open(filename.c_str());
71 if (!inputFile.good() || !inputFile.is_open())
72 {
73 std::string msg = "Failed to read configuration file: ";
74 msg += filename;
75 throw Error(msg);
76 }
77 parse(inputFile, isDryRun, filename);
78 inputFile.close();
79}
80
81void
82ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
83{
84 std::istringstream inputStream(input);
85 parse(inputStream, isDryRun, filename);
86}
87
88
89void
90ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
91{
92 try
93 {
94 boost::property_tree::read_info(input, m_global);
95 }
96 catch (const boost::property_tree::info_parser_error& error)
97 {
98 std::stringstream msg;
99 msg << "Failed to parse configuration file";
100 msg << " " << filename;
101 msg << " " << error.message() << " line " << error.line();
102 throw Error(msg.str());
103 }
104
105 process(isDryRun, filename);
106}
107
108void
109ConfigFile::process(bool isDryRun, const std::string& filename)
110{
111 BOOST_ASSERT(!filename.empty());
112
113 if (m_global.begin() == m_global.end())
114 {
115 std::string msg = "Error processing configuration file: ";
116 msg += filename;
117 msg += " no data";
118 throw Error(msg);
119 }
120
121 for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i)
122 {
123 const std::string& sectionName = i->first;
124 const ConfigSection& section = i->second;
125
126 SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName);
127 if (subscriberIt != m_subscriptions.end())
128 {
129 ConfigSectionHandler subscriber = subscriberIt->second;
130 subscriber(section, isDryRun, filename);
131 }
132 else
133 {
134 m_unknownSectionCallback(filename, sectionName, section, isDryRun);
135 }
136 }
137}
138
139} // util
140} // atmos