-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpoExecute.cpp
More file actions
115 lines (89 loc) · 3.01 KB
/
poExecute.cpp
File metadata and controls
115 lines (89 loc) · 3.01 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Created by Ilan Schifter on 4/16/13.
* Copyright 2013 Potion Design LLC . All rights reserved.
*/
#include "poExecute.h"
#include "poMessageCenter.h"
const string poExecute::EXE_DONE = "DONE_EXECUTING_COMMAND";
//----------------------------------------------------------------------------
poExecute::poExecute(string _command, bool _blocking, string _expected_stdout):
command(_command),
blocking(_blocking),
expected_stdout(_expected_stdout)
{
exec_count = 0;
}
//----------------------------------------------------------------------------
poExecute::~poExecute()
{
}
//----------------------------------------------------------------------------
bool poExecute::execute(){
++exec_count;
bool succeeded = true;
string data;
if(blocking){
FILE *stream;
stream = popen(command.c_str(), "r");
succeeded = stream != NULL;
if(succeeded){
char buffer[1024];
while ( fgets(buffer, 1024, stream) != NULL )
data.append(buffer);
if(data != expected_stdout){
printf("Failed to execute: %s\nSTDOUT: %s\n", command.c_str(), data.c_str());
succeeded = false;
}
}
pclose(stream);
}
else {
succeeded = popen(command.c_str(), "r") != NULL;
}
poDictionary dict;
dict.set("command", command);
dict.set("stdout", data);
dict.set("succeeded", succeeded);
poMessageCenter::broadcastMessage(EXE_DONE, this, dict);
--exec_count;
return succeeded;
}
//----------------------------------------------------------------------------
void poExecute::update()
{
}
//----------------------------------------------------------------------------
void poExecute::eventHandler(poEvent *event)
{
}
//----------------------------------------------------------------------------
void poExecute::messageHandler(const std::string &msg, const poDictionary& dict, const poObject *sender)
{
}
//----------------------------------------------------------------------------
int poExecute::numCurExecuting(){
return exec_count;
}
//----------------------------------------------------------------------------
void poExecute::setCommand(string _command){
command = _command;
}
//----------------------------------------------------------------------------
string poExecute::getCommand(){
return command;
}
//----------------------------------------------------------------------------
void poExecute::getExpectedStdout(string _expected_stdout){
expected_stdout = _expected_stdout;
}
//----------------------------------------------------------------------------
string poExecute::getExpectedStdout(){
return expected_stdout;
}
//----------------------------------------------------------------------------
void poExecute::setBlocking(bool b){
blocking = b;
}
//----------------------------------------------------------------------------
bool poExecute::isBlocking(){
return blocking;
}