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