blob: 6ba45aaa15a42a8cd01ea3ce23ff24eddb17fc3d [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -06002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060011 */
12
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060013#include "config-file.hpp"
14
15#include <boost/property_tree/ini_parser.hpp>
16#include <boost/filesystem.hpp>
17
18namespace ndn {
19
20ConfigFile::ConfigFile()
21 : m_path(findConfigFile())
22{
23 if (open())
24 {
25 parse();
26 close();
27 }
28}
29
30ConfigFile::~ConfigFile()
31{
32 if (m_input.is_open())
33 {
34 m_input.close();
35 }
36}
37
38boost::filesystem::path
39ConfigFile::findConfigFile()
40{
41 using namespace boost::filesystem;
42
Yingdi Yuf56c68f2014-04-24 21:50:13 -070043#ifdef NDN_CXX_HAVE_TESTS
44 if (std::getenv("TEST_HOME"))
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060045 {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070046 path testHome(std::getenv("TEST_HOME"));
47 testHome /= ".ndn/client.conf";
48 if (exists(testHome))
49 {
50 return absolute(testHome);
51 }
52 }
53#endif // NDN_CXX_HAVE_TESTS
54
55 if (std::getenv("HOME"))
56 {
57 path home(std::getenv("HOME"));
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060058 home /= ".ndn/client.conf";
59 if (exists(home))
60 {
61 return absolute(home);
62 }
63 }
64
Alexander Afanasyev766cea72014-04-24 19:16:42 -070065#ifdef NDN_CXX_SYSCONFDIR
66 path sysconfdir(NDN_CXX_SYSCONFDIR);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060067 sysconfdir /= "ndn/client.conf";
68
69 if (exists(sysconfdir))
70 {
71 return absolute(sysconfdir);
72 }
Alexander Afanasyev766cea72014-04-24 19:16:42 -070073#endif // NDN_CXX_SYSCONFDIR
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060074
75 path etc("/etc/ndn/client.conf");
76 if (exists(etc))
77 {
78 return absolute(etc);
79 }
80
81 return path();
82}
83
84
85
86bool
87ConfigFile::open()
88{
89 if (m_path.empty())
90 {
91 return false;
92 }
93
94 m_input.open(m_path.c_str());
95 if (!m_input.good() || !m_input.is_open())
96 {
97 return false;
98 }
99 return true;
100}
101
102void
103ConfigFile::close()
104{
105 if (m_input.is_open())
106 {
107 m_input.close();
108 }
109}
110
111
112const ConfigFile::Parsed&
113ConfigFile::parse()
114{
115 if (m_path.empty())
116 {
117 throw Error("Failed to locate configuration file for parsing");
118 }
119 else if (!m_input.is_open() && !open())
120 {
121 throw Error("Failed to open configuration file for parsing");
122 }
123
124 try
125 {
126 boost::property_tree::read_ini(m_input, m_config);
127 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700128 catch (boost::property_tree::ini_parser_error& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600129 {
130 std::stringstream msg;
131 msg << "Failed to parse configuration file";
132 msg << " " << m_path;
133 msg << " " << error.message() << " line " << error.line();
134 throw Error(msg.str());
135 }
136 return m_config;
137}
138
139}