cmProcessTools.cxx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmProcessTools.h"
  11. #include <cmsys/Process.h>
  12. #include <ostream>
  13. void cmProcessTools::RunProcess(struct cmsysProcess_s* cp, OutputParser* out,
  14. OutputParser* err)
  15. {
  16. cmsysProcess_Execute(cp);
  17. char* data = CM_NULLPTR;
  18. int length = 0;
  19. int p;
  20. while ((out || err) &&
  21. (p = cmsysProcess_WaitForData(cp, &data, &length, CM_NULLPTR), p)) {
  22. if (out && p == cmsysProcess_Pipe_STDOUT) {
  23. if (!out->Process(data, length)) {
  24. out = CM_NULLPTR;
  25. }
  26. } else if (err && p == cmsysProcess_Pipe_STDERR) {
  27. if (!err->Process(data, length)) {
  28. err = CM_NULLPTR;
  29. }
  30. }
  31. }
  32. cmsysProcess_WaitForExit(cp, CM_NULLPTR);
  33. }
  34. cmProcessTools::LineParser::LineParser(char sep, bool ignoreCR)
  35. : Log(CM_NULLPTR)
  36. , Prefix(CM_NULLPTR)
  37. , Separator(sep)
  38. , LineEnd('\0')
  39. , IgnoreCR(ignoreCR)
  40. {
  41. }
  42. void cmProcessTools::LineParser::SetLog(std::ostream* log, const char* prefix)
  43. {
  44. this->Log = log;
  45. this->Prefix = prefix ? prefix : "";
  46. }
  47. bool cmProcessTools::LineParser::ProcessChunk(const char* first, int length)
  48. {
  49. const char* last = first + length;
  50. for (const char* c = first; c != last; ++c) {
  51. if (*c == this->Separator || *c == '\0') {
  52. this->LineEnd = *c;
  53. // Log this line.
  54. if (this->Log && this->Prefix) {
  55. *this->Log << this->Prefix << this->Line << "\n";
  56. }
  57. // Hand this line to the subclass implementation.
  58. if (!this->ProcessLine()) {
  59. this->Line = "";
  60. return false;
  61. }
  62. this->Line = "";
  63. } else if (*c != '\r' || !this->IgnoreCR) {
  64. // Append this character to the line under construction.
  65. this->Line.append(1, *c);
  66. }
  67. }
  68. return true;
  69. }