cmake/Source/cmProcessTools.h

89 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. */
2009-10-04 10:30:41 +03:00
#ifndef cmProcessTools_h
#define cmProcessTools_h
2017-07-20 19:35:53 +02:00
#include "cmConfigure.h"
2017-04-14 19:02:05 +02:00
#include "cmProcessOutput.h"
2016-10-30 18:24:19 +01:00
#include <iosfwd>
#include <string.h>
#include <string>
2009-10-04 10:30:41 +03:00
/** \class cmProcessTools
* \brief Helper classes for process output parsing
*
*/
class cmProcessTools
{
public:
2017-04-14 19:02:05 +02:00
typedef cmProcessOutput::Encoding 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
virtual ~OutputParser() {}
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:
std::ostream* Log;
const char* Prefix;
std::string Line;
2015-11-17 17:22:37 +01:00
char Separator;
char LineEnd;
bool IgnoreCR;
2016-10-30 18:24:19 +01:00
bool ProcessChunk(const char* data, int length) CM_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:
2016-10-30 18:24:19 +01:00
OutputLogger(std::ostream& log, const char* prefix = CM_NULLPTR)
2016-07-09 11:21:54 +02:00
{
this->SetLog(&log, prefix);
}
2009-10-04 10:30:41 +03:00
private:
2016-10-30 18:24:19 +01:00
bool ProcessLine() CM_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,
2017-04-14 19:02:05 +02:00
OutputParser* err = CM_NULLPTR,
Encoding encoding = cmProcessOutput::Auto);
2009-10-04 10:30:41 +03:00
};
#endif