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