#include "ProcessUtilities.h" #include "HostPlatform.h" #if defined(HOST_IS_WINDOWS) #include "ProcessUtilities.win.cc" #elif defined(HOST_IS_POSIX) #include "ProcessUtilities.posix.cc" #endif #include "FileSystemUtilities.h" /* PROCESSWATHCER */ ProcessWatcher::~ProcessWatcher() { } /* PROCESSCONTROLLER */ ProcessController::~ProcessController() { } void ProcessController::add(ProcessWatcher* aWatch) { Public.push_back(aWatch); } void ProcessController::signalFinish(int ret) { for (unsigned int i=0; iprocessTerminated(this,ret); } /* LINEPROCESSLINECONTROLLER */ void LineProcessController::finished(int ret) { if (OutputSoFar.length()) standardOutputByLine(OutputSoFar); OutputSoFar.clear(); if (ErrorSoFar.length()) standardErrorByLine(ErrorSoFar); ErrorSoFar.clear(); } void LineProcessController::standardOutput(const std::string& aString) { OutputSoFar += aString; std::string EOL(eolTextSequence()); std::string::size_type idx = OutputSoFar.find(EOL); while (idx != std::string::npos) { standardOutputByLine(OutputSoFar.substr(0,idx)); OutputSoFar.erase(0,idx+EOL.length()); idx = OutputSoFar.find(EOL); } } void LineProcessController::standardError(const std::string& aString) { ErrorSoFar += aString; std::string EOL(eolTextSequence()); std::string::size_type idx = ErrorSoFar.find(EOL); while (idx != std::string::npos) { standardErrorByLine(ErrorSoFar.substr(0,idx)); ErrorSoFar.erase(0,idx+EOL.length()); idx = ErrorSoFar.find(EOL); } } class TraceController : public ProcessController { public: TraceController(); virtual void finished(int ret); virtual void standardOutput(const std::string& aString); virtual void standardError(const std::string& aString); virtual void failed(); bool Ended; int ReturnCode; std::string StdOut; std::string StdErr; }; TraceController::TraceController() : Ended(false), ReturnCode(0) { } void TraceController::finished(int ret) { ReturnCode = ret; Ended = true; } void TraceController::standardOutput(const std::string& aString) { StdOut += aString; } void TraceController::standardError(const std::string& aString) { StdErr += aString; } void TraceController::failed() { } int syncExecuteProcess(const std::string& Executable, const std::vector& Args, std::string &StdOut, std::string& StdErr) { TraceController Tracer; bool b = launchProcess(&Tracer,Executable,Args); if (!b) return -1; while (!Tracer.Ended) waitForProcessEvent(); StdOut = Tracer.StdOut; StdErr = Tracer.StdErr; return Tracer.ReturnCode; }