]> git.saurik.com Git - wxWidgets.git/blame - include/wx/private/streamtempinput.h
Change wxWakeUpPipe to be a wxEventLoopSourceHandler.
[wxWidgets.git] / include / wx / private / streamtempinput.h
CommitLineData
79066131 1///////////////////////////////////////////////////////////////////////////////
9b70925d 2// Name: wx/private/streamtempinput.h
79066131
VZ
3// Purpose: defines wxStreamTempInputBuffer which is used by Unix and MSW
4// implementations of wxExecute; this file is only used by the
5// library and never by the user code
6// Author: Vadim Zeitlin
9b70925d
VZ
7// Modified by: Rob Bresalier
8// Created: 2013-05-04
79066131
VZ
9// RCS-ID: $Id$
10// Copyright: (c) 2002 Vadim Zeitlin <vadim@wxwindows.org>
65571936 11// Licence: wxWindows licence
79066131
VZ
12///////////////////////////////////////////////////////////////////////////////
13
9b70925d
VZ
14#ifndef _WX_PRIVATE_STREAMTEMPINPUT_H
15#define _WX_PRIVATE_STREAMTEMPINPUT_H
79066131 16
9b70925d 17#include "wx/private/pipestream.h"
79066131
VZ
18
19// ----------------------------------------------------------------------------
20// wxStreamTempInputBuffer
21// ----------------------------------------------------------------------------
22
23/*
24 wxStreamTempInputBuffer is a hack which we need to solve the problem of
25 executing a child process synchronously with IO redirecting: when we do
26 this, the child writes to a pipe we open to it but when the pipe buffer
27 (which has finite capacity, e.g. commonly just 4Kb) becomes full we have to
28 read data from it because the child blocks in its write() until then and if
29 it blocks we are never going to return from wxExecute() so we dead lock.
30
31 So here is the fix: we now read the output as soon as it appears into a temp
32 buffer (wxStreamTempInputBuffer object) and later just stuff it back into
33 the stream when the process terminates. See supporting code in wxExecute()
34 itself as well.
35
36 Note that this is horribly inefficient for large amounts of output (count
37 the number of times we copy the data around) and so a better API is badly
38 needed! However it's not easy to devise a way to do this keeping backwards
39 compatibility with the existing wxExecute(wxEXEC_SYNC)...
40*/
79066131
VZ
41class wxStreamTempInputBuffer
42{
43public:
9b70925d
VZ
44 wxStreamTempInputBuffer()
45 {
46 m_stream = NULL;
47 m_buffer = NULL;
48 m_size = 0;
49 }
79066131
VZ
50
51 // call to associate a stream with this buffer, otherwise nothing happens
52 // at all
9b70925d
VZ
53 void Init(wxPipeInputStream *stream)
54 {
55 wxASSERT_MSG( !m_stream, wxS("Can only initialize once") );
56
57 m_stream = stream;
58 }
79066131
VZ
59
60 // check for input on our stream and cache it in our buffer if any
4d425dee
VZ
61 //
62 // return true if anything was done
9b70925d
VZ
63 bool Update()
64 {
65 if ( !m_stream || !m_stream->CanRead() )
66 return false;
67
68 // realloc in blocks of 4Kb: this is the default (and minimal) buffer
69 // size of the Unix pipes so it should be the optimal step
70 //
71 // NB: don't use "static int" in this inline function, some compilers
72 // (e.g. IBM xlC) don't like it
73 enum { incSize = 4096 };
74
75 void *buf = realloc(m_buffer, m_size + incSize);
76 if ( !buf )
77 return false;
78
79 m_buffer = buf;
80 m_stream->Read((char *)m_buffer + m_size, incSize);
81 m_size += m_stream->LastRead();
82
83 return true;
84 }
85
86 // check if can continue reading from the stream, this is used to disable
87 // the callback once we can't read anything more
88 bool Eof() const
89 {
90 // If we have no stream, always return true as we can't read any more.
91 return !m_stream || m_stream->Eof();
92 }
93
cda5c26a
VZ
94 // read everything remaining until the EOF, this should only be called once
95 // the child process terminates and we know that no more data is coming
96 bool ReadAll()
97 {
98 while ( !Eof() )
99 {
100 if ( !Update() )
101 return false;
102 }
103
104 return true;
105 }
106
9b70925d
VZ
107 // dtor puts the data buffered during this object lifetime into the
108 // associated stream
109 ~wxStreamTempInputBuffer()
110 {
111 if ( m_buffer )
112 {
113 m_stream->Ungetch(m_buffer, m_size);
114 free(m_buffer);
115 }
116 }
117
118 const void *GetBuffer() const { return m_buffer; }
79066131 119
9b70925d 120 size_t GetSize() const { return m_size; }
79066131
VZ
121
122private:
123 // the stream we're buffering, if NULL we don't do anything at all
124 wxPipeInputStream *m_stream;
125
126 // the buffer of size m_size (NULL if m_size == 0)
127 void *m_buffer;
128
129 // the size of the buffer
130 size_t m_size;
22f3361e 131
c0c133e1 132 wxDECLARE_NO_COPY_CLASS(wxStreamTempInputBuffer);
79066131
VZ
133};
134
9b70925d 135#endif // _WX_PRIVATE_STREAMTEMPINPUT_H