+
+// ============================================================================
+// implementation of IO redirection support classes
+// ============================================================================
+
+#if wxUSE_STREAMS && !defined(__WXWINCE__)
+
+// ----------------------------------------------------------------------------
+// wxPipeInputStreams
+// ----------------------------------------------------------------------------
+
+wxPipeInputStream::wxPipeInputStream(HANDLE hInput)
+{
+ m_hInput = hInput;
+}
+
+wxPipeInputStream::~wxPipeInputStream()
+{
+ if ( m_hInput != INVALID_HANDLE_VALUE )
+ ::CloseHandle(m_hInput);
+}
+
+bool wxPipeInputStream::CanRead() const
+{
+ if ( !IsOpened() )
+ return FALSE;
+
+ DWORD nAvailable;
+
+ // function name is misleading, it works with anon pipes as well
+ DWORD rc = ::PeekNamedPipe
+ (
+ m_hInput, // handle
+ NULL, 0, // ptr to buffer and its size
+ NULL, // [out] bytes read
+ &nAvailable, // [out] bytes available
+ NULL // [out] bytes left
+ );
+
+ if ( !rc )
+ {
+ if ( ::GetLastError() != ERROR_BROKEN_PIPE )
+ {
+ // unexpected error
+ wxLogLastError(_T("PeekNamedPipe"));
+ }
+
+ // don't try to continue reading from a pipe if an error occured or if
+ // it had been closed
+ ::CloseHandle(m_hInput);
+
+ wxPipeInputStream *self = wxConstCast(this, wxPipeInputStream);
+
+ self->m_hInput = INVALID_HANDLE_VALUE;
+ self->m_lasterror = wxSTREAM_EOF;
+
+ nAvailable = 0;
+ }
+
+ return nAvailable != 0;
+}
+
+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) )
+ {
+ m_lasterror = ::GetLastError() == ERROR_BROKEN_PIPE
+ ? wxSTREAM_EOF
+ : wxSTREAM_READ_ERROR;
+ }
+
+ // bytesRead is set to 0, as desired, if an error occured
+ return bytesRead;
+}
+
+// ----------------------------------------------------------------------------
+// wxPipeOutputStream
+// ----------------------------------------------------------------------------
+
+wxPipeOutputStream::wxPipeOutputStream(HANDLE hOutput)
+{
+ m_hOutput = hOutput;
+}
+
+wxPipeOutputStream::~wxPipeOutputStream()
+{
+ ::CloseHandle(m_hOutput);
+}
+
+size_t wxPipeOutputStream::OnSysWrite(const void *buffer, size_t len)
+{
+ DWORD bytesWritten;
+
+ m_lasterror = wxSTREAM_NO_ERROR;
+ if ( !::WriteFile(m_hOutput, buffer, len, &bytesWritten, NULL) )
+ {
+ m_lasterror = ::GetLastError() == ERROR_BROKEN_PIPE
+ ? wxSTREAM_EOF
+ : wxSTREAM_WRITE_ERROR;
+ }
+
+ return bytesWritten;
+}
+
+#endif // wxUSE_STREAMS
+
+// ============================================================================
+// wxExecute functions family
+// ============================================================================