blob: 880657ab1876e7db01ad66d48c7da0ca85a29d1c [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
13#ifndef NDN_MANAGEMENT_CONFIG_FILE_HPP
14#define NDN_MANAGEMENT_CONFIG_FILE_HPP
15
16#include "../common.hpp"
17
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070018#include <fstream>
19
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060020#include <boost/property_tree/ptree.hpp>
21#include <boost/filesystem.hpp>
22
23namespace ndn {
24
25class ConfigFile : noncopyable
26{
27public:
28
29 class Error : public std::runtime_error
30 {
31 public:
32 Error(const std::string& what)
33 : std::runtime_error(what)
34 {
35
36 }
37 };
38
39 typedef boost::property_tree::ptree Parsed;
40
41 /**
42 * Locate, open, and parse a library configuration file.
43 *
44 * @throws ConfigFile::Error on parse error
45 */
46 ConfigFile();
47
48 ~ConfigFile();
49
50 const boost::filesystem::path&
51 getPath() const;
52
53 const Parsed&
54 getParsedConfiguration() const;
55
56private:
57
58 bool
59 open();
60
61 void
62 close();
63
64 /**
65 * Parse a previously discovered and opened configuration file.
66 * For convenience this method will attempt to open the file
67 * if it has previously been located, but open() has not been called.
68 *
69 * @throws ConfigFile::Error on parse error
70 * @throws ConfigFile::Error on failure to open previously un-open configuration file
71 * @throws ConfigFile::Error if no configuration file was previously located
72 */
73 const Parsed&
74 parse();
75
76 /**
77 * Looking for the configuration file in these well-known locations:
78 *
79 * 1. $HOME/.ndn/client.conf
80 * 2. @SYSCONFDIR@/ndn/client.conf
81 * 3. /etc/ndn/client.conf
82 *
83 * @return path to preferred configuration (according to above order) or empty path on failure
84 */
85
86 boost::filesystem::path
87 findConfigFile();
88
89private:
90 boost::filesystem::path m_path; // absolute path to active configuration file (if any)
91 std::ifstream m_input;
92 Parsed m_config;
93};
94
95inline const boost::filesystem::path&
96ConfigFile::getPath() const
97{
98 return m_path;
99}
100
101inline const ConfigFile::Parsed&
102ConfigFile::getParsedConfiguration() const
103{
104 return m_config;
105}
106
107} // namespace ndn
108
109
110#endif // NDN_MANAGEMENT_CONFIG_FILE_HPP