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 UTIL_H
17 #define UTIL_H
18
19 #include <limits>
20 #include <string>
21 #include <vector>
22
23 #include <sys/types.h>
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 int32_t GetPid();
29 const char* GetProgramName();
30 int64_t GetMillisTime();
31
32 uint64_t GetThisThreadId();
33
34 void SetThreadName(const std::string &name);
35 void GetTimeStamp(std::string &startTime);
36
37 template<typename T>
AddInt(T op1,T op2,T minValue,T maxValue,T & res)38 bool AddInt(T op1, T op2, T minValue, T maxValue, T &res)
39 {
40 if (op1 >= 0) {
41 if (op2 > maxValue - op1) {
42 return false;
43 }
44 } else {
45 if (op2 < minValue - op1) {
46 return false;
47 }
48 }
49 res = op1 + op2;
50 return true;
51 }
52
AddInt32(int32_t op1,int32_t op2,int32_t & res)53 inline bool AddInt32(int32_t op1, int32_t op2, int32_t &res)
54 {
55 return AddInt(op1, op2, std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::max(), res);
56 }
57
AddInt64(int64_t op1,int64_t op2,int64_t & res)58 inline bool AddInt64(int64_t op1, int64_t op2, int64_t &res)
59 {
60 return AddInt(op1, op2, std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max(), res);
61 }
62
63 template<typename T>
MultiplyInt(T op1,T op2,T minVal,T maxVal,T & res)64 bool MultiplyInt(T op1, T op2, T minVal, T maxVal, T &res)
65 {
66 if (op1 > 0) {
67 if (op2 > 0) {
68 if (op1 > maxVal / op2) {
69 return false;
70 }
71 } else {
72 if (op2 < minVal / op1) {
73 return false;
74 }
75 }
76 } else {
77 if (op2 > 0) {
78 if (op1 < minVal / op2) {
79 return false;
80 }
81 } else {
82 if (op1 != 0 && op2 < maxVal / op1) {
83 return false;
84 }
85 }
86 }
87 res = op1 * op2;
88 return true;
89 }
90
MultiplyInt32(int32_t op1,int32_t op2,int32_t & res)91 inline bool MultiplyInt32(int32_t op1, int32_t op2, int32_t& res)
92 {
93 return MultiplyInt(op1, op2, std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::max(), res);
94 }
95
MultiplyInt64(int64_t op1,int64_t op2,int64_t & res)96 inline bool MultiplyInt64(int64_t op1, int64_t op2, int64_t& res)
97 {
98 return MultiplyInt(op1, op2, std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max(), res);
99 }
100
101 size_t StringSplit(const std::string &str, const std::string &sep, std::vector<std::string> &vecList);
102 std::string GetAnonyString(const std::string &value);
103 std::string StringPrintf(const char *format, ...);
104 bool CheckFileExtendName(const std::string &filePath, const std::string &checkExtension);
105 bool IsValidPath(const std::string &rootDir, const std::string &filePath);
106 bool IsValidSvgPath(const std::string &filePath);
107 bool IsValidSvgFile(const std::string &filePath);
108 bool IsNum(const std::string &str);
109 } // namespace DeviceStatus
110 } // namespace Msdp
111 } // namespace OHOS
112 #endif // UTIL_H
113