NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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) 2013-2021 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * 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.
20  */
21 
23 
24 #include <boost/filesystem/operations.hpp>
25 #include <boost/property_tree/ini_parser.hpp>
26 
27 namespace ndn {
28 
30  : m_path(findConfigFile())
31 {
32  if (open()) {
33  parse();
34  close();
35  }
36 }
37 
39 {
40  close();
41 }
42 
43 boost::filesystem::path
44 ConfigFile::findConfigFile()
45 {
46  using namespace boost::filesystem;
47 
48 #ifdef NDN_CXX_HAVE_TESTS
49  if (std::getenv("TEST_HOME")) {
50  path testHome(std::getenv("TEST_HOME"));
51  testHome /= ".ndn/client.conf";
52  if (exists(testHome)) {
53  return absolute(testHome);
54  }
55  }
56 #endif // NDN_CXX_HAVE_TESTS
57 
58  if (std::getenv("HOME")) {
59  path home(std::getenv("HOME"));
60  home /= ".ndn/client.conf";
61  if (exists(home)) {
62  return absolute(home);
63  }
64  }
65 
66 #ifdef NDN_CXX_SYSCONFDIR
67  path sysconfdir(NDN_CXX_SYSCONFDIR);
68  sysconfdir /= "ndn/client.conf";
69  if (exists(sysconfdir)) {
70  return absolute(sysconfdir);
71  }
72 #endif // NDN_CXX_SYSCONFDIR
73 
74  path etc("/etc/ndn/client.conf");
75  if (exists(etc)) {
76  return absolute(etc);
77  }
78 
79  return {};
80 }
81 
82 bool
83 ConfigFile::open()
84 {
85  if (m_path.empty()) {
86  return false;
87  }
88 
89  m_input.open(m_path.c_str());
90  if (!m_input.good() || !m_input.is_open()) {
91  return false;
92  }
93  return true;
94 }
95 
96 void
97 ConfigFile::close()
98 {
99  if (m_input.is_open()) {
100  m_input.close();
101  }
102 }
103 
104 const ConfigFile::Parsed&
105 ConfigFile::parse()
106 {
107  if (m_path.empty()) {
108  NDN_THROW(Error("Failed to locate configuration file for parsing"));
109  }
110  if (!m_input.is_open() && !open()) {
111  NDN_THROW(Error("Failed to open configuration file for parsing"));
112  }
113 
114  try {
115  boost::property_tree::read_ini(m_input, m_config);
116  }
117  catch (const boost::property_tree::ini_parser_error& error) {
118  NDN_THROW(Error("Failed to parse configuration file " + error.filename() +
119  " line " + to_string(error.line()) + ": " + error.message()));
120  }
121  return m_config;
122 }
123 
124 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
boost::property_tree::ptree Parsed
Definition: config-file.hpp:57
std::string to_string(const T &val)
Definition: backports.hpp:86
#define NDN_THROW(e)
Definition: exception.hpp:61
ConfigFile()
Locate, open, and parse a library configuration file.
Definition: config-file.cpp:29