2016-09-27 22:01:08 +03: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 18:40:18 +03:00
|
|
|
#ifndef cmProcessTools_h
|
|
|
|
#define cmProcessTools_h
|
|
|
|
|
2016-08-24 01:29:15 +03:00
|
|
|
#include <cmConfigure.h>
|
|
|
|
|
|
|
|
#include <iosfwd>
|
|
|
|
#include <string.h>
|
|
|
|
#include <string>
|
2009-02-24 18:40:18 +03:00
|
|
|
|
|
|
|
/** \class cmProcessTools
|
|
|
|
* \brief Helper classes for process output parsing
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class cmProcessTools
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** 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 17:34:04 +03:00
|
|
|
{
|
|
|
|
return this->ProcessChunk(data, length);
|
|
|
|
}
|
2009-07-10 19:07:27 +04:00
|
|
|
bool Process(const char* data)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
return this->Process(data, static_cast<int>(strlen(data)));
|
|
|
|
}
|
2009-02-24 19:08:38 +03:00
|
|
|
|
|
|
|
virtual ~OutputParser() {}
|
2009-02-24 18:40:18 +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-05-16 17:34:04 +03:00
|
|
|
class LineParser : public OutputParser
|
2009-02-24 18:40:18 +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-05-16 17:34:04 +03:00
|
|
|
|
2009-02-24 18:40:18 +03:00
|
|
|
protected:
|
|
|
|
std::ostream* Log;
|
|
|
|
const char* Prefix;
|
|
|
|
std::string Line;
|
2015-06-07 11:09:47 +03:00
|
|
|
char Separator;
|
|
|
|
char LineEnd;
|
|
|
|
bool IgnoreCR;
|
2016-06-27 22:25:27 +03:00
|
|
|
bool ProcessChunk(const char* data, int length) CM_OVERRIDE;
|
2009-02-24 18:40:18 +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-05-16 17:34:04 +03:00
|
|
|
class OutputLogger : public LineParser
|
2009-02-24 18:40:18 +03:00
|
|
|
{
|
|
|
|
public:
|
2016-06-27 23:44:16 +03:00
|
|
|
OutputLogger(std::ostream& log, const char* prefix = CM_NULLPTR)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
this->SetLog(&log, prefix);
|
|
|
|
}
|
|
|
|
|
2009-02-24 18:40:18 +03:00
|
|
|
private:
|
2016-06-27 22:25:27 +03:00
|
|
|
bool ProcessLine() CM_OVERRIDE { return true; }
|
2009-02-24 18:40:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Run a process and send output to given parsers. */
|
2016-05-16 17:34:04 +03:00
|
|
|
static void RunProcess(struct cmsysProcess_s* cp, OutputParser* out,
|
2016-06-27 23:44:16 +03:00
|
|
|
OutputParser* err = CM_NULLPTR);
|
2009-02-24 18:40:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|