cmProcessTools.cxx 2.5 KB

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