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