ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
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) 2013-2024 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/property_tree/ini_parser.hpp>
25#include <cstdlib>
26
27namespace ndn {
28
30 : m_path(findConfigFile())
31{
32 if (open()) {
33 parse();
34 close();
35 }
36}
37
39{
40 close();
41}
42
43std::filesystem::path
44ConfigFile::findConfigFile()
45{
46 using namespace std::filesystem;
47
48#ifdef NDN_CXX_WITH_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
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
73
74 path etc("/etc/ndn/client.conf");
75 if (exists(etc)) {
76 return absolute(etc);
77 }
78
79 return {};
80}
81
82bool
83ConfigFile::open()
84{
85 if (m_path.empty()) {
86 return false;
87 }
88
89 m_input.open(m_path);
90 if (!m_input.good() || !m_input.is_open()) {
91 return false;
92 }
93 return true;
94}
95
96void
97ConfigFile::close()
98{
99 if (m_input.is_open()) {
100 m_input.close();
101 }
102}
103
105ConfigFile::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
ConfigFile()
Locate, open, and parse a library configuration file.
boost::property_tree::ptree Parsed
#define NDN_THROW(e)
Definition exception.hpp:56
std::string to_string(const errinfo_stacktrace &x)
Definition exception.cpp:30
Definition data.cpp:25