blob: e45bb9ac505cd06bc9dd55795493c2956ddd102a [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
Ed Tanousabf2add2019-01-22 16:40:12 -080027using DbusVariantType =
28 std::variant<std::vector<std::tuple<std::string, std::string, std::string>>,
29 std::vector<std::string>, std::vector<double>, std::string,
30 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
31 uint16_t, uint8_t, bool>;
James Feist5b4aa862018-08-16 14:07:01 -070032
33using ManagedObjectType = std::vector<
34 std::pair<sdbusplus::message::object_path,
35 boost::container::flat_map<
36 std::string,
37 boost::container::flat_map<std::string, DbusVariantType>>>>;
38
39inline void escapePathForDbus(std::string& path)
40{
41 const std::regex reg("[^A-Za-z0-9_/]");
42 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
43}
44
45// gets the string N strings deep into a path
46// i.e. /0th/1st/2nd/3rd
47inline bool getNthStringFromPath(const std::string& path, int index,
48 std::string& result)
49{
50 int count = 0;
51 auto first = path.begin();
52 auto last = path.end();
53 for (auto it = path.begin(); it < path.end(); it++)
54 {
55 // skip first character as it's either a leading slash or the first
56 // character in the word
57 if (it == path.begin())
58 {
59 continue;
60 }
61 if (*it == '/')
62 {
63 count++;
64 if (count == index)
65 {
66 first = it;
67 }
68 if (count == index + 1)
69 {
70 last = it;
71 break;
72 }
73 }
74 }
75 if (count < index)
76 {
77 return false;
78 }
79 if (first != path.begin())
80 {
81 first++;
82 }
83 result = path.substr(first - path.begin(), last - first);
84 return true;
85}
86
87} // namespace utility
88} // namespace dbus