blob: a68939de46dc64d41c504066904c4a87e6dcfd64 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -06002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060020 */
21
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060022#include "config-file.hpp"
23
24#include <boost/property_tree/ini_parser.hpp>
25#include <boost/filesystem.hpp>
26
27namespace ndn {
28
29ConfigFile::ConfigFile()
30 : m_path(findConfigFile())
31{
32 if (open())
33 {
34 parse();
35 close();
36 }
37}
38
39ConfigFile::~ConfigFile()
40{
41 if (m_input.is_open())
42 {
43 m_input.close();
44 }
45}
46
47boost::filesystem::path
48ConfigFile::findConfigFile()
49{
50 using namespace boost::filesystem;
51
Yingdi Yuf56c68f2014-04-24 21:50:13 -070052#ifdef NDN_CXX_HAVE_TESTS
53 if (std::getenv("TEST_HOME"))
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060054 {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070055 path testHome(std::getenv("TEST_HOME"));
56 testHome /= ".ndn/client.conf";
57 if (exists(testHome))
58 {
59 return absolute(testHome);
60 }
61 }
62#endif // NDN_CXX_HAVE_TESTS
63
64 if (std::getenv("HOME"))
65 {
66 path home(std::getenv("HOME"));
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060067 home /= ".ndn/client.conf";
68 if (exists(home))
69 {
70 return absolute(home);
71 }
72 }
73
Alexander Afanasyev766cea72014-04-24 19:16:42 -070074#ifdef NDN_CXX_SYSCONFDIR
75 path sysconfdir(NDN_CXX_SYSCONFDIR);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060076 sysconfdir /= "ndn/client.conf";
77
78 if (exists(sysconfdir))
79 {
80 return absolute(sysconfdir);
81 }
Alexander Afanasyev766cea72014-04-24 19:16:42 -070082#endif // NDN_CXX_SYSCONFDIR
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060083
84 path etc("/etc/ndn/client.conf");
85 if (exists(etc))
86 {
87 return absolute(etc);
88 }
89
90 return path();
91}
92
93
94
95bool
96ConfigFile::open()
97{
98 if (m_path.empty())
99 {
100 return false;
101 }
102
103 m_input.open(m_path.c_str());
104 if (!m_input.good() || !m_input.is_open())
105 {
106 return false;
107 }
108 return true;
109}
110
111void
112ConfigFile::close()
113{
114 if (m_input.is_open())
115 {
116 m_input.close();
117 }
118}
119
120
121const ConfigFile::Parsed&
122ConfigFile::parse()
123{
124 if (m_path.empty())
125 {
126 throw Error("Failed to locate configuration file for parsing");
127 }
128 else if (!m_input.is_open() && !open())
129 {
130 throw Error("Failed to open configuration file for parsing");
131 }
132
133 try
134 {
135 boost::property_tree::read_ini(m_input, m_config);
136 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700137 catch (boost::property_tree::ini_parser_error& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600138 {
139 std::stringstream msg;
140 msg << "Failed to parse configuration file";
141 msg << " " << m_path;
142 msg << " " << error.message() << " line " << error.line();
143 throw Error(msg.str());
144 }
145 return m_config;
146}
147
148}