NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
config-file.cpp
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22
#include "
config-file.hpp
"
23
24
#include <boost/property_tree/ini_parser.hpp>
25
#include <boost/filesystem.hpp>
26
27
namespace
ndn
{
28
29
ConfigFile::ConfigFile
()
30
: m_path(findConfigFile())
31
{
32
if
(open())
33
{
34
parse();
35
close();
36
}
37
}
38
39
ConfigFile::~ConfigFile
()
40
{
41
if
(m_input.is_open())
42
{
43
m_input.close();
44
}
45
}
46
47
boost::filesystem::path
48
ConfigFile::findConfigFile()
49
{
50
using namespace
boost::filesystem;
51
52
#ifdef NDN_CXX_HAVE_TESTS
53
if
(std::getenv(
"TEST_HOME"
))
54
{
55
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"
));
67
home /=
".ndn/client.conf"
;
68
if
(exists(home))
69
{
70
return
absolute(home);
71
}
72
}
73
74
#ifdef NDN_CXX_SYSCONFDIR
75
path sysconfdir(NDN_CXX_SYSCONFDIR);
76
sysconfdir /=
"ndn/client.conf"
;
77
78
if
(exists(sysconfdir))
79
{
80
return
absolute(sysconfdir);
81
}
82
#endif // NDN_CXX_SYSCONFDIR
83
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
95
bool
96
ConfigFile::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
111
void
112
ConfigFile::close()
113
{
114
if
(m_input.is_open())
115
{
116
m_input.close();
117
}
118
}
119
120
121
const
ConfigFile::Parsed
&
122
ConfigFile::parse()
123
{
124
if
(m_path.empty())
125
{
126
BOOST_THROW_EXCEPTION(
Error
(
"Failed to locate configuration file for parsing"
));
127
}
128
else
if
(!m_input.is_open() && !open())
129
{
130
BOOST_THROW_EXCEPTION(
Error
(
"Failed to open configuration file for parsing"
));
131
}
132
133
try
134
{
135
boost::property_tree::read_ini(m_input, m_config);
136
}
137
catch
(boost::property_tree::ini_parser_error& error)
138
{
139
std::stringstream msg;
140
msg <<
"Failed to parse configuration file"
;
141
msg <<
" "
<< m_path;
142
msg <<
" "
<< error.message() <<
" line "
<< error.line();
143
BOOST_THROW_EXCEPTION(
Error
(msg.str()));
144
}
145
return
m_config;
146
}
147
148
}
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition:
ndn-strategy-choice-helper.hpp:34
ndn::ConfigFile::Parsed
boost::property_tree::ptree Parsed
Definition:
config-file.hpp:62
ndn::ConfigFile::~ConfigFile
~ConfigFile()
Definition:
config-file.cpp:39
ndn::ConfigFile::ConfigFile
ConfigFile()
Locate, open, and parse a library configuration file.
Definition:
config-file.cpp:29
config-file.hpp
ndn::ConfigFile::Error
Definition:
config-file.hpp:52
ndnSIM
ndn-cxx
src
util
config-file.cpp
Generated on Thu Nov 2 2017 03:30:29 for ndnSIM by
1.8.11