Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [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 | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 20 | */ |
| 21 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 22 | #include "config-file.hpp" |
| 23 | |
| 24 | #include <boost/property_tree/ini_parser.hpp> |
| 25 | #include <boost/filesystem.hpp> |
| 26 | |
| 27 | namespace ndn { |
| 28 | |
| 29 | ConfigFile::ConfigFile() |
| 30 | : m_path(findConfigFile()) |
| 31 | { |
| 32 | if (open()) |
| 33 | { |
| 34 | parse(); |
| 35 | close(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | ConfigFile::~ConfigFile() |
| 40 | { |
| 41 | if (m_input.is_open()) |
| 42 | { |
| 43 | m_input.close(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | boost::filesystem::path |
| 48 | ConfigFile::findConfigFile() |
| 49 | { |
| 50 | using namespace boost::filesystem; |
| 51 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 52 | #ifdef NDN_CXX_HAVE_TESTS |
| 53 | if (std::getenv("TEST_HOME")) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 54 | { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 55 | path testHome(std::getenv("TEST_HOME")); |
| 56 | testHome /= ".ndn/client.conf"; |
| 57 | if (exists(testHome)) |
| 58 | { |
| 59 | return absolute(testHome); |
| 60 | } |
| 61 | } |
| 62 | #endif // NDN_CXX_HAVE_TESTS |
| 63 | |
| 64 | if (std::getenv("HOME")) |
| 65 | { |
| 66 | path home(std::getenv("HOME")); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 67 | home /= ".ndn/client.conf"; |
| 68 | if (exists(home)) |
| 69 | { |
| 70 | return absolute(home); |
| 71 | } |
| 72 | } |
| 73 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 74 | #ifdef NDN_CXX_SYSCONFDIR |
| 75 | path sysconfdir(NDN_CXX_SYSCONFDIR); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 76 | sysconfdir /= "ndn/client.conf"; |
| 77 | |
| 78 | if (exists(sysconfdir)) |
| 79 | { |
| 80 | return absolute(sysconfdir); |
| 81 | } |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 82 | #endif // NDN_CXX_SYSCONFDIR |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 83 | |
| 84 | path etc("/etc/ndn/client.conf"); |
| 85 | if (exists(etc)) |
| 86 | { |
| 87 | return absolute(etc); |
| 88 | } |
| 89 | |
| 90 | return path(); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | bool |
| 96 | ConfigFile::open() |
| 97 | { |
| 98 | if (m_path.empty()) |
| 99 | { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | m_input.open(m_path.c_str()); |
| 104 | if (!m_input.good() || !m_input.is_open()) |
| 105 | { |
| 106 | return false; |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | ConfigFile::close() |
| 113 | { |
| 114 | if (m_input.is_open()) |
| 115 | { |
| 116 | m_input.close(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | const ConfigFile::Parsed& |
| 122 | ConfigFile::parse() |
| 123 | { |
| 124 | if (m_path.empty()) |
| 125 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 126 | BOOST_THROW_EXCEPTION(Error("Failed to locate configuration file for parsing")); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 127 | } |
| 128 | else if (!m_input.is_open() && !open()) |
| 129 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 130 | BOOST_THROW_EXCEPTION(Error("Failed to open configuration file for parsing")); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | try |
| 134 | { |
| 135 | boost::property_tree::read_ini(m_input, m_config); |
| 136 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 137 | catch (boost::property_tree::ini_parser_error& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 138 | { |
| 139 | std::stringstream msg; |
| 140 | msg << "Failed to parse configuration file"; |
| 141 | msg << " " << m_path; |
| 142 | msg << " " << error.message() << " line " << error.line(); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 143 | BOOST_THROW_EXCEPTION(Error(msg.str())); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 144 | } |
| 145 | return m_config; |
| 146 | } |
| 147 | |
| 148 | } |