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 | |
| 43 | path home(std::getenv("HOME")); |
| 44 | if (!home.empty()) |
| 45 | { |
| 46 | home /= ".ndn/client.conf"; |
| 47 | if (exists(home)) |
| 48 | { |
| 49 | return absolute(home); |
| 50 | } |
| 51 | } |
| 52 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 53 | #ifdef NDN_CXX_SYSCONFDIR |
| 54 | path sysconfdir(NDN_CXX_SYSCONFDIR); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 55 | sysconfdir /= "ndn/client.conf"; |
| 56 | |
| 57 | if (exists(sysconfdir)) |
| 58 | { |
| 59 | return absolute(sysconfdir); |
| 60 | } |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 61 | #endif // NDN_CXX_SYSCONFDIR |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 62 | |
| 63 | path etc("/etc/ndn/client.conf"); |
| 64 | if (exists(etc)) |
| 65 | { |
| 66 | return absolute(etc); |
| 67 | } |
| 68 | |
| 69 | return path(); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | bool |
| 75 | ConfigFile::open() |
| 76 | { |
| 77 | if (m_path.empty()) |
| 78 | { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | m_input.open(m_path.c_str()); |
| 83 | if (!m_input.good() || !m_input.is_open()) |
| 84 | { |
| 85 | return false; |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | ConfigFile::close() |
| 92 | { |
| 93 | if (m_input.is_open()) |
| 94 | { |
| 95 | m_input.close(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | const ConfigFile::Parsed& |
| 101 | ConfigFile::parse() |
| 102 | { |
| 103 | if (m_path.empty()) |
| 104 | { |
| 105 | throw Error("Failed to locate configuration file for parsing"); |
| 106 | } |
| 107 | else if (!m_input.is_open() && !open()) |
| 108 | { |
| 109 | throw Error("Failed to open configuration file for parsing"); |
| 110 | } |
| 111 | |
| 112 | try |
| 113 | { |
| 114 | boost::property_tree::read_ini(m_input, m_config); |
| 115 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 116 | catch (boost::property_tree::ini_parser_error& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 117 | { |
| 118 | std::stringstream msg; |
| 119 | msg << "Failed to parse configuration file"; |
| 120 | msg << " " << m_path; |
| 121 | msg << " " << error.message() << " line " << error.line(); |
| 122 | throw Error(msg.str()); |
| 123 | } |
| 124 | return m_config; |
| 125 | } |
| 126 | |
| 127 | } |