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 <ctime>
19 #include <map>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23
24 #include "define_multimodal.h"
25 #include "struct_multimodal.h"
26
27 namespace OHOS {
28 namespace MMI {
29 struct DeviceConfig {
30 int32_t autoSwitch { 1 };
31 int32_t delayTime { 300 };
32 int32_t intervalTime { 100 };
33 int32_t keyboardType { 0 };
34 };
35 const char *GetMmiErrorTypeDesc(int32_t errorCodeEnum);
36 std::string UuIdGenerate();
37 int64_t GetMicrotime();
38 int64_t GetSysClockTime();
39 int64_t GetMillisTime();
40 std::string GetUUid();
41 std::string GetThisThreadIdOfString();
42 uint64_t GetThisThreadId();
43 size_t StringToken(std::string &str, const std::string &sep, std::string &token);
44 size_t StringSplit(const std::string &str, const std::string &sep, std::vector<std::string> &vecList);
45 std::string IdsListToString(const std::vector<int32_t> &list, const std::string &sep = ", ");
46 void LocalTime(tm &t, time_t curTime = 0);
47 std::string Strftime(const std::string &format = "%F %T", time_t curTime = 0);
48 void PrintEventJoyStickAxisInfo(const EventJoyStickAxis &r, const int32_t fd,
49 const int32_t abilityId, const int32_t focusId, const int64_t preHandlerTime);
50 void PrintWMSInfo(const std::string &str, const int32_t fd, const int32_t abilityId, const int32_t focusId);
51 int32_t GetPid();
52 std::string GetFileName(const std::string &strPath);
53 const char* GetProgramName();
54 char* MmiBasename(char* path);
55 void SetThreadName(const std::string &name);
56 const std::string &GetThreadName();
57 void AddId(std::vector<int32_t> &list, int32_t id);
58 size_t CalculateDifference(const std::vector<int32_t> &list1, std::vector<int32_t> &list2,
59 std::vector<int32_t> &difList);
60 void ReadProFile(const std::string &filePath, int32_t deviceId,
61 std::map<int32_t, std::map<int32_t, int32_t>> &configMap);
62 void ReadProConfigFile(const std::string &realPath, int32_t deviceId,
63 std::map<int32_t, std::map<int32_t, int32_t>> &configKey);
64 std::string StringFmt(const char* str, ...);
65 std::string ReadJsonFile(const std::string &filePath);
66 std::string ReadUinputToolFile(const std::string &filePath);
67 int32_t ReadCursorStyleFile(const std::string &filePath);
68 int32_t ReadTomlFile(const std::string &filePath, DeviceConfig &devConf);
69 int32_t ReadConfigFile(const std::string &realPath, DeviceConfig &devConf);
70 int32_t ConfigItemSwitch(const std::string &configItem, const std::string &value, DeviceConfig &devConf);
71 std::string StringPrintf(const char *format, ...);
IsNum(const std::string & str)72 inline bool IsNum(const std::string &str)
73 {
74 std::istringstream sin(str);
75 double num;
76 return (sin >> num) && sin.eof();
77 }
RemoveSpace(std::string & str)78 inline void RemoveSpace(std::string &str)
79 {
80 str.erase(remove_if(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c);}), str.end());
81 }
82 template <typename T>
83 bool AddInt(T op1, T op2, T minVal, T maxVal, T &res);
AddInt32(int32_t op1,int32_t op2,int32_t & res)84 inline bool AddInt32(int32_t op1, int32_t op2, int32_t &res)
85 {
86 return AddInt(op1, op2, INT32_MIN, INT32_MAX, res);
87 }
AddInt64(int64_t op1,int64_t op2,int64_t & res)88 inline bool AddInt64(int64_t op1, int64_t op2, int64_t &res)
89 {
90 return AddInt(op1, op2, INT64_MIN, INT64_MAX, res);
91 }
92 template<typename T>
AddInt(T op1,T op2,T minVal,T maxVal,T & res)93 bool AddInt(T op1, T op2, T minVal, T maxVal, T &res)
94 {
95 if (op1 >= 0) {
96 if (op2 > maxVal - op1) {
97 return false;
98 }
99 } else {
100 if (op2 < minVal - op1) {
101 return false;
102 }
103 }
104 res = op1 + op2;
105 return true;
106 }
107 } // namespace MMI
108 } // namespace OHOS
109 #endif // UTIL_H
110