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 <string>
21 #include <vector>
22
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 int64_t GetSysClockTime();
41
42 int64_t GetMillisTime();
43
44 uint64_t GetThisThreadId();
45
46 size_t StringSplit(const std::string &str, const std::string &sep, std::vector<std::string> &vecList);
47
48 std::string IdsListToString(const std::vector<int32_t> &list, const std::string &sep = ", ");
49
50 int32_t GetPid();
51
52 const char* GetProgramName();
53
54 void SetThreadName(const std::string &name);
55
56 void ReadProFile(const std::string &filePath, int32_t deviceId,
57 std::map<int32_t, std::map<int32_t, int32_t>> &configMap);
58
59 void ReadProConfigFile(const std::string &realPath, int32_t deviceId,
60 std::map<int32_t, std::map<int32_t, int32_t>> &configKey);
61
62 bool IsValidJsonPath(const std::string &filePath);
63
64 std::string ReadJsonFile(const std::string &filePath);
65
66 int32_t ReadCursorStyleFile(const std::string &filePath);
67
68 int32_t ReadTomlFile(const std::string &filePath, DeviceConfig &devConf);
69
70 std::string FileVerification(std::string &filePath, const std::string &checkExtension);
71
72 std::string StringPrintf(const char *format, ...);
73
RemoveSpace(std::string & str)74 inline void RemoveSpace(std::string &str)
75 {
76 str.erase(remove_if(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c);}), str.end());
77 }
78
79 template<typename T>
AddInt(T op1,T op2,T minVal,T maxVal,T & res)80 bool AddInt(T op1, T op2, T minVal, T maxVal, T &res)
81 {
82 if (op1 >= 0) {
83 if (op2 > maxVal - op1) {
84 return false;
85 }
86 } else {
87 if (op2 < minVal - op1) {
88 return false;
89 }
90 }
91 res = op1 + op2;
92 return true;
93 }
94
AddInt32(int32_t op1,int32_t op2,int32_t & res)95 inline bool AddInt32(int32_t op1, int32_t op2, int32_t &res)
96 {
97 return AddInt(op1, op2, INT32_MIN, INT32_MAX, res);
98 }
99
AddInt64(int64_t op1,int64_t op2,int64_t & res)100 inline bool AddInt64(int64_t op1, int64_t op2, int64_t &res)
101 {
102 return AddInt(op1, op2, INT64_MIN, INT64_MAX, res);
103 }
104
105 template<typename T>
MMI_EQ(const T & x,const T & y)106 inline constexpr bool MMI_EQ(const T& x, const T& y)
107 {
108 if constexpr (std::is_floating_point<T>::value) {
109 return (std::abs((x) - (y)) <= (std::numeric_limits<T>::epsilon()));
110 } else {
111 return x == y;
112 }
113 }
114
115 template<typename T>
MMI_EQ(T x,T y,T epsilon)116 inline bool MMI_EQ(T x, T y, T epsilon)
117 {
118 return (std::abs((x) - (y)) <= (epsilon));
119 }
120
121 template<typename T>
MMI_EQ(const std::weak_ptr<T> & x,const std::weak_ptr<T> & y)122 inline bool MMI_EQ(const std::weak_ptr<T>& x, const std::weak_ptr<T>& y)
123 {
124 return !(x.owner_before(y) || y.owner_before(x));
125 }
126
MMI_LNE(float left,float right)127 inline bool MMI_LNE(float left, float right) //less not equal
128 {
129 constexpr float epsilon = -0.001f;
130 return (left - right) < epsilon;
131 }
132
MMI_GNE(float left,float right)133 inline bool MMI_GNE(float left, float right) //great not equal
134 {
135 constexpr float epsilon = 0.001f;
136 return (left - right) > epsilon;
137 }
138
MMI_GE(float left,float right)139 inline bool MMI_GE(float left, float right) //great or equal
140 {
141 constexpr float epsilon = -0.001f;
142 return (left - right) > epsilon;
143 }
144
MMI_LE(float left,float right)145 inline bool MMI_LE(float left, float right) //less or equal
146 {
147 constexpr float epsilon = 0.001f;
148 return (left - right) < epsilon;
149 }
150 } // namespace MMI
151 } // namespace OHOS
152 #endif // UTIL_H
153