cmProcessTools.cxx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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"
  4. #include <cmsys/Process.h>
  5. #include <ostream>
  6. void cmProcessTools::RunProcess(struct cmsysProcess_s* cp, OutputParser* out,
  7. OutputParser* err)
  8. {
  9. cmsysProcess_Execute(cp);
  10. char* data = CM_NULLPTR;
  11. int length = 0;
  12. int p;
  13. while ((out || err) &&
  14. (p = cmsysProcess_WaitForData(cp, &data, &length, CM_NULLPTR), p)) {
  15. if (out && p == cmsysProcess_Pipe_STDOUT) {
  16. if (!out->Process(data, length)) {
  17. out = CM_NULLPTR;
  18. }
  19. } else if (err && p == cmsysProcess_Pipe_STDERR) {
  20. if (!err->Process(data, length)) {
  21. err = CM_NULLPTR;
  22. }
  23. }
  24. }
  25. cmsysProcess_WaitForExit(cp, CM_NULLPTR);
  26. }
  27. cmProcessTools::LineParser::LineParser(char sep, bool ignoreCR)
  28. : Log(CM_NULLPTR)
  29. , Prefix(CM_NULLPTR)
  30. , Separator(sep)
  31. , LineEnd('\0')
  32. , IgnoreCR(ignoreCR)
  33. {
  34. }
  35. void cmProcessTools::LineParser::SetLog(std::ostream* log, const char* prefix)
  36. {
  37. this->Log = log;
  38. this->Prefix = prefix ? prefix : "";
  39. }
  40. bool cmProcessTools::LineParser::ProcessChunk(const char* first, int length)
  41. {
  42. const char* last = first + length;
  43. for (const char* c = first; c != last; ++c) {
  44. if (*c == this->Separator || *c == '\0') {
  45. this->LineEnd = *c;
  46. // Log this line.
  47. if (this->Log && this->Prefix) {
  48. *this->Log << this->Prefix << this->Line << "\n";
  49. }
  50. // Hand this line to the subclass implementation.
  51. if (!this->ProcessLine()) {
  52. this->Line = "";
  53. return false;
  54. }
  55. this->Line = "";
  56. } else if (*c != '\r' || !this->IgnoreCR) {
  57. // Append this character to the line under construction.
  58. this->Line.append(1, *c);
  59. }
  60. }
  61. return true;
  62. }