1 /*
2  * Copyright (C) 2023 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 #include <fstream>
16 #include "hidumper_test_utils.h"
17 
18 using namespace std;
19 namespace OHOS {
20 namespace HiviewDFX {
HidumperTestUtils()21 HidumperTestUtils::HidumperTestUtils()
22 {
23 }
24 
~HidumperTestUtils()25 HidumperTestUtils::~HidumperTestUtils()
26 {
27 }
28 
IsExistInCmdResult(const std::string & cmd,const std::string & str)29 bool HidumperTestUtils::IsExistInCmdResult(const std::string &cmd, const std::string &str)
30 {
31     bool res = false;
32     size_t len = 0;
33     FILE *fp = popen(cmd.c_str(), "r");
34     char* buffer = nullptr;
35     while (getline(&buffer, &len, fp) != -1) {
36         std::string line = buffer;
37         if (line.find(str) != string::npos) {
38             res = true;
39             break;
40         }
41     }
42     pclose(fp);
43     return res;
44 }
45 
IsExistStrInFile(const std::string & cmd,const std::string & str,const std::string & filePath)46 bool HidumperTestUtils::IsExistStrInFile(const std::string &cmd, const std::string &str, const std::string &filePath)
47 {
48     bool res = false;
49     FILE *fp = popen(cmd.c_str(), "r");
50     pclose(fp);
51 
52     std::ifstream file(filePath);
53     if (!file.is_open()) {
54         return res;
55     }
56     std::string line;
57     while (std::getline(file, line)) {
58         if (line.find(str) != string::npos) {
59             res = true;
60             break;
61         }
62     }
63     file.close();
64     return res;
65 }
66 } // namespace HiviewDFX
67 } // namespace OHOS
68