1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmProcessTools.h"
7 #include "cmsys/Process.h"
9 #include "cmProcessOutput.h"
11 void cmProcessTools::RunProcess(struct cmsysProcess_s* cp, OutputParser* out,
12 OutputParser* err, Encoding encoding)
14 cmsysProcess_Execute(cp);
18 cmProcessOutput processOutput(encoding);
20 while ((out || err) &&
21 (p = cmsysProcess_WaitForData(cp, &data, &length, nullptr))) {
22 if (out && p == cmsysProcess_Pipe_STDOUT) {
23 processOutput.DecodeText(data, length, strdata, 1);
24 if (!out->Process(strdata.c_str(), static_cast<int>(strdata.size()))) {
27 } else if (err && p == cmsysProcess_Pipe_STDERR) {
28 processOutput.DecodeText(data, length, strdata, 2);
29 if (!err->Process(strdata.c_str(), static_cast<int>(strdata.size()))) {
35 processOutput.DecodeText(std::string(), strdata, 1);
36 if (!strdata.empty()) {
37 out->Process(strdata.c_str(), static_cast<int>(strdata.size()));
41 processOutput.DecodeText(std::string(), strdata, 2);
42 if (!strdata.empty()) {
43 err->Process(strdata.c_str(), static_cast<int>(strdata.size()));
46 cmsysProcess_WaitForExit(cp, nullptr);
49 cmProcessTools::LineParser::LineParser(char sep, bool ignoreCR)
55 void cmProcessTools::LineParser::SetLog(std::ostream* log, const char* prefix)
58 this->Prefix = prefix ? prefix : "";
61 bool cmProcessTools::LineParser::ProcessChunk(const char* first, int length)
63 const char* last = first + length;
64 for (const char* c = first; c != last; ++c) {
65 if (*c == this->Separator || *c == '\0') {
69 if (this->Log && this->Prefix) {
70 *this->Log << this->Prefix << this->Line << "\n";
73 // Hand this line to the subclass implementation.
74 if (!this->ProcessLine()) {
80 } else if (*c != '\r' || !this->IgnoreCR) {
81 // Append this character to the line under construction.
82 this->Line.append(1, *c);