blob: 8677cc87f7b6e50944be84ee7e60cad030d73e9e [file] [log] [blame]
James Feist5b4aa862018-08-16 14:07:01 -07001/*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16#pragma once
17
18#include <regex>
19#include <sdbusplus/message.hpp>
20
21namespace dbus
22{
23
24namespace utility
25{
26
27using DbusVariantType = sdbusplus::message::variant<
28 std::vector<std::tuple<std::string, std::string, std::string>>,
James Feistb7a08d02018-12-11 14:55:37 -080029 std::vector<std::string>, std::vector<double>, std::string, int64_t,
30 uint64_t, double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>;
James Feist5b4aa862018-08-16 14:07:01 -070031
32using ManagedObjectType = std::vector<
33 std::pair<sdbusplus::message::object_path,
34 boost::container::flat_map<
35 std::string,
36 boost::container::flat_map<std::string, DbusVariantType>>>>;
37
38inline void escapePathForDbus(std::string& path)
39{
40 const std::regex reg("[^A-Za-z0-9_/]");
41 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
42}
43
44// gets the string N strings deep into a path
45// i.e. /0th/1st/2nd/3rd
46inline bool getNthStringFromPath(const std::string& path, int index,
47 std::string& result)
48{
49 int count = 0;
50 auto first = path.begin();
51 auto last = path.end();
52 for (auto it = path.begin(); it < path.end(); it++)
53 {
54 // skip first character as it's either a leading slash or the first
55 // character in the word
56 if (it == path.begin())
57 {
58 continue;
59 }
60 if (*it == '/')
61 {
62 count++;
63 if (count == index)
64 {
65 first = it;
66 }
67 if (count == index + 1)
68 {
69 last = it;
70 break;
71 }
72 }
73 }
74 if (count < index)
75 {
76 return false;
77 }
78 if (first != path.begin())
79 {
80 first++;
81 }
82 result = path.substr(first - path.begin(), last - first);
83 return true;
84}
85
86} // namespace utility
87} // namespace dbus