cmProcessTools.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmProcessTools_h
  14. #define cmProcessTools_h
  15. #include "cmStandardIncludes.h"
  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. { return this->ProcessChunk(data, length); }
  32. virtual ~OutputParser() {}
  33. protected:
  34. /** Implement in a subclass to process a chunk of data. It should
  35. return true only if it is interested in more data. */
  36. virtual bool ProcessChunk(const char* data, int length) = 0;
  37. };
  38. /** Process output parser that extracts one line at a time. */
  39. class LineParser: public OutputParser
  40. {
  41. public:
  42. /** Construct with line separation character and choose whether to
  43. ignore carriage returns. */
  44. LineParser(char sep = '\n', bool ignoreCR = true);
  45. /** Configure logging of lines as they are extracted. */
  46. void SetLog(std::ostream* log, const char* prefix);
  47. protected:
  48. char Separator;
  49. bool IgnoreCR;
  50. std::ostream* Log;
  51. const char* Prefix;
  52. std::string Line;
  53. virtual bool ProcessChunk(const char* data, int length);
  54. /** Implement in a subclass to process one line of input. It
  55. should return true only if it is interested in more data. */
  56. virtual bool ProcessLine() = 0;
  57. };
  58. /** Trivial line handler for simple logging. */
  59. class OutputLogger: public LineParser
  60. {
  61. public:
  62. OutputLogger(std::ostream& log, const char* prefix = 0)
  63. { this->SetLog(&log, prefix); }
  64. private:
  65. virtual bool ProcessLine() { return true; }
  66. };
  67. /** Run a process and send output to given parsers. */
  68. static void RunProcess(struct cmsysProcess_s* cp,
  69. OutputParser* out, OutputParser* err = 0);
  70. };
  71. #endif