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