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 #ifndef UTIL_H
16 #define UTIL_H
17
18 #include <map>
19 #include <string>
20 #include <vector>
21
22 #include "define_multimodal.h"
23 #include "struct_multimodal.h"
24
25 namespace OHOS {
26
27 extern "C" {
28 int32_t ReadConfigInfo(const char* line, int32_t len, int32_t* element_key, int32_t* element_value);
29 }
30
31 namespace MMI {
32 struct DeviceConfig {
33 int32_t autoSwitch { 1 };
34 int32_t delayTime { 500 };
35 int32_t intervalTime { 50 };
36 int32_t keyboardType { 0 };
37 };
38
39 int64_t GetSysClockTime();
40
41 int64_t GetMillisTime();
42
43 uint64_t GetThisThreadId();
44
45 size_t StringSplit(const std::string &str, const std::string &sep, std::vector<std::string> &vecList);
46
47 std::string IdsListToString(const std::vector<int32_t> &list, const std::string &sep = ", ");
48
49 int32_t GetPid();
50
51 const char* GetProgramName();
52
53 void SetThreadName(const std::string &name);
54
55 void ReadProFile(const std::string &filePath, int32_t deviceId,
56 std::map<int32_t, std::map<int32_t, int32_t>> &configMap);
57
58 void ReadProConfigFile(const std::string &realPath, int32_t deviceId,
59 std::map<int32_t, std::map<int32_t, int32_t>> &configKey);
60
61 bool IsValidJsonPath(const std::string &filePath);
62
63 std::string ReadJsonFile(const std::string &filePath);
64
65 int32_t ReadCursorStyleFile(const std::string &filePath);
66
67 int32_t ReadTomlFile(const std::string &filePath, DeviceConfig &devConf);
68
69 std::string FileVerification(std::string &filePath, const std::string &checkExtension);
70
71 std::string StringPrintf(const char *format, ...);
72
RemoveSpace(std::string & str)73 inline void RemoveSpace(std::string &str)
74 {
75 str.erase(remove_if(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c);}), str.end());
76 }
77
78 template<typename T>
AddInt(T op1,T op2,T minVal,T maxVal,T & res)79 bool AddInt(T op1, T op2, T minVal, T maxVal, T &res)
80 {
81 if (op1 >= 0) {
82 if (op2 > maxVal - op1) {
83 return false;
84 }
85 } else {
86 if (op2 < minVal - op1) {
87 return false;
88 }
89 }
90 res = op1 + op2;
91 return true;
92 }
93
AddInt32(int32_t op1,int32_t op2,int32_t & res)94 inline bool AddInt32(int32_t op1, int32_t op2, int32_t &res)
95 {
96 return AddInt(op1, op2, INT32_MIN, INT32_MAX, res);
97 }
98
AddInt64(int64_t op1,int64_t op2,int64_t & res)99 inline bool AddInt64(int64_t op1, int64_t op2, int64_t &res)
100 {
101 return AddInt(op1, op2, INT64_MIN, INT64_MAX, res);
102 }
103 } // namespace MMI
104 } // namespace OHOS
105 #endif // UTIL_H
106