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