blob: ffb8051146a4ecf9e43f294b3c5e06bd4eedba79 [file] [log] [blame]
Yingdi Yu92672512015-03-24 13:59:33 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07003 * Copyright (c) 2014-2017, Regents of the University of California
Yingdi Yu92672512015-03-24 13:59:33 -07004 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07005 * This file is part of NDN DeLorean, An Authentication System for Data Archives in
6 * Named Data Networking. See AUTHORS.md for complete list of NDN DeLorean authors
7 * and contributors.
Yingdi Yu92672512015-03-24 13:59:33 -07008 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07009 * NDN DeLorean is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
Yingdi Yu92672512015-03-24 13:59:33 -070013 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070014 * NDN DeLorean is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
16 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Yingdi Yu92672512015-03-24 13:59:33 -070017 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070018 * You should have received a copy of the GNU General Public License along with NDN
19 * DeLorean, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yingdi Yu92672512015-03-24 13:59:33 -070020 */
21
22#include "config-file.hpp"
23#include <boost/property_tree/info_parser.hpp>
24#include <boost/filesystem.hpp>
25namespace nsl {
26namespace conf {
27
28ConfigFile::ConfigFile(const std::string& filename)
29 : m_filename(filename)
30{
31}
32
33void
34ConfigFile::parse()
35{
36 std::ifstream input;
37 input.open(m_filename.c_str());
38 if (!input.good() || !input.is_open())
39 throw Error("Failed to read configuration file: " + m_filename);
40
41 ConfigSection configSection;
42 try {
43 boost::property_tree::read_info(input, configSection);
44 }
45 catch (boost::property_tree::info_parser_error& error) {
46 std::stringstream msg;
47 msg << "Failed to parse configuration file";
48 msg << " " << m_filename;
49 msg << " " << error.message() << " line " << error.line();
50 throw Error(msg.str());
51 }
52
53 bool hasLoggerName = false;
54 bool hasDbDir = false;
55 bool hasPolicy = false;
56 bool hasValidatorRule = false;
57 for (const auto& section : configSection) {
58 if (boost::iequals(section.first, "logger-name")) {
59 try {
60 m_loggerName = Name(section.second.data());
61 }
62 catch (Name::Error& e) {
63 throw Error("Wrong logger-name: " + section.second.data());
64 }
65 hasLoggerName = true;
66 }
67 else if (boost::iequals(section.first, "db-dir")) {
68 using namespace boost::filesystem;
69
70 m_dbDir = absolute(section.second.data(), path(m_filename).parent_path()).string();
71 hasDbDir = true;
72 }
73 else if (boost::iequals(section.first, "policy")) {
74 m_policy = section.second;
75 hasPolicy = true;
76 }
77 else if (boost::iequals(section.first, "validator")) {
78 m_validatorRule = section.second;
79 hasValidatorRule = true;
80 }
81 else
82 throw Error("Error in loading policy checker: unrecognized section " + section.first);
83 }
84
85 if (!hasDbDir) {
86 m_dbDir = boost::filesystem::path(m_filename).parent_path().string();
87 hasDbDir = true;
88 }
89
90 if (hasDbDir && hasLoggerName && hasPolicy && hasValidatorRule)
91 return;
92
93 throw Error("incomplete configuration");
94}
95
96} // namespace conf
97} // namespace nsl