blob: 358bc05aff91044901320b0d37e09fda1fa4c572 [file] [log] [blame]
Václav Kubernát94938b72018-05-04 15:12:24 +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*/
Václav Kubernát4108e0d2018-10-29 13:32:22 +01008#include <boost/algorithm/string/erase.hpp>
9#include <boost/algorithm/string/predicate.hpp>
Václav Kubernát94938b72018-05-04 15:12:24 +020010#include "utils.hpp"
11
12std::string joinPaths(const std::string& prefix, const std::string& suffix)
13{
Václav Kubernát50949e62018-08-30 12:50:08 +020014 if (prefix.empty() || suffix.empty() || prefix == "/")
Václav Kubernát94938b72018-05-04 15:12:24 +020015 return prefix + suffix;
16 else
17 return prefix + '/' + suffix;
18}
Václav Kubernát60d6f292018-05-25 09:45:32 +020019
20std::string stripLastNodeFromPath(const std::string& path)
21{
22 std::string res = path;
23 auto pos = res.find_last_of('/');
24 if (pos == res.npos)
25 res.clear();
26 else
27 res.erase(pos);
28 return res;
29}
Václav Kubernátebca2552018-06-08 19:06:02 +020030
Václav Kubernát2eaceb82018-10-08 19:56:30 +020031schemaPath_ pathWithoutLastNode(const schemaPath_& path)
Václav Kubernátebca2552018-06-08 19:06:02 +020032{
Václav Kubernát2eaceb82018-10-08 19:56:30 +020033 return schemaPath_{path.m_scope, decltype(schemaPath_::m_nodes)(path.m_nodes.begin(), path.m_nodes.end() - 1)};
Václav Kubernátebca2552018-06-08 19:06:02 +020034}
Václav Kubernát0b0272f2018-06-13 14:13:08 +020035
36std::string leafDataTypeToString(yang::LeafDataTypes type)
37{
38 switch (type) {
39 case yang::LeafDataTypes::String:
40 return "a string";
41 case yang::LeafDataTypes::Decimal:
42 return "a decimal";
43 case yang::LeafDataTypes::Bool:
44 return "a boolean";
45 case yang::LeafDataTypes::Int:
46 return "an integer";
47 case yang::LeafDataTypes::Uint:
48 return "an unsigned integer";
49 case yang::LeafDataTypes::Enum:
50 return "an enum";
51 default:
52 return "";
53 }
54}
Václav Kubernát744f57f2018-06-29 22:46:26 +020055
Václav Kubernát2eaceb82018-10-08 19:56:30 +020056std::string fullNodeName(const schemaPath_& location, const ModuleNodePair& pair)
Václav Kubernát744f57f2018-06-29 22:46:26 +020057{
58 if (!pair.first) {
59 return location.m_nodes.at(0).m_prefix.value().m_name + ":" + pair.second;
60 } else {
61 return pair.first.value() + ":" + pair.second;
62 }
63}
Václav Kubernát2eaceb82018-10-08 19:56:30 +020064
65std::string fullNodeName(const dataPath_& location, const ModuleNodePair& pair)
66{
67 return fullNodeName(dataPathToSchemaPath(location), pair);
68}
Václav Kubernát4108e0d2018-10-29 13:32:22 +010069
70std::set<std::string> filterAndErasePrefix(const std::set<std::string>& set, const std::string_view prefix)
71{
72 std::set<std::string> filtered;
73 std::copy_if(set.begin(), set.end(),
74 std::inserter(filtered, filtered.end()),
75 [prefix] (auto it) { return boost::starts_with(it, prefix); });
76
77 std::set<std::string> withoutPrefix;
78 std::transform(filtered.begin(), filtered.end(),
79 std::inserter(withoutPrefix, withoutPrefix.end()),
80 [prefix] (auto it) { boost::erase_first(it, prefix); return it; });
81 return withoutPrefix;
82}
83