blob: 4e5d6653da1ddacd9a53a6905584a71280eee8c4 [file] [log] [blame]
Václav Kubernát96344a12018-05-28 16:33:39 +02001/*
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átb61336d2018-05-28 17:35:03 +02009#include <iostream>
Václav Kubernát6415b822018-08-22 17:40:01 +020010#include "datastore_access.hpp"
Václav Kubernát96344a12018-05-28 16:33:39 +020011#include "interpreter.hpp"
12
Václav Kubernátebca2552018-06-08 19:06:02 +020013struct 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át812ee282018-08-30 17:10:03 +020027void Interpreter::operator()(const commit_&) const
28{
29 m_datastore.commitChanges();
30}
31
Václav Kubernát07204242018-06-04 18:12:09 +020032void Interpreter::operator()(const set_& set) const
33{
Václav Kubernát6415b822018-08-22 17:40:01 +020034 m_datastore.setLeaf(absolutePathFromCommand(set), set.m_data);
Václav Kubernát07204242018-06-04 18:12:09 +020035}
36
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020037void 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át96344a12018-05-28 16:33:39 +020045void Interpreter::operator()(const cd_& cd) const
46{
47 m_parser.changeNode(cd.m_path);
48}
49
Václav Kubernátb61336d2018-05-28 17:35:03 +020050void Interpreter::operator()(const create_& create) const
51{
Václav Kubernát6415b822018-08-22 17:40:01 +020052 m_datastore.createPresenceContainer(absolutePathFromCommand(create));
Václav Kubernátb61336d2018-05-28 17:35:03 +020053}
54
55void Interpreter::operator()(const delete_& delet) const
56{
Václav Kubernát6415b822018-08-22 17:40:01 +020057 m_datastore.deletePresenceContainer(absolutePathFromCommand(delet));
Václav Kubernátb61336d2018-05-28 17:35:03 +020058}
59
Václav Kubernát11afac72018-07-18 14:59:53 +020060void Interpreter::operator()(const ls_& ls) const
61{
62 std::cout << "Possible nodes:" << std::endl;
63
64 for (const auto& it : m_parser.availableNodes(ls.m_path))
65 std::cout << it << std::endl;
66}
67
Václav Kubernát6415b822018-08-22 17:40:01 +020068template <typename T>
69std::string Interpreter::absolutePathFromCommand(const T& command) const
70{
Václav Kubernát37171a12018-08-31 17:01:48 +020071 if (command.m_path.m_scope == Scope::Absolute)
72 return "/" + pathToDataString(command.m_path);
73 else
74 return joinPaths(m_parser.currentNode(), pathToDataString(command.m_path));
Václav Kubernát6415b822018-08-22 17:40:01 +020075}
76
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020077std::string Interpreter::absolutePathFromCommand(const get_& get) const
78{
79 if (!get.m_path) {
80 return m_parser.currentNode();
81 } else if (get.m_path->m_scope == Scope::Absolute) {
82 return "/" + pathToDataString(*get.m_path);
83 } else {
84 return joinPaths(m_parser.currentNode(), pathToDataString(*get.m_path));
85 }
86}
87
Václav Kubernát6415b822018-08-22 17:40:01 +020088Interpreter::Interpreter(Parser& parser, DatastoreAccess& datastore)
Václav Kubernát812ee282018-08-30 17:10:03 +020089 : m_parser(parser)
90 , m_datastore(datastore)
Václav Kubernát96344a12018-05-28 16:33:39 +020091{
92}