Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
| 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
| 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 9 | #include <iostream> |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 10 | #include "datastore_access.hpp" |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 11 | #include "interpreter.hpp" |
| 12 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 13 | struct leafDataToString : boost::static_visitor<std::string> { |
| 14 | std::string operator()(const enum_& data) const |
| 15 | { |
| 16 | return data.m_value; |
| 17 | } |
| 18 | template <typename T> |
| 19 | std::string operator()(const T& data) const |
| 20 | { |
| 21 | std::stringstream stream; |
| 22 | stream << data; |
| 23 | return stream.str(); |
| 24 | } |
| 25 | }; |
| 26 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 27 | void Interpreter::operator()(const commit_&) const |
| 28 | { |
| 29 | m_datastore.commitChanges(); |
| 30 | } |
| 31 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 32 | void Interpreter::operator()(const set_& set) const |
| 33 | { |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 34 | m_datastore.setLeaf(absolutePathFromCommand(set), set.m_data); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 37 | void Interpreter::operator()(const get_& get) const |
| 38 | { |
| 39 | auto items = m_datastore.getItems(absolutePathFromCommand(get)); |
| 40 | for (auto it : items) { |
| 41 | std::cout << it.first << " = " << boost::apply_visitor(leafDataToString(), it.second) << std::endl; |
| 42 | } |
| 43 | } |
| 44 | |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 45 | void Interpreter::operator()(const cd_& cd) const |
| 46 | { |
| 47 | m_parser.changeNode(cd.m_path); |
| 48 | } |
| 49 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 50 | void Interpreter::operator()(const create_& create) const |
| 51 | { |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 52 | m_datastore.createPresenceContainer(absolutePathFromCommand(create)); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void Interpreter::operator()(const delete_& delet) const |
| 56 | { |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 57 | m_datastore.deletePresenceContainer(absolutePathFromCommand(delet)); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 60 | void Interpreter::operator()(const ls_& ls) const |
| 61 | { |
| 62 | std::cout << "Possible nodes:" << std::endl; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame^] | 63 | auto recursion{Recursion::NonRecursive}; |
| 64 | for (auto it : ls.m_options) { |
| 65 | if (it == LsOption::Recursive) |
| 66 | recursion = Recursion::Recursive; |
| 67 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 68 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame^] | 69 | for (const auto& it : m_parser.availableNodes(ls.m_path, recursion)) |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 70 | std::cout << it << std::endl; |
| 71 | } |
| 72 | |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 73 | template <typename T> |
| 74 | std::string Interpreter::absolutePathFromCommand(const T& command) const |
| 75 | { |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 76 | if (command.m_path.m_scope == Scope::Absolute) |
| 77 | return "/" + pathToDataString(command.m_path); |
| 78 | else |
| 79 | return joinPaths(m_parser.currentNode(), pathToDataString(command.m_path)); |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 82 | std::string Interpreter::absolutePathFromCommand(const get_& get) const |
| 83 | { |
| 84 | if (!get.m_path) { |
| 85 | return m_parser.currentNode(); |
| 86 | } else if (get.m_path->m_scope == Scope::Absolute) { |
| 87 | return "/" + pathToDataString(*get.m_path); |
| 88 | } else { |
| 89 | return joinPaths(m_parser.currentNode(), pathToDataString(*get.m_path)); |
| 90 | } |
| 91 | } |
| 92 | |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 93 | Interpreter::Interpreter(Parser& parser, DatastoreAccess& datastore) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 94 | : m_parser(parser) |
| 95 | , m_datastore(datastore) |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 96 | { |
| 97 | } |