Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame^] | 1 | /* |
| 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 | |
| 10 | InvalidNodeException::~InvalidNodeException() = default; |
| 11 | |
| 12 | std::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 | |
| 20 | const std::unordered_set<std::string>& CTree::children(const std::string& node) const |
| 21 | { |
| 22 | return m_nodes.at(node); |
| 23 | } |
| 24 | |
| 25 | bool 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 | } |
| 35 | void 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 | } |
| 43 | std::string CTree::currentNode() const |
| 44 | { |
| 45 | return m_curDir; |
| 46 | } |
| 47 | |
| 48 | void 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 | } |
| 55 | void 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 | } |