1 /* 2 * Copyright (C) 2021 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 SP_UTILS_H 16 #define SP_UTILS_H 17 #include <iostream> 18 #include <vector> 19 #include <map> 20 #include <set> 21 #include <string> 22 #include <sstream> 23 namespace OHOS { 24 namespace SmartPerf { 25 class SPUtilesTye { 26 public: 27 template<typename T> StringToSometype(const std::string & str)28 static T StringToSometype(const std::string& str) 29 { 30 T result; 31 std::stringstream ss; 32 ss << str; 33 ss >> result; 34 return result; 35 } 36 }; 37 namespace SPUtils { 38 /** 39 * @brief Check if the file has permission to access 40 * 41 * @param fileName 42 * @return true 43 * @return false 44 */ 45 bool HasNumber(const std::string &str); 46 bool Cmp(const std::string &a, const std::string &b); 47 /** 48 * @brief Comparison key name 49 * 50 * @param a 51 * @param b 52 * @return true 53 * @return false 54 */ 55 bool FileAccess(const std::string &fileName); 56 /** 57 * @brief Load content from file node 58 * 59 * @param filePath 60 * @param content 61 * @return true 62 * @return false 63 */ 64 bool LoadFile(const std::string &filePath, std::string &content); 65 /** 66 * @brief read command return result 67 * 68 * @param cmd 69 * @param result 70 * @return true 71 * @return false 72 */ 73 bool LoadCmdWithLinkBreak(const std::string &cmd, bool isClearLinkBreak, std::string &result); 74 /** 75 * @brief 76 * 77 * @param path 78 * @return std::string 79 */ 80 81 bool LoadCmd(const std::string &cmd, std::string &result); 82 /** 83 * @brief 84 * 85 * @param path 86 * @return std::string 87 */ 88 std::string IncludePathDelimiter(const std::string &path); 89 /** 90 * @brief 91 * @param path 92 * @param files 93 */ 94 void ForDirFiles(const std::string &path, std::vector<std::string> &files); 95 96 /** 97 * @brief check if substr in parentstr 98 * 99 * @param str 100 * @param sub 101 * @return true 102 * @return false 103 */ 104 bool IsSubString(const std::string &str, const std::string &sub); 105 /** 106 * @brief split content by delimiter 107 * 108 * @param content 109 * @param sp 110 * @param out 111 */ 112 void StrSplit(const std::string &content, const std::string &sp, std::vector<std::string> &out); 113 /** 114 * @brief extract number from str 115 * 116 * @param str 117 * @return std::string 118 */ 119 std::string ExtractNumber(const std::string &str); 120 /** 121 * @brief replace '' \r\n from str 122 * @param res 123 */ 124 void ReplaceString(std::string &res); 125 /** 126 * @brief get cur Time longlong 127 * 128 */ 129 long long GetCurTime(); 130 /** 131 * @brief get top pkg 132 * 133 */ 134 std::string GetTopPkgName(); 135 std::string GetRadar(); 136 std::string GetRadarResponse(); 137 std::string GetRadarComplete(); 138 std::string GetRadarFrame(); 139 std::map<std::string, std::string> GetDeviceInfo(); 140 std::map<std::string, std::string> GetCpuInfo(bool isTcpMessage); 141 std::map<std::string, std::string> GetGpuInfo(bool isTcpMessage); 142 std::string GetDeviceInfoMap(); 143 std::string GetScreen(); 144 void RemoveSpace(std::string &str); 145 bool IntegerVerification(const std::string& str, std::string& errorInfo); 146 bool VeriyParameter(std::set<std::string>& keys, const std::string& param, std::string& errorInfo); 147 bool VeriyKey(std::set<std::string>& keys, std::map<std::string, std::string>& mapInfo, std::string& errorInfo); 148 bool VerifyValueStr(std::map<std::string, std::string>& mapInfo, std::string& errorInfo); 149 bool IntegerValueVerification(std::set<std::string> &keys, std::map<std::string, std::string> &mapInfo, 150 std::string &errorInfo); 151 bool RemSpaceAndTraPara(std::vector<std::string>& outParam, std::map<std::string, std::string>& mapInfo, 152 std::string &errorInfo); 153 bool IsInvalidInputfromComParam(const std::string& param, std::string &errorInfo); 154 bool IsHmKernel(); 155 std::string GetCpuNum(); 156 void GetCurrentTime(int prevTime); 157 bool IsForeGround(const std::string &pkg); 158 bool IsFindAbilist(); 159 void SetRkFlag(); 160 bool GetPathPermissions(const std::string &path); 161 bool GetIsGameApp(const std::string& pkg); 162 bool IsFindForeGround(const std::string &line); 163 std::string GetVersion(); 164 int& GetTtyDeviceFd(); 165 void GetTestsaPlugin(int command); 166 void KillStartDaemon(); 167 void CreateDir(const std::string &dirPath); 168 void RemoveDirOrFile(const std::string &dirPath); 169 void CopyFiles(const std::string& cpStr); 170 void TarFiles(const std::string& tarStr); 171 std::string ExecuteCommand(const std::string& command); 172 size_t GetFileSize(std::string filePath); 173 bool IsFindDHGame(const std::string &pkg); 174 std::string GetSurface(); 175 std::string GetProductName(); 176 }; 177 } 178 } 179 180 #endif // SP_UTILS_H 181