blob: 5c71dded60a2578957bc28d17df90527ad53323c [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
34class ConfigFile : noncopyable
35{
36public:
37
38 class Error : public std::runtime_error
39 {
40 public:
41 Error(const std::string& what)
42 : std::runtime_error(what)
43 {
44
45 }
46 };
47
48 typedef boost::property_tree::ptree Parsed;
49
50 /**
51 * Locate, open, and parse a library configuration file.
52 *
53 * @throws ConfigFile::Error on parse error
54 */
55 ConfigFile();
56
57 ~ConfigFile();
58
59 const boost::filesystem::path&
60 getPath() const;
61
62 const Parsed&
63 getParsedConfiguration() const;
64
65private:
66
67 bool
68 open();
69
70 void
71 close();
72
73 /**
74 * Parse a previously discovered and opened configuration file.
75 * For convenience this method will attempt to open the file
76 * if it has previously been located, but open() has not been called.
77 *
78 * @throws ConfigFile::Error on parse error
79 * @throws ConfigFile::Error on failure to open previously un-open configuration file
80 * @throws ConfigFile::Error if no configuration file was previously located
81 */
82 const Parsed&
83 parse();
84
85 /**
86 * Looking for the configuration file in these well-known locations:
87 *
88 * 1. $HOME/.ndn/client.conf
89 * 2. @SYSCONFDIR@/ndn/client.conf
90 * 3. /etc/ndn/client.conf
91 *
92 * @return path to preferred configuration (according to above order) or empty path on failure
93 */
94
95 boost::filesystem::path
96 findConfigFile();
97
98private:
99 boost::filesystem::path m_path; // absolute path to active configuration file (if any)
100 std::ifstream m_input;
101 Parsed m_config;
102};
103
104inline const boost::filesystem::path&
105ConfigFile::getPath() const
106{
107 return m_path;
108}
109
110inline const ConfigFile::Parsed&
111ConfigFile::getParsedConfiguration() const
112{
113 return m_config;
114}
115
116} // namespace ndn
117
118
119#endif // NDN_MANAGEMENT_CONFIG_FILE_HPP