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 #include <string>
22 #include <vector>
23
24 #include "mmi_log.h"
25 #include "define_multimodal.h"
26 #include "struct_multimodal.h"
27
28 namespace OHOS {
29
30 extern "C" {
31 int32_t ReadConfigInfo(const char* line, int32_t len, int32_t* element_key, int32_t* element_value);
32 }
33
34 namespace MMI {
35 struct DeviceConfig {
36 int32_t autoSwitch { 1 };
37 int32_t delayTime { 500 };
38 int32_t intervalTime { 50 };
39 int32_t keyboardType { 0 };
40 };
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 std::string FileVerification(std::string &filePath, const std::string &checkExtension);
73
74 std::string StringPrintf(const char *format, ...);
75
RemoveSpace(std::string & str)76 inline void RemoveSpace(std::string &str)
77 {
78 str.erase(remove_if(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c);}), str.end());
79 }
80
81 template<typename T>
AddInt(T op1,T op2,T minVal,T maxVal,T & res)82 bool AddInt(T op1, T op2, T minVal, T maxVal, T &res)
83 {
84 if (op1 >= 0) {
85 if (op2 > maxVal - op1) {
86 return false;
87 }
88 } else {
89 if (op2 < minVal - op1) {
90 return false;
91 }
92 }
93 res = op1 + op2;
94 return true;
95 }
96
AddInt32(int32_t op1,int32_t op2,int32_t & res)97 inline bool AddInt32(int32_t op1, int32_t op2, int32_t &res)
98 {
99 return AddInt(op1, op2, INT32_MIN, INT32_MAX, res);
100 }
101
AddInt64(int64_t op1,int64_t op2,int64_t & res)102 inline bool AddInt64(int64_t op1, int64_t op2, int64_t &res)
103 {
104 return AddInt(op1, op2, INT64_MIN, INT64_MAX, res);
105 }
106
107 template<typename T>
MMI_EQ(const T & x,const T & y)108 inline constexpr bool MMI_EQ(const T& x, const T& y)
109 {
110 if constexpr (std::is_floating_point<T>::value) {
111 return (std::abs((x) - (y)) <= (std::numeric_limits<T>::epsilon()));
112 } else {
113 return x == y;
114 }
115 }
116
117 template<typename T>
MMI_EQ(T x,T y,T epsilon)118 inline bool MMI_EQ(T x, T y, T epsilon)
119 {
120 return (std::abs((x) - (y)) <= (epsilon));
121 }
122
123 template<typename T>
MMI_EQ(const std::weak_ptr<T> & x,const std::weak_ptr<T> & y)124 inline bool MMI_EQ(const std::weak_ptr<T>& x, const std::weak_ptr<T>& y)
125 {
126 return !(x.owner_before(y) || y.owner_before(x));
127 }
128
MMI_LNE(float left,float right)129 inline bool MMI_LNE(float left, float right) //less not equal
130 {
131 constexpr float epsilon = -0.001f;
132 return (left - right) < epsilon;
133 }
134
MMI_GNE(float left,float right)135 inline bool MMI_GNE(float left, float right) //great not equal
136 {
137 constexpr float epsilon = 0.001f;
138 return (left - right) > epsilon;
139 }
140
MMI_GE(float left,float right)141 inline bool MMI_GE(float left, float right) //great or equal
142 {
143 constexpr float epsilon = -0.001f;
144 return (left - right) > epsilon;
145 }
146
MMI_LE(float left,float right)147 inline bool MMI_LE(float left, float right) //less or equal
148 {
149 constexpr float epsilon = 0.001f;
150 return (left - right) < epsilon;
151 }
152
MS2US(int64_t ms)153 inline constexpr int64_t MS2US(int64_t ms)
154 {
155 constexpr int64_t unit { 1000 };
156 return (ms * unit);
157 }
158 } // namespace MMI
159 } // namespace OHOS
160 #endif // UTIL_H
161