blob: c25018c03719c5471c25caa7995b7061add52a16 [file] [log] [blame]
Yingdi Yu92672512015-03-24 13:59:33 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California
4 *
5 * This file is part of NSL (NDN Signature Logger).
6 * See AUTHORS.md for complete list of NSL authors and contributors.
7 *
8 * NSL is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NSL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NSL, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of nsl authors and contributors.
20 */
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