blob: 69afb585ef90d7d61cb4f26d7e7d44310b459958 [file] [log] [blame]
Václav Kubernát624a8872018-03-02 17:28:47 +01001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
Václav Kubernátd6662962018-03-22 17:41:33 +01003 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
Václav Kubernát624a8872018-03-02 17:28:47 +01004 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
Václav Kubernátb96eef72018-05-04 19:10:22 +02009#include <iostream>
Václav Kubernát624a8872018-03-02 17:28:47 +010010#include "CTree.hpp"
Václav Kubernát94938b72018-05-04 15:12:24 +020011#include "utils.hpp"
Václav Kubernát624a8872018-03-02 17:28:47 +010012
13InvalidNodeException::~InvalidNodeException() = default;
14
Václav Kubernátff2c9f62018-05-16 20:26:31 +020015struct nodeToString : public boost::static_visitor<std::string> {
Václav Kubernát60d6f292018-05-25 09:45:32 +020016 std::string operator()(const nodeup_&) const
17 {
18 return "..";
19 }
Václav Kubernátff2c9f62018-05-16 20:26:31 +020020 template <class T>
21 std::string operator()(const T& node) const
22 {
23 return node.m_name;
24 }
25};
Václav Kubernát624a8872018-03-02 17:28:47 +010026
Václav Kubernátd6662962018-03-22 17:41:33 +010027
28CTree::CTree()
29{
Václav Kubernátb96eef72018-05-04 19:10:22 +020030 m_nodes.emplace("", std::unordered_map<std::string, NodeType>());
Václav Kubernátd6662962018-03-22 17:41:33 +010031}
32
Václav Kubernátb96eef72018-05-04 19:10:22 +020033const std::unordered_map<std::string, NodeType>& CTree::children(const std::string& name) const
Václav Kubernát624a8872018-03-02 17:28:47 +010034{
Václav Kubernátb96eef72018-05-04 19:10:22 +020035 return m_nodes.at(name);
Václav Kubernát624a8872018-03-02 17:28:47 +010036}
37
Václav Kubernátb96eef72018-05-04 19:10:22 +020038bool CTree::nodeExists(const std::string& location, const std::string& name) const
Václav Kubernát624a8872018-03-02 17:28:47 +010039{
Václav Kubernátb96eef72018-05-04 19:10:22 +020040 if (name.empty())
Václav Kubernát624a8872018-03-02 17:28:47 +010041 return true;
Václav Kubernátd6662962018-03-22 17:41:33 +010042 const auto& childrenRef = children(location);
43
Václav Kubernátb96eef72018-05-04 19:10:22 +020044 return childrenRef.find(name) != childrenRef.end();
Václav Kubernát624a8872018-03-02 17:28:47 +010045}
Václav Kubernátd6662962018-03-22 17:41:33 +010046
Václav Kubernátb96eef72018-05-04 19:10:22 +020047bool CTree::isContainer(const std::string& location, const std::string& name) const
Václav Kubernátd6662962018-03-22 17:41:33 +010048{
Václav Kubernátb96eef72018-05-04 19:10:22 +020049 if (!nodeExists(location, name))
Václav Kubernátd6662962018-03-22 17:41:33 +010050 return false;
Václav Kubernátb96eef72018-05-04 19:10:22 +020051
52 return children(location).at(name).type() == typeid(schema::container);
Václav Kubernátd6662962018-03-22 17:41:33 +010053}
54
55void CTree::addContainer(const std::string& location, const std::string& name)
56{
Václav Kubernátb96eef72018-05-04 19:10:22 +020057 m_nodes.at(location).emplace(name, schema::container{});
Václav Kubernátd6662962018-03-22 17:41:33 +010058
59 //create a new set of children for the new node
60 std::string key = joinPaths(location, name);
Václav Kubernátb96eef72018-05-04 19:10:22 +020061 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernátd6662962018-03-22 17:41:33 +010062}
63
64
Václav Kubernátb96eef72018-05-04 19:10:22 +020065bool CTree::listHasKey(const std::string& location, const std::string& name, const std::string& key) const
Václav Kubernát624a8872018-03-02 17:28:47 +010066{
Václav Kubernátb96eef72018-05-04 19:10:22 +020067 assert(isList(location, name));
68
69 const auto& child = children(location).at(name);
70 const auto& list = boost::get<schema::list>(child);
71 return list.m_keys.find(key) != list.m_keys.end();
72}
73
74const std::set<std::string>& CTree::listKeys(const std::string& location, const std::string& name) const
75{
76 assert(isList(location, name));
77
78 const auto& child = children(location).at(name);
79 const auto& list = boost::get<schema::list>(child);
80 return list.m_keys;
81}
82
83bool CTree::isList(const std::string& location, const std::string& name) const
84{
85 if (!nodeExists(location, name))
86 return false;
87 const auto& child = children(location).at(name);
88 if (child.type() != typeid(schema::list))
89 return false;
90
91 return true;
92}
93
94void CTree::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
95{
96 m_nodes.at(location).emplace(name, schema::list{keys});
97
98 m_nodes.emplace(name, std::unordered_map<std::string, NodeType>());
99}
100
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200101void CTree::changeNode(const path_& name)
Václav Kubernátb96eef72018-05-04 19:10:22 +0200102{
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200103 if (name.m_nodes.empty()) {
Václav Kubernát624a8872018-03-02 17:28:47 +0100104 m_curDir = "";
105 return;
106 }
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200107 for (const auto& it : name.m_nodes) {
Václav Kubernát60d6f292018-05-25 09:45:32 +0200108 const std::string node = boost::apply_visitor(nodeToString(), it);
109 if (node == "..") {
110 m_curDir = stripLastNodeFromPath(m_curDir);
111 } else {
112 m_curDir = joinPaths(m_curDir, boost::apply_visitor(nodeToString(), it));
113 }
114
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200115 }
Václav Kubernát624a8872018-03-02 17:28:47 +0100116}
117std::string CTree::currentNode() const
118{
119 return m_curDir;
120}