Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmProcessTools.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cmProcessTools.h"
13
14 #include <cmsys/Process.h>
15
16 //----------------------------------------------------------------------------
17 void cmProcessTools::RunProcess(struct cmsysProcess_s* cp,
18                                 OutputParser* out, OutputParser* err)
19 {
20   cmsysProcess_Execute(cp);
21   char* data = 0;
22   int length = 0;
23   int p;
24   while((out||err) && (p=cmsysProcess_WaitForData(cp, &data, &length, 0), p))
25     {
26     if(out && p == cmsysProcess_Pipe_STDOUT)
27       {
28       if(!out->Process(data, length))
29         {
30         out = 0;
31         }
32       }
33     else if(err && p == cmsysProcess_Pipe_STDERR)
34       {
35       if(!err->Process(data, length))
36         {
37         err = 0;
38         }
39       }
40     }
41   cmsysProcess_WaitForExit(cp, 0);
42 }
43
44
45 //----------------------------------------------------------------------------
46 cmProcessTools::LineParser::LineParser(char sep, bool ignoreCR):
47   Separator(sep), IgnoreCR(ignoreCR), Log(0), Prefix(0), LineEnd('\0')
48 {
49 }
50
51 //----------------------------------------------------------------------------
52 void cmProcessTools::LineParser::SetLog(std::ostream* log, const char* prefix)
53 {
54   this->Log = log;
55   this->Prefix = prefix? prefix : "";
56 }
57
58 //----------------------------------------------------------------------------
59 bool cmProcessTools::LineParser::ProcessChunk(const char* first, int length)
60 {
61   const char* last = first + length;
62   for(const char* c = first; c != last; ++c)
63     {
64     if(*c == this->Separator || *c == '\0')
65       {
66       this->LineEnd = *c;
67
68       // Log this line.
69       if(this->Log && this->Prefix)
70         {
71         *this->Log << this->Prefix << this->Line << "\n";
72         }
73
74       // Hand this line to the subclass implementation.
75       if(!this->ProcessLine())
76         {
77         this->Line = "";
78         return false;
79         }
80
81       this->Line = "";
82       }
83     else if(*c != '\r' || !this->IgnoreCR)
84       {
85       // Append this character to the line under construction.
86       this->Line.append(1, *c);
87       }
88     }
89   return true;
90 }