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 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 9 | #include <iostream> |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 10 | #include "CTree.hpp" |
Václav Kubernát | 94938b7 | 2018-05-04 15:12:24 +0200 | [diff] [blame] | 11 | #include "utils.hpp" |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 12 | |
| 13 | InvalidNodeException::~InvalidNodeException() = default; |
| 14 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 15 | struct nodeToString : public boost::static_visitor<std::string> { |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame^] | 16 | std::string operator()(const nodeup_&) const |
| 17 | { |
| 18 | return ".."; |
| 19 | } |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 20 | template <class T> |
| 21 | std::string operator()(const T& node) const |
| 22 | { |
| 23 | return node.m_name; |
| 24 | } |
| 25 | }; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 26 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 27 | |
| 28 | CTree::CTree() |
| 29 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 30 | m_nodes.emplace("", std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 33 | const std::unordered_map<std::string, NodeType>& CTree::children(const std::string& name) const |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 34 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 35 | return m_nodes.at(name); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 38 | bool CTree::nodeExists(const std::string& location, const std::string& name) const |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 39 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 40 | if (name.empty()) |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 41 | return true; |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 42 | const auto& childrenRef = children(location); |
| 43 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 44 | return childrenRef.find(name) != childrenRef.end(); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 45 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 46 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 47 | bool CTree::isContainer(const std::string& location, const std::string& name) const |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 48 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 49 | if (!nodeExists(location, name)) |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 50 | return false; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 51 | |
| 52 | return children(location).at(name).type() == typeid(schema::container); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void CTree::addContainer(const std::string& location, const std::string& name) |
| 56 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 57 | m_nodes.at(location).emplace(name, schema::container{}); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 58 | |
| 59 | //create a new set of children for the new node |
| 60 | std::string key = joinPaths(location, name); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 61 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 65 | bool CTree::listHasKey(const std::string& location, const std::string& name, const std::string& key) const |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 66 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 67 | 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 | |
| 74 | const 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 | |
| 83 | bool 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 | |
| 94 | void 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át | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 101 | void CTree::changeNode(const path_& name) |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 102 | { |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 103 | if (name.m_nodes.empty()) { |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 104 | m_curDir = ""; |
| 105 | return; |
| 106 | } |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 107 | for (const auto& it : name.m_nodes) { |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame^] | 108 | 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át | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 115 | } |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 116 | } |
| 117 | std::string CTree::currentNode() const |
| 118 | { |
| 119 | return m_curDir; |
| 120 | } |