packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmProcessTools.h
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #ifndef cmProcessTools_h
13 #define cmProcessTools_h
14
15 #include "cmStandardIncludes.h"
16
17 /** \class cmProcessTools
18  * \brief Helper classes for process output parsing
19  *
20  */
21 class cmProcessTools
22 {
23 public:
24   /** Abstract interface for process output parsers.  */
25   class OutputParser
26   {
27   public:
28     /** Process the given output data from a tool.  Processing may be
29         done incrementally.  Returns true if the parser is interested
30         in any more data and false if it is done.  */
31     bool Process(const char* data, int length)
32       { return this->ProcessChunk(data, length); }
33     bool Process(const char* data)
34       { return this->Process(data, static_cast<int>(strlen(data))); }
35
36     virtual ~OutputParser() {}
37   protected:
38     /** Implement in a subclass to process a chunk of data.  It should
39         return true only if it is interested in more data.  */
40     virtual bool ProcessChunk(const char* data, int length) = 0;
41   };
42
43   /** Process output parser that extracts one line at a time.  */
44   class LineParser: public OutputParser
45   {
46   public:
47     /** Construct with line separation character and choose whether to
48         ignore carriage returns.  */
49     LineParser(char sep = '\n', bool ignoreCR = true);
50
51     /** Configure logging of lines as they are extracted.  */
52     void SetLog(std::ostream* log, const char* prefix);
53   protected:
54     char Separator;
55     bool IgnoreCR;
56     std::ostream* Log;
57     const char* Prefix;
58     char LineEnd;
59     std::string Line;
60     virtual bool ProcessChunk(const char* data, int length);
61
62     /** Implement in a subclass to process one line of input.  It
63         should return true only if it is interested in more data.  */
64     virtual bool ProcessLine() = 0;
65   };
66
67   /** Trivial line handler for simple logging.  */
68   class OutputLogger: public LineParser
69   {
70   public:
71     OutputLogger(std::ostream& log, const char* prefix = 0)
72       { this->SetLog(&log, prefix); }
73   private:
74     virtual bool ProcessLine() { return true; }
75   };
76
77   /** Run a process and send output to given parsers.  */
78   static void RunProcess(struct cmsysProcess_s* cp,
79                          OutputParser* out, OutputParser* err = 0);
80 };
81
82 #endif