/* * Copyright (C) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef TEST_COMMON_H #define TEST_COMMON_H #include "file_utils.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "include/securec.h" #include "string_util.h" using namespace OHOS::HiviewDFX; int ExecCmdWithRet(std::string cmd, std::vector &resvec) { if (cmd.size() == 0) { return 0; } std::cout<< "cmd is " + cmd < cmdret; ExecCmdWithRet(cmd, cmdret); } void CmdRun(std::string cmd, std::string &result) { std::vector cmdret; int resultlen; int i = 0; std::string rst; resultlen = ExecCmdWithRet(cmd, cmdret); while (i < resultlen) { rst = rst + cmdret[i]; i = i + 1; } result = rst; } void CleanCmd() { std::string cmdResult; std::string cleanCmd = "hilog -r"; CmdRun(cleanCmd, cmdResult); std::cout << cmdResult; sleep(1); } std::string ExecuteCmd(std::string cmd) { std::vector cmdret; int resultlen; int i = 0; std::string rst; resultlen = ExecCmdWithRet(cmd, cmdret); while (i < resultlen) { rst = rst + cmdret[i] + "\n"; i = i + 1; } return rst; } void SaveCmdOutput(std::string cmd, std::string saveFile) { std::fstream fstr(saveFile, std::ios::out); std::string cmdRet = ExecuteCmd(cmd); fstr << cmdRet; fstr.close(); } void RedirecthiLog(std::string &hilogredirect, std::string &timeout) { unsigned long i; std::vector cmdret; unsigned long cmdretlen; std::string cmd = "rm " + hilogredirect; cmdretlen = ExecCmdWithRet(cmd, cmdret); for (i = 0; i < cmdretlen; i++) { std::cout<" + hilogredirect; std::cout< ¶, std::string info) { if (info.empty()) { return false; } std::vector splitStr; StringUtil::SplitStr(info, "\n", splitStr, false, false); unsigned long matchcnt; bool result = false; std::string eventinfoline; std::cout<::iterator iter = splitStr.begin(); iter != splitStr.end(); ++iter) { eventinfoline = std::string (iter->c_str()); for (unsigned long i = 0; i < para.size(); i++) { std::cout<= 0) { matchcnt++; } } std::cout<<"Expect1:"<= 0) { len--; } if (len >= 0 && x[len] > y[len]) { return false; } return true; } int GetTxtLine(std::string filename) { FILE *fd = fopen(filename.c_str(), "r"); int count = 0; if (fd != nullptr) { while (!feof(fd)) { if (fgetc(fd) == '\n') { count++; } } } std::cout << count << std::endl; if (fd != nullptr) { fclose(fd); } return count; } std::string ReadFile(std::string filename) { std::ifstream ifile(filename); std::ostringstream buf; char ch; if (ifile.fail()) { std::cout<<"open file fail!"< getfileinpath(std::string path) { std::vector filelist; DIR *dir; struct dirent *pdirent; dir = opendir(path.c_str()); if (dir != nullptr) { while ((pdirent = readdir(dir)) != nullptr) { if (strncmp(pdirent->d_name, ".", strlen(pdirent->d_name)) == 0 || strncmp(pdirent->d_name, "..", strlen(pdirent->d_name)) == 0) { continue; } else if (pdirent->d_type == 4) { continue; } else { filelist.push_back(pdirent->d_name); } } closedir(dir); } return filelist; } #endif