resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmProcessTools.h
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #pragma once
4
5 #include "cmConfigure.h" // IWYU pragma: keep
6
7 #include <cstring>
8 #include <iosfwd>
9 #include <string>
10
11 #include "cmProcessOutput.h"
12
13 /** \class cmProcessTools
14  * \brief Helper classes for process output parsing
15  *
16  */
17 class cmProcessTools
18 {
19 public:
20   using Encoding = cmProcessOutput::Encoding;
21   /** Abstract interface for process output parsers.  */
22   class OutputParser
23   {
24   public:
25     /** Process the given output data from a tool.  Processing may be
26         done incrementally.  Returns true if the parser is interested
27         in any more data and false if it is done.  */
28     bool Process(const char* data, int length)
29     {
30       return this->ProcessChunk(data, length);
31     }
32     bool Process(const char* data)
33     {
34       return this->Process(data, static_cast<int>(strlen(data)));
35     }
36
37     virtual ~OutputParser() = default;
38
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
45   /** Process output parser that extracts one line at a time.  */
46   class LineParser : public OutputParser
47   {
48   public:
49     /** Construct with line separation character and choose whether to
50         ignore carriage returns.  */
51     LineParser(char sep = '\n', bool ignoreCR = true);
52
53     /** Configure logging of lines as they are extracted.  */
54     void SetLog(std::ostream* log, const char* prefix);
55
56   protected:
57     std::ostream* Log = nullptr;
58     const char* Prefix = nullptr;
59     std::string Line;
60     char Separator;
61     char LineEnd = '\0';
62     bool IgnoreCR;
63     bool ProcessChunk(const char* data, int length) override;
64
65     /** Implement in a subclass to process one line of input.  It
66         should return true only if it is interested in more data.  */
67     virtual bool ProcessLine() = 0;
68   };
69
70   /** Trivial line handler for simple logging.  */
71   class OutputLogger : public LineParser
72   {
73   public:
74     OutputLogger(std::ostream& log, const char* prefix = nullptr)
75     {
76       this->SetLog(&log, prefix);
77     }
78
79   private:
80     bool ProcessLine() override { return true; }
81   };
82
83   /** Run a process and send output to given parsers.  */
84   static void RunProcess(struct cmsysProcess_s* cp, OutputParser* out,
85                          OutputParser* err = nullptr,
86                          Encoding encoding = cmProcessOutput::Auto);
87 };