diff --git a/include/wx/stream.h b/include/wx/stream.h index d08c24693b..5b80f8766c 100644 --- a/include/wx/stream.h +++ b/include/wx/stream.h @@ -43,8 +43,7 @@ enum wxStreamError wxSTREAM_NO_ERROR = 0, // stream is in good state wxSTREAM_EOF, // EOF reached in Read() or similar wxSTREAM_WRITE_ERROR, // generic write error - wxSTREAM_READ_ERROR, // generic read error - wxSTREAM_PIPE_ERROR // broken pipe: the other half was closed + wxSTREAM_READ_ERROR // generic read error }; // compatibility diff --git a/src/common/process.cpp b/src/common/process.cpp index ed1c866ba3..43dfcdf867 100644 --- a/src/common/process.cpp +++ b/src/common/process.cpp @@ -122,8 +122,7 @@ void wxProcess::SetPipeStreams(wxInputStream *inputSstream, bool wxProcess::IsInputOpened() const { - return m_inputStream && - m_inputStream->GetLastError() != wxSTREAM_PIPE_ERROR; + return m_inputStream && m_inputStream->GetLastError() != wxSTREAM_EOF; } bool wxProcess::IsInputAvailable() const diff --git a/src/common/utilscmn.cpp b/src/common/utilscmn.cpp index 2d56c89014..2cc90ec0e9 100644 --- a/src/common/utilscmn.cpp +++ b/src/common/utilscmn.cpp @@ -1062,6 +1062,38 @@ wxString wxGetCurrentDir() // wxExecute // ---------------------------------------------------------------------------- +// wxDoExecuteWithCapture() helper: reads an entire stream into one array +// +// returns TRUE if ok, FALSE if error +static bool ReadAll(wxInputStream *is, wxArrayString& output) +{ + wxCHECK_MSG( is, FALSE, _T("NULL stream in wxExecute()?") ); + + // the stream could be already at EOF or in wxSTREAM_BROKEN_PIPE state + is->Reset(); + + wxTextInputStream tis(*is); + + bool cont = TRUE; + while ( cont ) + { + wxString line = tis.ReadLine(); + if ( is->Eof() ) + break; + + if ( !*is ) + { + cont = FALSE; + } + else + { + output.Add(line); + } + } + + return cont; +} + // this is a private function because it hasn't a clean interface: the first // array is passed by reference, the second by pointer - instead we have 2 // public versions of wxExecute() below @@ -1083,51 +1115,15 @@ static long wxDoExecuteWithCapture(const wxString& command, #if wxUSE_STREAMS if ( rc != -1 ) { - wxInputStream* is = process->GetInputStream(); - wxCHECK_MSG( is, -1, _T("if wxExecute() succeded, stream can't be NULL") ); - wxTextInputStream tis(*is); + if ( !ReadAll(process->GetInputStream(), output) ) + rc = -1; - wxTextInputStream *tes = NULL; - wxInputStream *es = NULL; if ( error ) { - es = process->GetErrorStream(); - - wxCHECK_MSG( es, -1, _T("stderr can't be NULL") ); - - tes = new wxTextInputStream(*es); + if ( !ReadAll(process->GetErrorStream(), *error) ) + rc = -1; } - bool cont; - do - { - cont = FALSE; - - if ( !is->Eof() && is->IsOk() ) - { - wxString line = tis.ReadLine(); - if ( !*is ) - break; - - cont = TRUE; - - output.Add(line); - } - - if ( error && !es->Eof() && es->IsOk() ) - { - wxString line = tes->ReadLine(); - if ( !*es ) - break; - - cont = TRUE; - - error->Add(line); - } - } - while ( cont ); - - delete tes; } #endif // wxUSE_STREAMS diff --git a/src/msw/utilsexc.cpp b/src/msw/utilsexc.cpp index 6e02444a1e..ae26542774 100644 --- a/src/msw/utilsexc.cpp +++ b/src/msw/utilsexc.cpp @@ -377,7 +377,7 @@ bool wxPipeInputStream::CanRead() const wxPipeInputStream *self = wxConstCast(this, wxPipeInputStream); self->m_hInput = INVALID_HANDLE_VALUE; - self->m_lasterror = wxSTREAM_PIPE_ERROR; + self->m_lasterror = wxSTREAM_EOF; nAvailable = 0; } @@ -388,6 +388,13 @@ bool wxPipeInputStream::CanRead() const size_t wxPipeInputStream::OnSysRead(void *buffer, size_t len) { + if ( !IsOpened() ) + { + m_lasterror = wxSTREAM_EOF; + + return 0; + } + DWORD bytesRead; if ( !::ReadFile(m_hInput, buffer, len, &bytesRead, NULL) ) {