cmake/Source/cmProcessTools.h

88 lines
2.5 KiB
C
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2021-09-14 00:13:48 +02:00
#pragma once
2009-10-04 10:30:41 +03:00
2018-01-26 17:06:56 +01:00
#include "cmConfigure.h" // IWYU pragma: keep
2016-10-30 18:24:19 +01:00
2020-02-01 23:06:01 +01:00
#include <cstring>
2016-10-30 18:24:19 +01:00
#include <iosfwd>
#include <string>
2009-10-04 10:30:41 +03:00
2020-02-01 23:06:01 +01:00
#include "cmProcessOutput.h"
2009-10-04 10:30:41 +03:00
/** \class cmProcessTools
* \brief Helper classes for process output parsing
*
*/
class cmProcessTools
{
public:
2020-02-01 23:06:01 +01:00
using Encoding = cmProcessOutput::Encoding;
2009-10-04 10:30:41 +03:00
/** Abstract interface for process output parsers. */
class OutputParser
{
public:
/** Process the given output data from a tool. Processing may be
done incrementally. Returns true if the parser is interested
in any more data and false if it is done. */
bool Process(const char* data, int length)
2016-07-09 11:21:54 +02:00
{
return this->ProcessChunk(data, length);
}
2009-10-04 10:30:41 +03:00
bool Process(const char* data)
2016-07-09 11:21:54 +02:00
{
return this->Process(data, static_cast<int>(strlen(data)));
}
2009-10-04 10:30:41 +03:00
2019-11-11 23:01:05 +01:00
virtual ~OutputParser() = default;
2018-08-09 18:06:22 +02:00
2009-10-04 10:30:41 +03:00
protected:
/** Implement in a subclass to process a chunk of data. It should
return true only if it is interested in more data. */
virtual bool ProcessChunk(const char* data, int length) = 0;
};
/** Process output parser that extracts one line at a time. */
2016-07-09 11:21:54 +02:00
class LineParser : public OutputParser
2009-10-04 10:30:41 +03:00
{
public:
/** Construct with line separation character and choose whether to
ignore carriage returns. */
LineParser(char sep = '\n', bool ignoreCR = true);
/** Configure logging of lines as they are extracted. */
void SetLog(std::ostream* log, const char* prefix);
2016-07-09 11:21:54 +02:00
2009-10-04 10:30:41 +03:00
protected:
2019-11-11 23:01:05 +01:00
std::ostream* Log = nullptr;
const char* Prefix = nullptr;
2009-10-04 10:30:41 +03:00
std::string Line;
2015-11-17 17:22:37 +01:00
char Separator;
2019-11-11 23:01:05 +01:00
char LineEnd = '\0';
2015-11-17 17:22:37 +01:00
bool IgnoreCR;
2018-01-26 17:06:56 +01:00
bool ProcessChunk(const char* data, int length) override;
2009-10-04 10:30:41 +03:00
/** Implement in a subclass to process one line of input. It
should return true only if it is interested in more data. */
virtual bool ProcessLine() = 0;
};
/** Trivial line handler for simple logging. */
2016-07-09 11:21:54 +02:00
class OutputLogger : public LineParser
2009-10-04 10:30:41 +03:00
{
public:
2018-01-26 17:06:56 +01:00
OutputLogger(std::ostream& log, const char* prefix = nullptr)
2016-07-09 11:21:54 +02:00
{
this->SetLog(&log, prefix);
}
2009-10-04 10:30:41 +03:00
private:
2018-01-26 17:06:56 +01:00
bool ProcessLine() override { return true; }
2009-10-04 10:30:41 +03:00
};
/** Run a process and send output to given parsers. */
2016-07-09 11:21:54 +02:00
static void RunProcess(struct cmsysProcess_s* cp, OutputParser* out,
2018-01-26 17:06:56 +01:00
OutputParser* err = nullptr,
2017-04-14 19:02:05 +02:00
Encoding encoding = cmProcessOutput::Auto);
2009-10-04 10:30:41 +03:00
};