1 /* 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 21 namespace dbus 22 { 23 24 namespace utility 25 { 26 27 using DbusVariantType = sdbusplus::message::variant< 28 std::vector<std::tuple<std::string, std::string, std::string>>, 29 std::vector<std::string>, std::string, int64_t, uint64_t, double, int32_t, 30 uint32_t, int16_t, uint16_t, uint8_t, bool>; 31 32 using 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 38 inline 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 46 inline 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 88