blob: 7d89cf37e4db694dece73934eb8e819bbc07e45b [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
9#include "CTree.hpp"
10
11InvalidNodeException::~InvalidNodeException() = default;
12
13std::string joinPaths(const std::string& prefix, const std::string& suffix)
14{
15 if (prefix.empty() || suffix.empty())
16 return prefix + suffix;
17 else
18 return prefix + '/' + suffix;
19}
20
Václav Kubernátd6662962018-03-22 17:41:33 +010021bool TreeNode::operator<(const TreeNode& b) const
22{
23 return this->m_name < b.m_name;
24}
25
26CTree::CTree()
27{
28 m_nodes.emplace("", std::unordered_map<std::string, NODE_TYPE>());
29}
30
31const std::unordered_map<std::string, NODE_TYPE>& CTree::children(const std::string& node) const
Václav Kubernát624a8872018-03-02 17:28:47 +010032{
33 return m_nodes.at(node);
34}
35
Václav Kubernátd6662962018-03-22 17:41:33 +010036bool CTree::nodeExists(const std::string& location, const std::string& node) const
Václav Kubernát624a8872018-03-02 17:28:47 +010037{
Václav Kubernátd6662962018-03-22 17:41:33 +010038 if (node.empty())
Václav Kubernát624a8872018-03-02 17:28:47 +010039 return true;
Václav Kubernátd6662962018-03-22 17:41:33 +010040 const auto& childrenRef = children(location);
41
42 return childrenRef.find(node) != childrenRef.end();
Václav Kubernát624a8872018-03-02 17:28:47 +010043}
Václav Kubernátd6662962018-03-22 17:41:33 +010044
45bool CTree::isContainer(const std::string& location, const std::string& node) const
46{
47 if (!nodeExists(location, node))
48 return false;
49 return children(location).at(node) == TYPE_CONTAINER;
50}
51
52void CTree::addContainer(const std::string& location, const std::string& name)
53{
54 m_nodes.at(location).emplace(name, TYPE_CONTAINER);
55
56 //create a new set of children for the new node
57 std::string key = joinPaths(location, name);
58 m_nodes.emplace(key, std::unordered_map<std::string, NODE_TYPE>());
59}
60
61
Václav Kubernát624a8872018-03-02 17:28:47 +010062void CTree::changeNode(const std::string& node)
63{
64 if (node.empty()) {
65 m_curDir = "";
66 return;
67 }
68 m_curDir += joinPaths(m_curDir, node);
69}
70std::string CTree::currentNode() const
71{
72 return m_curDir;
73}