1 /* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef UTILITY_H 16 #define UTILITY_H 17 18 #include <cstring> 19 #include <sstream> 20 #include <string> 21 #include <type_traits> 22 23 namespace OHOS { 24 namespace Msdp { 25 namespace DeviceStatus { 26 27 template<typename, typename = std::void_t<>> 28 struct IsStreamable : public std::false_type {}; 29 30 template<typename T> 31 struct IsStreamable<T, std::void_t<decltype(operator<<(std::declval<std::ostream>(), std::declval<T>()))>> 32 : public std::true_type {}; 33 34 class Utility { 35 public: 36 static size_t CopyNulstr(char *dest, size_t size, const char *src); 37 static bool StartWith(const char *str, const char *prefix); 38 static bool StartWith(const std::string &str, const std::string &prefix); 39 40 static void RemoveTrailingChars(char c, char *path); 41 static void RemoveTrailingChars(const std::string &toRemoved, std::string &path); 42 static bool IsEmpty(const char *str); 43 static bool IsEqual(const char *s1, const char *s2); 44 45 template <typename... Args, 46 typename = std::enable_if_t<(IsStreamable<Args>::value && ...), std::string>> 47 static std::string ConcatAsString(Args&&... args) 48 { 49 std::ostringstream ss; 50 (..., (ss << std::forward<Args>(args))); 51 return ss.str(); 52 } 53 54 static void RemoveSpace(std::string &str); 55 static bool IsInteger(const std::string &target); 56 static bool DoesFileExist(const char *path); 57 static ssize_t GetFileSize(const char *path); 58 59 static void ShowFileAttributes(const char *path); 60 static void ShowUserAndGroup(); 61 }; 62 63 inline bool Utility::IsEmpty(const char *str) 64 { 65 return ((str == nullptr) || (str[0] == '\0')); 66 } 67 68 inline bool Utility::IsEqual(const char *s1, const char *s2) 69 { 70 if (IsEmpty(s1)) { 71 return IsEmpty(s2); 72 } else if (IsEmpty(s2)) { 73 return false; 74 } 75 return (strcmp(s1, s2) == 0); 76 } 77 } // namespace DeviceStatus 78 } // namespace Msdp 79 } // namespace OHOS 80 #endif // UTILITY_H