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 
63     static bool DoesFileExist(const char *path);
64     static ssize_t GetFileSize(const char *path);
65     static ssize_t GetFileSize(const std::string &filePath);
66 
67     static void ShowFileAttributes(const char *path);
68     static void ShowUserAndGroup();
69 
70     static int64_t GetSysClockTime();
71 };
72 
73 inline bool Utility::IsEmpty(const char *str) noexcept
74 {
75     return ((str == nullptr) || (str[0] == '\0'));
76 }
77 
78 inline bool Utility::IsEqual(const char *s1, const char *s2) noexcept
79 {
80     if (IsEmpty(s1)) {
81         return IsEmpty(s2);
82     } else if (IsEmpty(s2)) {
83         return false;
84     }
85     return (std::strcmp(s1, s2) == 0);
86 }
87 
88 inline std::string Utility::Anonymize(const std::string &id)
89 {
90     return Anonymize(id.c_str());
91 }
92 } // namespace DeviceStatus
93 } // namespace Msdp
94 } // namespace OHOS
95 #endif // UTILITY_H