added wxInputStream::CanRead() and assorted other stream changes

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@17693 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2002-11-04 00:39:44 +00:00
parent aaa7e220f7
commit 5a86eeac8c
2 changed files with 238 additions and 137 deletions

View File

@@ -1,7 +1,7 @@
/////////////////////////////////////////////////////////////////////////////
// Name: wx/stream.h
// Purpose: "wxWindows stream" base classes
// Author: Guilhem Lavaux
// Purpose: stream classes
// Author: Guilhem Lavaux, Guillermo Rodriguez Garcia, Vadim Zeitlin
// Modified by:
// Created: 11/07/98
// RCS-ID: $Id$
@@ -34,31 +34,40 @@ typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&);
WXDLLEXPORT wxOutputStream& wxEndL(wxOutputStream& o_stream);
// ---------------------------------------------------------------------------
// wxStream: base classes
// ---------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
enum wxStreamError
{
wxSTREAM_NO_ERROR = 0,
wxSTREAM_NO_ERR = wxSTREAM_NO_ERROR,
wxSTREAM_NOERROR = wxSTREAM_NO_ERROR,
wxSTREAM_EOF,
wxSTREAM_WRITE_ERROR,
wxSTREAM_WRITE_ERR = wxSTREAM_WRITE_ERROR,
wxSTREAM_READ_ERROR,
wxSTREAM_READ_ERR = wxSTREAM_READ_ERROR
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
};
// compatibility
#if WXWIN_COMPATIBILITY_2_2
#define wxStream_NOERROR wxSTREAM_NOERROR
#define wxStream_EOF wxSTREAM_EOF
#define wxStream_WRITE_ERR wxSTREAM_WRITE_ERROR
#define wxStream_READ_ERR wxSTREAM_READ_ERROR
#define wxSTREAM_NO_ERR wxSTREAM_NO_ERROR
#define wxSTREAM_NOERROR wxSTREAM_NO_ERROR
#define wxSTREAM_WRITE_ERR wxSTREAM_WRITE_ERROR
#define wxSTREAM_READ_ERR wxSTREAM_READ_ERROR
#endif // WXWIN_COMPATIBILITY_2_2
// ============================================================================
// base stream classes: wxInputStream and wxOutputStream
// ============================================================================
// ---------------------------------------------------------------------------
// wxStreamBase: common (but non virtual!) base for all stream classes
// ---------------------------------------------------------------------------
class WXDLLEXPORT wxStreamBase
{
public:
@@ -67,80 +76,168 @@ public:
// error testing
wxStreamError GetLastError() const { return m_lasterror; }
bool IsOk() const { return LastError() == wxSTREAM_NOERROR; }
bool operator!() const { return LastError() != wxSTREAM_NOERROR; }
bool IsOk() const { return GetLastError() == wxSTREAM_NOERROR; }
bool operator!() const { return !IsOk(); }
// reset the stream state
void Reset() { m_lasterror = wxSTREAM_NOERROR; }
// deprecated (doesn't make sense!), don't use
virtual size_t GetSize() const { return 0; }
#if WXWIN_COMPATIBILITY_2_2
// deprecated, for compatibility only
wxStreamError LastError() const { return m_lasterror; }
size_t StreamSize() const { return GetSize(); }
#endif // WXWIN_COMPATIBILITY_2_2
protected:
// VZ: these functions are really pure virtual and shouldn't be declared
// in the base class because it creates ambiguties in stream classes
// deriving from both wxInputStream and wxOutputStream
#if 0
virtual size_t OnSysRead(void *buffer, size_t bufsize);
virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
#endif
virtual off_t OnSysSeek(off_t seek, wxSeekMode mode);
virtual off_t OnSysTell() const;
friend class wxStreamBuffer;
size_t m_lastcount;
wxStreamError m_lasterror;
friend class wxStreamBuffer;
};
class WXDLLEXPORT wxInputStream : /* virtual */ public wxStreamBase
// ----------------------------------------------------------------------------
// wxInputStream: base class for the input streams
// ----------------------------------------------------------------------------
class WXDLLEXPORT wxInputStream : public wxStreamBase
{
public:
// ctor and dtor, nothing exciting
wxInputStream();
virtual ~wxInputStream();
// is the stream at EOF?
virtual bool Eof() const;
// IO functions
// ------------
// return a character from the stream without removing it, i.e. it will
// still be returned by the next call to GetC()
//
// blocks until something appears in the stream if necessary, if nothing
// ever does (i.e. EOF) LastRead() will return 0 (and the return value is
// undefined), otherwise 1
virtual char Peek();
// return one character from the stream, blocking until it appears if
// necessary
//
// if EOF, return value is undefined and LastRead() will return 0 and not 1
char GetC();
// read at most the given number of bytes from the stream
//
// there are 2 possible situations here: either there is nothing at all in
// the stream right now in which case Read() blocks until something appears
// (use CanRead() to avoid this) or there is already some data available in
// the stream and then Read() doesn't block but returns just the data it
// can read without waiting for more
//
// in any case, if there are not enough bytes in the stream right now,
// LastRead() value will be less than size but greater than 0. If it is 0,
// it means that EOF has been reached.
virtual wxInputStream& Read(void *buffer, size_t size);
wxInputStream& Read(wxOutputStream& stream_out);
// Position functions
virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
virtual off_t TellI() const;
// copy the entire contents of this stream into streamOut, stopping only
// when EOF is reached or an error occurs
wxInputStream& Read(wxOutputStream& streamOut);
// State functions
virtual size_t LastRead() { return wxStreamBase::m_lastcount; }
// Ungetch
// status functions
// ----------------
// returns the number of bytes read by the last call to Read(), GetC() or
// Peek()
//
// this should be used to discover whether that call succeeded in reading
// all the requested data or not
virtual size_t LastRead() const { return wxStreamBase::m_lastcount; }
// returns TRUE if some data is available in the stream right now, so that
// calling Read() wouldn't block
virtual bool CanRead() const;
// is the stream at EOF?
//
// note that this cannot be really implemented for all streams and
// CanRead() is more reliable than Eof()
virtual bool Eof() const;
// write back buffer
// -----------------
// put back the specified number of bytes into the stream, they will be
// fetched by the next call to the read functions
//
// returns the number of bytes really stuffed back
size_t Ungetch(const void *buffer, size_t size);
// put back the specified character in the stream
//
// returns TRUE if ok, FALSE on error
bool Ungetch(char c);
// Operators
// position functions
// ------------------
// move the stream pointer to the given position (if the stream supports
// it)
//
// returns wxInvalidOffset on error
virtual off_t SeekI(off_t pos, wxSeekMode mode = wxFromStart);
// return the current position of the stream pointer or wxInvalidOffset
virtual off_t TellI() const;
// stream-like operators
// ---------------------
wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
wxInputStream& operator>>(__wxInputManip func) { return func(*this); }
protected:
// to be implemented in the derived classes (it should have been pure
// virtual)
virtual size_t OnSysRead(void *buffer, size_t bufsize);
// do read up to size bytes of data into the provided buffer
//
// this method should return 0 if EOF has been reached or an error occured
// (m_lasterror should be set accordingly as well) or the number of bytes
// read
virtual size_t OnSysRead(void *buffer, size_t size) = 0;
// Ungetch managers
// write-back buffer support
// -------------------------
// return the pointer to a buffer big enough to hold sizeNeeded bytes
char *AllocSpaceWBack(size_t sizeNeeded);
// read up to size data from the write back buffer, return the number of
// bytes read
size_t GetWBack(void *buf, size_t size);
// write back buffer or NULL if none
char *m_wback;
size_t m_wbacksize;
size_t m_wbackcur;
char *AllocSpaceWBack(size_t needed_size);
size_t GetWBack(void *buf, size_t bsize);
// the size of the buffer
size_t m_wbacksize;
// the current position in the buffer
size_t m_wbackcur;
friend class wxStreamBuffer;
};
class WXDLLEXPORT wxOutputStream : /* virtual */ public wxStreamBase
// ----------------------------------------------------------------------------
// wxOutputStream: base for the output streams
// ----------------------------------------------------------------------------
class WXDLLEXPORT wxOutputStream : public wxStreamBase
{
public:
wxOutputStream();
@@ -168,6 +265,10 @@ protected:
friend class wxStreamBuffer;
};
// ============================================================================
// helper stream classes
// ============================================================================
// ---------------------------------------------------------------------------
// A stream for measuring streamed output
// ---------------------------------------------------------------------------
@@ -224,6 +325,10 @@ protected:
wxOutputStream *m_parent_o_stream;
};
// ============================================================================
// buffered streams
// ============================================================================
// ---------------------------------------------------------------------------
// Stream buffer: this class can be derived from and passed to
// wxBufferedStreams to implement custom buffering
@@ -240,7 +345,6 @@ public:
};
wxStreamBuffer(wxStreamBase& stream, BufMode mode);
wxStreamBuffer(BufMode mode);
wxStreamBuffer(const wxStreamBuffer& buf);
virtual ~wxStreamBuffer();
@@ -294,6 +398,10 @@ public:
// deprecated, for compatibility only
wxStreamBase *Stream() { return m_stream; }
// this constructs a dummy wxStreamBuffer, used by (and exists for)
// wxMemoryStreams only, don't use!
wxStreamBuffer(BufMode mode);
protected:
void GetFromBuffer(void *buffer, size_t size);
void PutToBuffer(const void *buffer, size_t size);
@@ -328,13 +436,12 @@ protected:
// flags
bool m_destroybuf, // deallocate buffer?
m_destroystream, // delete associated stream?
m_fixed,
m_flushable;
};
// ---------------------------------------------------------------------------
// wxBufferedStreams
// wxBufferedInputStream
// ---------------------------------------------------------------------------
class WXDLLEXPORT wxBufferedInputStream : public wxFilterInputStream
@@ -367,6 +474,10 @@ protected:
wxStreamBuffer *m_i_streambuf;
};
// ----------------------------------------------------------------------------
// wxBufferedOutputStream
// ----------------------------------------------------------------------------
class WXDLLEXPORT wxBufferedOutputStream : public wxFilterOutputStream
{
public:

View File

@@ -46,7 +46,7 @@
// ----------------------------------------------------------------------------
// the temporary buffer size used when copying from stream to stream
#define BUF_TEMP_SIZE 10000
#define BUF_TEMP_SIZE 4096
// ============================================================================
// implementation
@@ -80,6 +80,16 @@ void wxStreamBuffer::Init()
m_fixed = TRUE;
}
wxStreamBuffer::wxStreamBuffer(BufMode mode)
{
Init();
m_stream = NULL;
m_mode = mode;
m_flushable = FALSE;
}
wxStreamBuffer::wxStreamBuffer(wxStreamBase& stream, BufMode mode)
{
Init();
@@ -88,32 +98,13 @@ wxStreamBuffer::wxStreamBuffer(wxStreamBase& stream, BufMode mode)
m_mode = mode;
m_flushable = TRUE;
m_destroystream = FALSE;
}
wxStreamBuffer::wxStreamBuffer(BufMode mode)
{
Init();
wxASSERT_MSG(mode != read_write, wxT("you have to use the other ctor for read_write mode") );
if ( mode == read )
m_stream = new wxInputStream;
else if ( mode == write)
m_stream = new wxOutputStream;
else
m_stream = NULL;
m_mode = mode;
m_flushable = FALSE;
m_destroystream = TRUE;
}
wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer& buffer)
{
// doing this has big chances to lead to a crash when the source buffer is
// destroyed (otherwise assume the caller knows what he does)
wxASSERT_MSG( !buffer.m_destroybuf && !buffer.m_destroystream,
wxASSERT_MSG( !buffer.m_destroybuf,
_T("it's a bad idea to copy this buffer") );
m_buffer_start = buffer.m_buffer_start;
@@ -125,7 +116,6 @@ wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer& buffer)
m_stream = buffer.m_stream;
m_mode = buffer.m_mode;
m_destroybuf = FALSE;
m_destroystream = FALSE;
}
void wxStreamBuffer::FreeBuffer()
@@ -137,9 +127,6 @@ void wxStreamBuffer::FreeBuffer()
wxStreamBuffer::~wxStreamBuffer()
{
FreeBuffer();
if ( m_destroystream )
delete m_stream;
}
wxInputStream *wxStreamBuffer::GetInputStream() const
@@ -195,14 +182,15 @@ void wxStreamBuffer::SetBufferIO(size_t bufsize)
void wxStreamBuffer::ResetBuffer()
{
wxCHECK_RET( m_stream, _T("should have a stream in wxStreamBuffer") );
if ( m_stream )
{
m_stream->m_lasterror = wxStream_NOERROR;
m_stream->m_lastcount = 0;
if (m_mode == read && m_flushable)
m_buffer_pos = m_buffer_end;
else
m_buffer_pos = m_buffer_start;
}
m_buffer_pos = m_mode == read && m_flushable
? m_buffer_end
: m_buffer_start;
}
// fill the buffer with as much data as possible (only for read buffers)
@@ -315,7 +303,7 @@ void wxStreamBuffer::PutChar(char c)
// if we don't have buffer at all, just forward this call to the stream,
if ( !HasBuffer() )
{
outStream->OnSysWrite(&c, 1);
outStream->OnSysWrite(&c, sizeof(c));
}
else
{
@@ -327,7 +315,7 @@ void wxStreamBuffer::PutChar(char c)
}
else
{
PutToBuffer(&c, 1);
PutToBuffer(&c, sizeof(c));
m_stream->m_lastcount = 1;
}
}
@@ -345,7 +333,7 @@ char wxStreamBuffer::Peek()
}
char c;
GetFromBuffer(&c, 1);
GetFromBuffer(&c, sizeof(c));
m_buffer_pos--;
return c;
@@ -360,7 +348,7 @@ char wxStreamBuffer::GetChar()
char c;
if ( !HasBuffer() )
{
inStream->OnSysRead(&c, 1);
inStream->OnSysRead(&c, sizeof(c));
}
else
{
@@ -371,7 +359,7 @@ char wxStreamBuffer::GetChar()
}
else
{
GetFromBuffer(&c, 1);
GetFromBuffer(&c, sizeof(c));
m_stream->m_lastcount = 1;
}
}
@@ -380,17 +368,19 @@ char wxStreamBuffer::GetChar()
}
size_t wxStreamBuffer::Read(void *buffer, size_t size)
{
// lasterror is reset before all new IO calls
if ( m_stream )
m_stream->m_lasterror = wxStream_NOERROR;
size_t read;
if ( !HasBuffer() )
{
wxInputStream *inStream = GetInputStream();
wxCHECK_MSG( inStream, 0, _T("should have a stream in wxStreamBuffer") );
// lasterror is reset before all new IO calls
m_stream->m_lasterror = wxStream_NOERROR;
if ( !HasBuffer() )
{
m_stream->m_lastcount = inStream->OnSysRead(buffer, size);
read = inStream->OnSysRead(buffer, size);
}
else // we have a buffer, use it
{
@@ -421,10 +411,13 @@ size_t wxStreamBuffer::Read(void *buffer, size_t size)
}
}
m_stream->m_lastcount = orig_size - size;
read = orig_size - size;
}
return m_stream->m_lastcount;
if ( m_stream )
m_stream->m_lastcount = read;
return read;
}
// this should really be called "Copy()"
@@ -615,10 +608,8 @@ off_t wxStreamBuffer::Tell() const
{
off_t pos;
// only ask the stream for position if we have a real stream and not a
// dummy one which we created ourselves, otherwise we'd call
// wxStream::OnSysTell() which would always return wxInvalidOffset
if ( !m_destroystream )
// ask the stream for position if we have a real one
if ( m_stream )
{
pos = m_stream->OnSysTell();
if ( pos == wxInvalidOffset )
@@ -677,31 +668,19 @@ wxInputStream::~wxInputStream()
free(m_wback);
}
size_t wxInputStream::OnSysRead(void * WXUNUSED(buffer),
size_t WXUNUSED(bufsize))
bool wxInputStream::CanRead() const
{
return 0;
// we don't know if there is anything to read or not and by default we
// prefer to be optimistic and try to read data unless we know for sure
// there is no more of it
return m_lasterror != wxSTREAM_EOF;
}
bool wxInputStream::Eof() const
{
wxInputStream *self = wxConstCast(this, wxInputStream);
char c;
self->Read(&c, 1);
// some streams can know that they're at EOF before actually trying to
// read beyond the end of stream (e.g. files) while others have no way of
// knowing it, so to provide the same behaviour in all cases we only
// return TRUE from here if the character really couldn't be read
if ( !self->LastRead() && GetLastError() == wxSTREAM_EOF )
{
return TRUE;
}
self->Ungetch(c);
return FALSE;
// the only way the base class can know we're at EOF is when we'd already
// tried to read beyond it in which case last error is set accordingly
return GetLastError() == wxSTREAM_EOF;
}
char *wxInputStream::AllocSpaceWBack(size_t needed_size)
@@ -730,7 +709,7 @@ char *wxInputStream::AllocSpaceWBack(size_t needed_size)
return m_wback;
}
size_t wxInputStream::GetWBack(void *buf, size_t bsize)
size_t wxInputStream::GetWBack(void *buf, size_t size)
{
if (!m_wback)
return 0;
@@ -738,10 +717,10 @@ size_t wxInputStream::GetWBack(void *buf, size_t bsize)
// how many bytes do we have in the buffer?
size_t toget = m_wbacksize - m_wbackcur;
if ( bsize < toget )
if ( size < toget )
{
// we won't read everything
toget = bsize;
toget = size;
}
// copy the data from the cache
@@ -783,13 +762,13 @@ size_t wxInputStream::Ungetch(const void *buf, size_t bufsize)
bool wxInputStream::Ungetch(char c)
{
return Ungetch(&c, sizeof(char)) != 0;
return Ungetch(&c, sizeof(c)) != 0;
}
char wxInputStream::GetC()
{
char c;
Read(&c, 1);
Read(&c, sizeof(c));
return c;
}
@@ -811,6 +790,13 @@ wxInputStream& wxInputStream::Read(void *buf, size_t size)
break;
}
if ( p != buf && !CanRead() )
{
// we have already read something and we would block in OnSysRead()
// now: don't do it but return immediately
break;
}
read = OnSysRead(p, size);
if ( !read )
{
@@ -825,7 +811,7 @@ wxInputStream& wxInputStream::Read(void *buf, size_t size)
char wxInputStream::Peek()
{
char c;
Read(&c, 1);
Read(&c, sizeof(c));
if (m_lasterror == wxStream_NOERROR)
{
Ungetch(c);
@@ -838,13 +824,17 @@ char wxInputStream::Peek()
wxInputStream& wxInputStream::Read(wxOutputStream& stream_out)
{
char buf[BUF_TEMP_SIZE];
size_t bytes_read = BUF_TEMP_SIZE;
while (bytes_read == BUF_TEMP_SIZE)
for ( ;; )
{
bytes_read = Read(buf, bytes_read).LastRead();
bytes_read = stream_out.Write(buf, bytes_read).LastWrite();
size_t bytes_read = Read(buf, WXSIZEOF(buf)).LastRead();
if ( !bytes_read )
break;
if ( stream_out.Write(buf, bytes_read).LastWrite() != bytes_read )
break;
}
return *this;
}
@@ -913,7 +903,7 @@ size_t wxOutputStream::OnSysWrite(const void * WXUNUSED(buffer),
void wxOutputStream::PutC(char c)
{
Write(&c, 1);
Write(&c, sizeof(c));
}
wxOutputStream& wxOutputStream::Write(const void *buffer, size_t size)