diff options
author | Brad King <brad.king@kitware.com> | 2009-02-24 15:40:18 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2009-02-24 15:40:18 (GMT) |
commit | 1936499250425a1d50d70759758c7385b4bdf6d4 (patch) | |
tree | ba467f3fdf79068f715b64eb6a72536d9cee2996 /Source/cmProcessTools.cxx | |
parent | 6bdc2b5d99313ab6418b07ff91320135907ac261 (diff) | |
download | CMake-1936499250425a1d50d70759758c7385b4bdf6d4.zip CMake-1936499250425a1d50d70759758c7385b4bdf6d4.tar.gz CMake-1936499250425a1d50d70759758c7385b4bdf6d4.tar.bz2 |
ENH: Create cmProcessTools to parse child output
This class provides a RunProcess method to run a child process and send
its output to an abstract parsing interface. This also provides a
simple line parser and logger implementing the parsing interface.
Diffstat (limited to 'Source/cmProcessTools.cxx')
-rw-r--r-- | Source/cmProcessTools.cxx | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/Source/cmProcessTools.cxx b/Source/cmProcessTools.cxx new file mode 100644 index 0000000..ef40f57 --- /dev/null +++ b/Source/cmProcessTools.cxx @@ -0,0 +1,93 @@ +/*========================================================================= + + Program: CMake - Cross-Platform Makefile Generator + Module: $RCSfile$ + Language: C++ + Date: $Date$ + Version: $Revision$ + + Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved. + See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "cmProcessTools.h" + +#include <cmsys/Process.h> + +//---------------------------------------------------------------------------- +void cmProcessTools::RunProcess(struct cmsysProcess_s* cp, + OutputParser* out, OutputParser* err) +{ + cmsysProcess_Execute(cp); + char* data = 0; + int length = 0; + int p; + while((out||err) && (p=cmsysProcess_WaitForData(cp, &data, &length, 0), p)) + { + if(out && p == cmsysProcess_Pipe_STDOUT) + { + if(!out->Process(data, length)) + { + out = 0; + } + } + else if(err && p == cmsysProcess_Pipe_STDERR) + { + if(!err->Process(data, length)) + { + err = 0; + } + } + } + cmsysProcess_WaitForExit(cp, 0); +} + + +//---------------------------------------------------------------------------- +cmProcessTools::LineParser::LineParser(char sep, bool ignoreCR): + Separator(sep), IgnoreCR(ignoreCR), Log(0), Prefix(0) +{ +} + +//---------------------------------------------------------------------------- +void cmProcessTools::LineParser::SetLog(std::ostream* log, const char* prefix) +{ + this->Log = log; + this->Prefix = prefix? prefix : ""; +} + +//---------------------------------------------------------------------------- +bool cmProcessTools::LineParser::ProcessChunk(const char* first, int length) +{ + const char* last = first + length; + for(const char* c = first; c != last; ++c) + { + if(*c == this->Separator) + { + // Log this line. + if(this->Log && this->Prefix) + { + *this->Log << this->Prefix << this->Line << "\n"; + } + + // Hand this line to the subclass implementation. + if(!this->ProcessLine()) + { + this->Line = ""; + return false; + } + + this->Line = ""; + } + else if(*c != '\r' || !this->IgnoreCR) + { + // Append this character to the line under construction. + this->Line.append(1, *c); + } + } + return true; +} |