blob: d0eddd565bfcc50a367c07371dabc51a4e4b540f [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7
8#include "config-file.hpp"
9
10#include <boost/property_tree/info_parser.hpp>
11
12namespace nfd {
13
14NFD_LOG_INIT("ConfigFile");
15
16ConfigFile::ConfigFile()
17{
18
19}
20
21void
22ConfigFile::addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060023 ConfigSectionHandler subscriber)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070024{
25 m_subscriptions[sectionName] = subscriber;
26}
27
28void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060029ConfigFile::parse(const std::string& filename, bool isDryRun)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070030{
31 std::ifstream inputFile;
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060032 inputFile.open(filename.c_str());
33 if (!inputFile.good() || !inputFile.is_open())
Steve DiBenedettobb75b552014-02-08 12:12:11 -070034 {
35 std::string msg = "Failed to read configuration file: ";
36 msg += filename;
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070037 throw Error(msg);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070038 }
39 parse(inputFile, isDryRun, filename);
40 inputFile.close();
41}
42
43void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060044ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070045{
46 std::istringstream inputStream(input);
47 parse(inputStream, isDryRun, filename);
48}
49
50
51void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060052ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070053{
54 try
55 {
56 boost::property_tree::read_info(input, m_global);
57 }
58 catch (const boost::property_tree::info_parser_error& error)
59 {
60 std::stringstream msg;
61 msg << "Failed to parse configuration file";
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060062 msg << " " << filename;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070063 msg << " " << error.message() << " line " << error.line();
64 throw Error(msg.str());
65 }
66
67 process(isDryRun, filename);
68}
69
70void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060071ConfigFile::process(bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070072{
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060073 BOOST_ASSERT(!filename.empty());
Steve DiBenedettobb75b552014-02-08 12:12:11 -070074 // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):("")));
75
76 if (m_global.begin() == m_global.end())
77 {
78 std::string msg = "Error processing configuration file";
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060079 msg += ": ";
80 msg += filename;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070081 msg += " no data";
82 throw Error(msg);
83 }
84
85 for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i)
86 {
87 const std::string& sectionName = i->first;
88 const ConfigSection& section = i->second;
89
90 SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName);
91 if (subscriberIt != m_subscriptions.end())
92 {
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060093 ConfigSectionHandler subscriber = subscriberIt->second;
94 subscriber(section, isDryRun, filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070095 }
96 else
97 {
98 std::string msg = "Error processing configuration file";
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060099 msg += " ";
100 msg += filename;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700101 msg += " no module subscribed for section: " + sectionName;
102 throw Error(msg);
103 }
104 }
105}
106
107}
108