cmProcessTools.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #ifndef cmProcessTools_h
  11. #define cmProcessTools_h
  12. #include <cmConfigure.h>
  13. #include <iosfwd>
  14. #include <string.h>
  15. #include <string>
  16. /** \class cmProcessTools
  17. * \brief Helper classes for process output parsing
  18. *
  19. */
  20. class cmProcessTools
  21. {
  22. public:
  23. /** Abstract interface for process output parsers. */
  24. class OutputParser
  25. {
  26. public:
  27. /** Process the given output data from a tool. Processing may be
  28. done incrementally. Returns true if the parser is interested
  29. in any more data and false if it is done. */
  30. bool Process(const char* data, int length)
  31. {
  32. return this->ProcessChunk(data, length);
  33. }
  34. bool Process(const char* data)
  35. {
  36. return this->Process(data, static_cast<int>(strlen(data)));
  37. }
  38. virtual ~OutputParser() {}
  39. protected:
  40. /** Implement in a subclass to process a chunk of data. It should
  41. return true only if it is interested in more data. */
  42. virtual bool ProcessChunk(const char* data, int length) = 0;
  43. };
  44. /** Process output parser that extracts one line at a time. */
  45. class LineParser : public OutputParser
  46. {
  47. public:
  48. /** Construct with line separation character and choose whether to
  49. ignore carriage returns. */
  50. LineParser(char sep = '\n', bool ignoreCR = true);
  51. /** Configure logging of lines as they are extracted. */
  52. void SetLog(std::ostream* log, const char* prefix);
  53. protected:
  54. std::ostream* Log;
  55. const char* Prefix;
  56. std::string Line;
  57. char Separator;
  58. char LineEnd;
  59. bool IgnoreCR;
  60. bool ProcessChunk(const char* data, int length) CM_OVERRIDE;
  61. /** Implement in a subclass to process one line of input. It
  62. should return true only if it is interested in more data. */
  63. virtual bool ProcessLine() = 0;
  64. };
  65. /** Trivial line handler for simple logging. */
  66. class OutputLogger : public LineParser
  67. {
  68. public:
  69. OutputLogger(std::ostream& log, const char* prefix = CM_NULLPTR)
  70. {
  71. this->SetLog(&log, prefix);
  72. }
  73. private:
  74. bool ProcessLine() CM_OVERRIDE { return true; }
  75. };
  76. /** Run a process and send output to given parsers. */
  77. static void RunProcess(struct cmsysProcess_s* cp, OutputParser* out,
  78. OutputParser* err = CM_NULLPTR);
  79. };
  80. #endif