blob: 162cf60200c4b1762b8775686b52d0fd37b21c5a [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
43 path home(std::getenv("HOME"));
44 if (!home.empty())
45 {
46 home /= ".ndn/client.conf";
47 if (exists(home))
48 {
49 return absolute(home);
50 }
51 }
52
Alexander Afanasyev766cea72014-04-24 19:16:42 -070053#ifdef NDN_CXX_SYSCONFDIR
54 path sysconfdir(NDN_CXX_SYSCONFDIR);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060055 sysconfdir /= "ndn/client.conf";
56
57 if (exists(sysconfdir))
58 {
59 return absolute(sysconfdir);
60 }
Alexander Afanasyev766cea72014-04-24 19:16:42 -070061#endif // NDN_CXX_SYSCONFDIR
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060062
63 path etc("/etc/ndn/client.conf");
64 if (exists(etc))
65 {
66 return absolute(etc);
67 }
68
69 return path();
70}
71
72
73
74bool
75ConfigFile::open()
76{
77 if (m_path.empty())
78 {
79 return false;
80 }
81
82 m_input.open(m_path.c_str());
83 if (!m_input.good() || !m_input.is_open())
84 {
85 return false;
86 }
87 return true;
88}
89
90void
91ConfigFile::close()
92{
93 if (m_input.is_open())
94 {
95 m_input.close();
96 }
97}
98
99
100const ConfigFile::Parsed&
101ConfigFile::parse()
102{
103 if (m_path.empty())
104 {
105 throw Error("Failed to locate configuration file for parsing");
106 }
107 else if (!m_input.is_open() && !open())
108 {
109 throw Error("Failed to open configuration file for parsing");
110 }
111
112 try
113 {
114 boost::property_tree::read_ini(m_input, m_config);
115 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700116 catch (boost::property_tree::ini_parser_error& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600117 {
118 std::stringstream msg;
119 msg << "Failed to parse configuration file";
120 msg << " " << m_path;
121 msg << " " << error.message() << " line " << error.line();
122 throw Error(msg.str());
123 }
124 return m_config;
125}
126
127}