2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2009-02-24 15:40:18 +00:00
|
|
|
#ifndef cmProcessTools_h
|
|
|
|
#define cmProcessTools_h
|
|
|
|
|
2016-11-01 18:36:58 +00:00
|
|
|
#include "cmProcessOutput.h"
|
2016-08-23 22:29:15 +00:00
|
|
|
#include <cmConfigure.h>
|
|
|
|
|
|
|
|
#include <iosfwd>
|
|
|
|
#include <string.h>
|
|
|
|
#include <string>
|
2009-02-24 15:40:18 +00:00
|
|
|
|
|
|
|
/** \class cmProcessTools
|
|
|
|
* \brief Helper classes for process output parsing
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class cmProcessTools
|
|
|
|
{
|
|
|
|
public:
|
2016-11-01 18:36:58 +00:00
|
|
|
typedef cmProcessOutput::Encoding Encoding;
|
2009-02-24 15:40:18 +00: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-05-16 14:34:04 +00:00
|
|
|
{
|
|
|
|
return this->ProcessChunk(data, length);
|
|
|
|
}
|
2009-07-10 15:07:27 +00:00
|
|
|
bool Process(const char* data)
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
|
|
|
return this->Process(data, static_cast<int>(strlen(data)));
|
|
|
|
}
|
2009-02-24 16:08:38 +00:00
|
|
|
|
|
|
|
virtual ~OutputParser() {}
|
2009-02-24 15:40:18 +00: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-05-16 14:34:04 +00:00
|
|
|
class LineParser : public OutputParser
|
2009-02-24 15:40:18 +00: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-05-16 14:34:04 +00:00
|
|
|
|
2009-02-24 15:40:18 +00:00
|
|
|
protected:
|
|
|
|
std::ostream* Log;
|
|
|
|
const char* Prefix;
|
|
|
|
std::string Line;
|
2015-06-07 08:09:47 +00:00
|
|
|
char Separator;
|
|
|
|
char LineEnd;
|
|
|
|
bool IgnoreCR;
|
2016-06-27 19:25:27 +00:00
|
|
|
bool ProcessChunk(const char* data, int length) CM_OVERRIDE;
|
2009-02-24 15:40:18 +00: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-05-16 14:34:04 +00:00
|
|
|
class OutputLogger : public LineParser
|
2009-02-24 15:40:18 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-06-27 20:44:16 +00:00
|
|
|
OutputLogger(std::ostream& log, const char* prefix = CM_NULLPTR)
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
|
|
|
this->SetLog(&log, prefix);
|
|
|
|
}
|
|
|
|
|
2009-02-24 15:40:18 +00:00
|
|
|
private:
|
2016-06-27 19:25:27 +00:00
|
|
|
bool ProcessLine() CM_OVERRIDE { return true; }
|
2009-02-24 15:40:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Run a process and send output to given parsers. */
|
2016-05-16 14:34:04 +00:00
|
|
|
static void RunProcess(struct cmsysProcess_s* cp, OutputParser* out,
|
2016-11-01 18:36:58 +00:00
|
|
|
OutputParser* err = CM_NULLPTR,
|
|
|
|
Encoding encoding = cmProcessOutput::Auto);
|
2009-02-24 15:40:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|