blob: 2bb6c2d753c7e43807d943501422cd01b97429dd [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -06002/**
Alexander Afanasyev57e00362016-06-23 13:22:54 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060020 */
21
22#ifndef NDN_MANAGEMENT_CONFIG_FILE_HPP
23#define NDN_MANAGEMENT_CONFIG_FILE_HPP
24
25#include "../common.hpp"
26
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070027#include <fstream>
28
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060029#include <boost/property_tree/ptree.hpp>
30#include <boost/filesystem.hpp>
31
32namespace ndn {
33
Alexander Afanasyev57e00362016-06-23 13:22:54 -070034/**
35 * @brief System configuration file for NDN platform
36 *
37 * The config file controls the default transport to connect to NDN forwarder, type and
38 * location of PIB and TPM.
39 *
40 * Looking for the configuration file in these well-known locations (in order):
41 *
42 * - `$HOME/.ndn/client.conf`
43 * - `@SYSCONFDIR@/ndn/client.conf`
44 * - `/etc/ndn/client.conf`
45 *
46 * @sa Manpage of ndn-client.conf
47 */
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060048class ConfigFile : noncopyable
49{
50public:
51
52 class Error : public std::runtime_error
53 {
54 public:
55 Error(const std::string& what)
56 : std::runtime_error(what)
57 {
58
59 }
60 };
61
62 typedef boost::property_tree::ptree Parsed;
63
64 /**
Alexander Afanasyev57e00362016-06-23 13:22:54 -070065 * @brief Locate, open, and parse a library configuration file.
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060066 *
67 * @throws ConfigFile::Error on parse error
68 */
69 ConfigFile();
70
71 ~ConfigFile();
72
73 const boost::filesystem::path&
74 getPath() const;
75
76 const Parsed&
77 getParsedConfiguration() const;
78
79private:
80
81 bool
82 open();
83
84 void
85 close();
86
87 /**
Alexander Afanasyev57e00362016-06-23 13:22:54 -070088 * @brief Parse a previously discovered and opened configuration file.
89 *
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060090 * For convenience this method will attempt to open the file
91 * if it has previously been located, but open() has not been called.
92 *
93 * @throws ConfigFile::Error on parse error
94 * @throws ConfigFile::Error on failure to open previously un-open configuration file
95 * @throws ConfigFile::Error if no configuration file was previously located
96 */
97 const Parsed&
98 parse();
99
100 /**
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700101 * @brief Find the configuration file in well-known locations
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600102 *
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700103 * The well-known locations include (in order):
104 *
105 * - `$HOME/.ndn/client.conf`
106 * - `@SYSCONFDIR@/ndn/client.conf`
107 * - `/etc/ndn/client.conf`
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600108 *
109 * @return path to preferred configuration (according to above order) or empty path on failure
110 */
111
112 boost::filesystem::path
113 findConfigFile();
114
115private:
116 boost::filesystem::path m_path; // absolute path to active configuration file (if any)
117 std::ifstream m_input;
118 Parsed m_config;
119};
120
121inline const boost::filesystem::path&
122ConfigFile::getPath() const
123{
124 return m_path;
125}
126
127inline const ConfigFile::Parsed&
128ConfigFile::getParsedConfiguration() const
129{
130 return m_config;
131}
132
133} // namespace ndn
134
135
136#endif // NDN_MANAGEMENT_CONFIG_FILE_HPP