Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * 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 DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #ifndef NDN_MANAGEMENT_CONFIG_FILE_HPP |
| 14 | #define NDN_MANAGEMENT_CONFIG_FILE_HPP |
| 15 | |
| 16 | #include "../common.hpp" |
| 17 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame^] | 18 | #include <fstream> |
| 19 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 20 | #include <boost/property_tree/ptree.hpp> |
| 21 | #include <boost/filesystem.hpp> |
| 22 | |
| 23 | namespace ndn { |
| 24 | |
| 25 | class ConfigFile : noncopyable |
| 26 | { |
| 27 | public: |
| 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 | |
| 56 | private: |
| 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 | |
| 89 | private: |
| 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 | |
| 95 | inline const boost::filesystem::path& |
| 96 | ConfigFile::getPath() const |
| 97 | { |
| 98 | return m_path; |
| 99 | } |
| 100 | |
| 101 | inline const ConfigFile::Parsed& |
| 102 | ConfigFile::getParsedConfiguration() const |
| 103 | { |
| 104 | return m_config; |
| 105 | } |
| 106 | |
| 107 | } // namespace ndn |
| 108 | |
| 109 | |
| 110 | #endif // NDN_MANAGEMENT_CONFIG_FILE_HPP |