blob: 994515af5e2db929c002920a64c5c4653b2273b5 [file] [log] [blame]
Václav Kubernát624a8872018-03-02 17:28:47 +01001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
5 *
6*/
7
8#include "CTree.hpp"
9
10InvalidNodeException::~InvalidNodeException() = default;
11
12std::string joinPaths(const std::string& prefix, const std::string& suffix)
13{
14 if (prefix.empty() || suffix.empty())
15 return prefix + suffix;
16 else
17 return prefix + '/' + suffix;
18}
19
20const std::unordered_set<std::string>& CTree::children(const std::string& node) const
21{
22 return m_nodes.at(node);
23}
24
25bool CTree::checkNode(const std::string& location, const std::string& node) const
26{
27 if (node == ".." || node.empty())
28 return true;
29 const auto& childrenRef = children(location); //first, get a reference to all children
30 if (childrenRef.find(node) == childrenRef.end()) { //find the desired node, if it isn't present throw an exception
31 throw InvalidNodeException(node);
32 }
33 return true;
34}
35void CTree::changeNode(const std::string& node)
36{
37 if (node.empty()) {
38 m_curDir = "";
39 return;
40 }
41 m_curDir += joinPaths(m_curDir, node);
42}
43std::string CTree::currentNode() const
44{
45 return m_curDir;
46}
47
48void CTree::addNode(const std::string& location, const std::string& name)
49{
50 m_nodes.at(location).insert(name);
51
52 //create a new set of children for the new node
53 m_nodes.emplace(joinPaths(location, name), std::unordered_set<std::string>());
54}
55void CTree::initDefault()
56{
57 m_nodes.emplace("", std::unordered_set<std::string>());
58 addNode("", "aaa");
59 addNode("", "bbb");
60 addNode("", "ccc");
61 addNode("aaa", "aaabbb");
62 addNode("aaa", "aaauuu");
63 addNode("bbb", "bbbuuu");
64}