Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame^] | 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include "CTree.hpp" |
| 10 | |
| 11 | InvalidNodeException::~InvalidNodeException() = default; |
| 12 | |
| 13 | std::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át | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame^] | 21 | bool TreeNode::operator<(const TreeNode& b) const |
| 22 | { |
| 23 | return this->m_name < b.m_name; |
| 24 | } |
| 25 | |
| 26 | CTree::CTree() |
| 27 | { |
| 28 | m_nodes.emplace("", std::unordered_map<std::string, NODE_TYPE>()); |
| 29 | } |
| 30 | |
| 31 | const std::unordered_map<std::string, NODE_TYPE>& CTree::children(const std::string& node) const |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 32 | { |
| 33 | return m_nodes.at(node); |
| 34 | } |
| 35 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame^] | 36 | bool CTree::nodeExists(const std::string& location, const std::string& node) const |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 37 | { |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame^] | 38 | if (node.empty()) |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 39 | return true; |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame^] | 40 | const auto& childrenRef = children(location); |
| 41 | |
| 42 | return childrenRef.find(node) != childrenRef.end(); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 43 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame^] | 44 | |
| 45 | bool 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 | |
| 52 | void 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át | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 62 | void 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 | } |
| 70 | std::string CTree::currentNode() const |
| 71 | { |
| 72 | return m_curDir; |
| 73 | } |