10 #include "CommandRunner.h"
19 CommandRunner::CommandRunner()
21 DebugLog2("CommandRunner::CommandRunner()");
28 CommandRunner::~CommandRunner()
35 DebugLog2("CommandRunner::~CommandRunner()");
38 CommandRunner *CommandRunner::alloc()
40 return new CommandRunner();
43 CommandRunner *CommandRunner::init()
45 DebugLog2("CommandRunner::init()");
52 CommandRunner *CommandRunner::retain()
54 DebugLog2("CommandRunner::retain()");
60 CommandRunner *CommandRunner::autorelease()
62 DebugLog2("CommandRunner::autorelease()");
64 Object::autorelease();
68 void CommandRunner::setCommandPath(String *path)
70 DebugLog2("CommandRunner::setCommandPath()");
75 _command = path->retain();
79 void CommandRunner::setArguments(Array *args)
81 DebugLog2("CommandRunner::setArguments()");
86 _arguments = args->retain();
90 bool CommandRunner::readLine(String *line)
95 void CommandRunner::write(String *line)
99 unsigned __stdcall CommandRunner_run(void *arg)
101 ((CommandRunner *)arg)->run();
105 void CommandRunner::run()
107 DebugLog2("CommandRunner::run()");
109 AutoreleasePool *pool = AutoreleasePool::alloc()->init();
114 while ((_command != NULL) && (_state == ST_READY))
116 DebugLog3("pre processing");
119 Pipe *pipe = Pipe::alloc()->init();
122 Task *task = Task::alloc()->init();
123 task->setLaunchPath(_command);
126 if (_arguments != NULL)
128 task->setArguments(_arguments);
132 task->setStandardOutput(pipe->fileHandleForWriting());
133 task->setStandardError(pipe->fileHandleForWriting());
139 FileInputStream *fis = FileInputStream::fileInputStream(pipe->fileHandleForReading());
151 AutoreleasePool *pool2 = AutoreleasePool::alloc()->init();
154 bool done2 = readLine(fis->readLine());
160 done = (done2 || (_state == ST_DONE));
168 DebugLog3("post processing");
190 DebugLog2("CommandRunner::run() done.");
193 bool CommandRunner::start()
195 DebugLog2("CommandRunner::start()");
201 if (_state == ST_IDLE)
204 unsigned int uiThreadId;
206 h = (HANDLE)_beginthreadex(NULL, 0, CommandRunner_run, this, 0, &uiThreadId);
216 bool needSleep = false;
220 if (_state == ST_IDLE)
224 else if (_state == ST_RUN)
229 else if (_state == ST_READY)
237 ::Sleep(100); // 100 ms
250 void CommandRunner::stop()
252 DebugLog2("CommandRunner::stop()");
254 if (_state != ST_IDLE)
263 done = (_state == ST_IDLE);
270 DebugLog2("CommandRunner::stop() done.");
273 bool CommandRunner::isRunning()
277 result = (_state == ST_RUN);
282 const char *CommandRunner::className()
284 return "CommandRunner";