• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 <map>
20 #include <set>
21 
22 #include "mmi_log.h"
23 #include "define_multimodal.h"
24 #include "struct_multimodal.h"
25 
26 namespace OHOS {
27 
28 extern "C" {
29     int32_t ReadConfigInfo(const char* line, int32_t len, int32_t* element_key, int32_t* element_value);
30 }
31 
32 namespace MMI {
33 struct DeviceConfig {
34     int32_t autoSwitch { 1 };
35     int32_t delayTime { 500 };
36     int32_t intervalTime { 50 };
37     int32_t keyboardType { 0 };
38 };
39 
40 typedef void (*ScreenCaptureCallback)(int32_t pid, bool isStart);
41 
42 int64_t GetSysClockTime();
43 
44 int64_t GetMillisTime();
45 
46 int64_t GetTimeToMilli(int64_t timeDT);
47 
48 uint64_t GetThisThreadId();
49 
50 size_t StringSplit(const std::string &str, const std::string &sep, std::vector<std::string> &vecList);
51 
52 std::string IdsListToString(const std::vector<int32_t> &list, const std::string &sep = ", ");
53 
54 int32_t GetPid();
55 
56 const char* GetProgramName();
57 
58 void SetThreadName(const std::string &name);
59 
60 void ReadProFile(const std::string &filePath, int32_t deviceId,
61     std::map<int32_t, std::map<int32_t, int32_t>> &configMap);
62 
63 void ReadProConfigFile(const std::string &realPath, int32_t deviceId,
64     std::map<int32_t, std::map<int32_t, int32_t>> &configKey);
65 
66 bool IsValidJsonPath(const std::string &filePath);
67 
68 std::string ReadJsonFile(const std::string &filePath);
69 
70 int32_t ReadCursorStyleFile(const std::string &filePath);
71 
72 int32_t ReadTomlFile(const std::string &filePath, DeviceConfig &devConf);
73 
74 bool ReadFile(const std::string &path, std::string &content);
75 
76 std::string IntToHexRGB(int32_t color);
77 
78 void StringReplace(std::string &str, const std::string &pattern, const std::string &to);
79 
80 std::string FileVerification(std::string &filePath, const std::string &checkExtension);
81 
82 std::string StringPrintf(const char *format, ...);
83 
RemoveSpace(std::string & str)84 inline void RemoveSpace(std::string &str)
85 {
86     str.erase(remove_if(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c);}), str.end());
87 }
88 
89 template<typename T>
AddInt(T op1,T op2,T minVal,T maxVal,T & res)90 bool AddInt(T op1, T op2, T minVal, T maxVal, T &res)
91 {
92     if (op1 >= 0) {
93         if (op2 > maxVal - op1) {
94             return false;
95         }
96     } else {
97         if (op2 < minVal - op1) {
98             return false;
99         }
100     }
101     res = op1 + op2;
102     return true;
103 }
104 
AddInt32(int32_t op1,int32_t op2,int32_t & res)105 inline bool AddInt32(int32_t op1, int32_t op2, int32_t &res)
106 {
107     return AddInt(op1, op2, INT32_MIN, INT32_MAX, res);
108 }
109 
AddInt64(int64_t op1,int64_t op2,int64_t & res)110 inline bool AddInt64(int64_t op1, int64_t op2, int64_t &res)
111 {
112     return AddInt(op1, op2, INT64_MIN, INT64_MAX, res);
113 }
114 
115 template<typename T>
MMI_EQ(const T & x,const T & y)116 inline constexpr bool MMI_EQ(const T& x, const T& y)
117 {
118     if constexpr (std::is_floating_point<T>::value) {
119         return (std::abs((x) - (y)) <= (std::numeric_limits<T>::epsilon()));
120     } else {
121         return x == y;
122     }
123 }
124 
125 template<typename T>
MMI_EQ(T x,T y,T epsilon)126 inline bool MMI_EQ(T x, T y, T epsilon)
127 {
128     return (std::abs((x) - (y)) <= (epsilon));
129 }
130 
131 template<typename T>
MMI_EQ(const std::weak_ptr<T> & x,const std::weak_ptr<T> & y)132 inline bool MMI_EQ(const std::weak_ptr<T>& x, const std::weak_ptr<T>& y)
133 {
134     return !(x.owner_before(y) || y.owner_before(x));
135 }
136 
MMI_LNE(float left,float right)137 inline bool MMI_LNE(float left, float right) //less not equal
138 {
139     constexpr float epsilon = -0.001f;
140     return (left - right) < epsilon;
141 }
142 
MMI_GNE(float left,float right)143 inline bool MMI_GNE(float left, float right) //great not equal
144 {
145     constexpr float epsilon = 0.001f;
146     return (left - right) > epsilon;
147 }
148 
MMI_GE(float left,float right)149 inline bool MMI_GE(float left, float right) //great or equal
150 {
151     constexpr float epsilon = -0.001f;
152     return (left - right) > epsilon;
153 }
154 
MMI_LE(float left,float right)155 inline bool MMI_LE(float left, float right) //less or equal
156 {
157     constexpr float epsilon = 0.001f;
158     return (left - right) < epsilon;
159 }
160 
MS2US(int64_t ms)161 inline constexpr int64_t MS2US(int64_t ms)
162 {
163     constexpr int64_t unit { 1000 };
164     return (ms * unit);
165 }
166 
167 template<typename, typename = std::void_t<>>
168 struct IsStreamable : public std::false_type {};
169 
170 template<typename T>
171 struct IsStreamable<T, std::void_t<decltype(operator<<(std::declval<std::ostream>(), std::declval<T>()))>>
172     : public std::true_type {};
173 
174 template<typename T>
175 std::enable_if_t<IsStreamable<T>::value, std::string> DumpSet(const std::set<T> &items)
176 {
177     std::ostringstream sItems;
178 
179     if (auto iter = items.cbegin(); iter != items.cend()) {
180         sItems << *iter;
181         for (++iter; iter != items.cend(); ++iter) {
182             sItems << "," << *iter;
183         }
184     }
185     return std::move(sItems).str();
186 }
187 
188 bool IsInteger(const char *target);
189 } // namespace MMI
190 } // namespace OHOS
191 #endif // UTIL_H
192