cmProcessTools.h 2.7 KB

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