1 /**
2 BAREOS® - Backup Archiving REcovery Open Sourced
3
4 Copyright (C) 2018-2019 Bareos GmbH & Co. KG
5
6 This program is Free Software; you can redistribute it and/or
7 modify it under the terms of version three of the GNU Affero General Public
8 License as published by the Free Software Foundation and included
9 in the file LICENSE.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Affero General Public License for more details.
15
16 You should have received a copy of the GNU Affero General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
20 */
21
22 #include "qualified_resource_name_type_converter.h"
23 #include "lib/ascii_control_characters.h"
24 #include "lib/bstringlist.h"
25
26 #include <sstream>
27 #include <algorithm>
28 #include <iterator>
29
30 template <class T1, class T2>
swapPairs(std::map<T1,T2> m)31 std::map<T2, T1> swapPairs(std::map<T1, T2> m)
32 {
33 std::map<T2, T1> m1;
34
35 for (auto&& item : m) { m1.emplace(item.second, item.first); }
36
37 return m1;
38 };
39
QualifiedResourceNameTypeConverter(const std::map<int,std::string> & map)40 QualifiedResourceNameTypeConverter::QualifiedResourceNameTypeConverter(
41 const std::map<int, std::string>& map)
42 : type_name_relation_map_(map), name_type_relation_map_(swapPairs(map))
43 {
44 return;
45 }
46
ResourceTypeToString(const int & r_type) const47 std::string QualifiedResourceNameTypeConverter::ResourceTypeToString(
48 const int& r_type) const
49 {
50 if (type_name_relation_map_.empty()) { return std::string(); }
51 if (type_name_relation_map_.find(r_type) == type_name_relation_map_.end()) {
52 return std::string();
53 }
54 return type_name_relation_map_.at(r_type);
55 }
56
StringToResourceType(const std::string & r_name) const57 int QualifiedResourceNameTypeConverter::StringToResourceType(
58 const std::string& r_name) const
59 {
60 if (name_type_relation_map_.empty()) { return -1; }
61 if (name_type_relation_map_.find(r_name) == name_type_relation_map_.end()) {
62 return -1;
63 }
64 return name_type_relation_map_.at(r_name);
65 }
66
ResourceToString(const std::string & name_of_resource,const int & r_type,const std::string separator,std::string & str_out) const67 bool QualifiedResourceNameTypeConverter::ResourceToString(
68 const std::string& name_of_resource,
69 const int& r_type,
70 const std::string separator,
71 std::string& str_out) const
72 {
73 std::string r_name = ResourceTypeToString(r_type);
74 if (r_name.empty()) { return false; }
75 str_out = r_name + separator + name_of_resource;
76 return true;
77 }
78
79
ResourceToString(const std::string & name_of_resource,const int & r_type,std::string & str_out) const80 bool QualifiedResourceNameTypeConverter::ResourceToString(
81 const std::string& name_of_resource,
82 const int& r_type,
83 std::string& str_out) const
84 {
85 std::string separator(1, AsciiControlCharacters::RecordSeparator());
86 return ResourceToString(name_of_resource, r_type, separator, str_out);
87 }
88
StringToResource(std::string & name_of_resource,int & r_type,const std::string & str_in) const89 bool QualifiedResourceNameTypeConverter::StringToResource(
90 std::string& name_of_resource,
91 int& r_type,
92 const std::string& str_in) const
93 {
94 BStringList string_list(str_in, AsciiControlCharacters::RecordSeparator());
95
96 if (string_list.size()
97 < 2) { /* minimum of parameters are name_of_resource and r_type */
98 return false;
99 }
100
101 /* convert resource type */
102 std::string r_type_str = string_list.at(0);
103 int r_type_temp = StringToResourceType(r_type_str);
104 if (r_type_temp == -1) { return false; }
105 r_type = r_type_temp;
106
107 name_of_resource = string_list.at(1);
108 return true;
109 }
110