-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShellEngine.hpp
43 lines (38 loc) · 1023 Bytes
/
ShellEngine.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef SHELLENGINE_HPP
#define SHELLENGINE_HPP
#include <stdio.h>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
class ShellEngine
{
public:
static int ExecuteShellCommand(const char* cmd, std::vector<std::string>& result)
{
int ret = -1;
result.clear();
FILE *pp = popen(cmd, "r"); //建立管道
if (pp != NULL)
{
char buffer[1024]; //设置一个合适的长度,以存储每一行输出
while (fgets(buffer, sizeof(buffer), pp) != NULL)
{
if (buffer[strlen(buffer) - 1] == '\n')
{
buffer[strlen(buffer) - 1] = '\0'; //去除换行符
}
result.push_back(buffer);
}
pclose(pp); //关闭管道
}
ret = result.size();
return ret;
}
static void ExecuteProcess(const char* cmd)
{
system(cmd);
}
};
#endif // SHELLENGINE_HPP