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 16 #ifndef UTILITY_H 17 #define UTILITY_H 18 19 #include <cstring> 20 #include <map> 21 #include <sstream> 22 #include <string> 23 #include <type_traits> 24 25 namespace OHOS { 26 class Parcel; 27 namespace Msdp { 28 namespace DeviceStatus { 29 30 template<typename, typename = std::void_t<>> 31 struct IsStreamable : public std::false_type {}; 32 33 template<typename T> 34 struct IsStreamable<T, std::void_t<decltype(operator<<(std::declval<std::ostream>(), std::declval<T>()))>> 35 : public std::true_type {}; 36 37 class Utility { 38 public: 39 static size_t CopyNulstr(char *dest, size_t size, const char *src); 40 static bool StartWith(const char *str, const char *prefix); 41 static bool StartWith(const std::string &str, const std::string &prefix); 42 43 static void RemoveTrailingChars(char c, char *path); 44 static void RemoveTrailingChars(const std::string &toRemoved, std::string &path); 45 static bool IsEmpty(const char *str) noexcept; 46 static bool IsEqual(const char *s1, const char *s2) noexcept; 47 48 template <typename... Args, 49 typename = std::enable_if_t<(IsStreamable<Args>::value && ...), std::string>> 50 static std::string ConcatAsString(Args&&... args) 51 { 52 std::ostringstream ss; 53 (..., (ss << std::forward<Args>(args))); 54 return ss.str(); 55 } 56 57 static void RemoveSpace(std::string &str); 58 static bool IsInteger(const std::string &target); 59 60 static std::string Anonymize(const std::string &id); 61 static std::string Anonymize(const char *id); 62 static std::string DragRadarAnonymize(const char* id); 63 64 static bool DoesFileExist(const char *path); 65 static ssize_t GetFileSize(const char *path); 66 static ssize_t GetFileSize(const std::string &filePath); 67 68 static void ShowFileAttributes(const char *path); 69 static void ShowUserAndGroup(); 70 71 static int64_t GetSysClockTime(); 72 }; 73 74 inline bool Utility::IsEmpty(const char *str) noexcept 75 { 76 return ((str == nullptr) || (str[0] == '\0')); 77 } 78 79 inline bool Utility::IsEqual(const char *s1, const char *s2) noexcept 80 { 81 if (IsEmpty(s1)) { 82 return IsEmpty(s2); 83 } else if (IsEmpty(s2)) { 84 return false; 85 } 86 return (std::strcmp(s1, s2) == 0); 87 } 88 89 inline std::string Utility::Anonymize(const std::string &id) 90 { 91 return Anonymize(id.c_str()); 92 } 93 } // namespace DeviceStatus 94 } // namespace Msdp 95 } // namespace OHOS 96 #endif // UTILITY_H