cmProcessTools.cxx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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)
  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)
  57. {
  58. // Log this line.
  59. if(this->Log && this->Prefix)
  60. {
  61. *this->Log << this->Prefix << this->Line << "\n";
  62. }
  63. // Hand this line to the subclass implementation.
  64. if(!this->ProcessLine())
  65. {
  66. this->Line = "";
  67. return false;
  68. }
  69. this->Line = "";
  70. }
  71. else if(*c != '\r' || !this->IgnoreCR)
  72. {
  73. // Append this character to the line under construction.
  74. this->Line.append(1, *c);
  75. }
  76. }
  77. return true;
  78. }