------------------------------------------------------------------------------
| 33 | |
| 34 | //------------------------------------------------------------------------------ |
| 35 | void vtkExecutableRunner::Execute() |
| 36 | { |
| 37 | std::string command = this->Command; |
| 38 | details::ltrim(command); |
| 39 | if (!command.empty()) |
| 40 | { |
| 41 | std::vector<std::string> splittedCommand = this->GetCommandToExecute(); |
| 42 | |
| 43 | // get a vector of C string for vtksys |
| 44 | std::vector<const char*> stringViewC; |
| 45 | stringViewC.reserve(splittedCommand.size() + 1); |
| 46 | for (const auto& cmd : splittedCommand) |
| 47 | { |
| 48 | stringViewC.emplace_back(cmd.c_str()); |
| 49 | } |
| 50 | stringViewC.emplace_back(nullptr); |
| 51 | |
| 52 | // Configure and launch process |
| 53 | vtksysProcess* process = vtksysProcess_New(); |
| 54 | vtksysProcess_SetCommand(process, stringViewC.data()); |
| 55 | vtksysProcess_SetPipeShared(process, vtksysProcess_Pipe_STDOUT, 0); |
| 56 | vtksysProcess_SetPipeShared(process, vtksysProcess_Pipe_STDERR, 0); |
| 57 | vtksysProcess_SetTimeout(process, this->Timeout); |
| 58 | vtksysProcess_Execute(process); |
| 59 | |
| 60 | // Get output streams |
| 61 | int pipe; |
| 62 | std::string out; |
| 63 | std::string err; |
| 64 | // While loop needed because there is a limit to the buffer size of |
| 65 | // the vtksysProcess streams. If output is too big we have to append. |
| 66 | do |
| 67 | { |
| 68 | char* output = nullptr; |
| 69 | int length = 0; |
| 70 | pipe = vtksysProcess_WaitForData(process, &output, &length, nullptr); |
| 71 | switch (pipe) |
| 72 | { |
| 73 | case vtksysProcess_Pipe_STDOUT: |
| 74 | out.append(std::string(output, length)); |
| 75 | break; |
| 76 | |
| 77 | case vtksysProcess_Pipe_STDERR: |
| 78 | err.append(std::string(output, length)); |
| 79 | break; |
| 80 | } |
| 81 | } while (pipe != vtksysProcess_Pipe_None); |
| 82 | |
| 83 | // Exit properly |
| 84 | this->ReturnValue = this->ExitProcess(process); |
| 85 | vtksysProcess_Delete(process); |
| 86 | |
| 87 | // Trim last whitespaces |
| 88 | if (this->RightTrimResult) |
| 89 | { |
| 90 | details::rtrim(out); |
| 91 | details::rtrim(err); |
| 92 | } |