X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3d4c6a214ac5b00dbf5e314e369471135db81c0d..76015a6bb79156d8af1b5e8b4b8e46b4e192d18f:/src/common/mstream.cpp?ds=inline diff --git a/src/common/mstream.cpp b/src/common/mstream.cpp index a12041ee0e..66af403e2f 100644 --- a/src/common/mstream.cpp +++ b/src/common/mstream.cpp @@ -1,133 +1,207 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: mmstream.cpp +// Name: src/common/mstream.cpp // Purpose: "Memory stream" classes // Author: Guilhem Lavaux -// Modified by: +// Modified by: VZ (23.11.00): general code review // Created: 04/01/98 // RCS-ID: $Id$ // Copyright: (c) Guilhem Lavaux -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ -#pragma implementation "mmstream.h" +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop #endif +#if wxUSE_STREAMS + +#include "wx/mstream.h" + +#ifndef WX_PRECOMP + #include "wx/stream.h" +#endif //WX_PRECOMP + #include -#include -#include -wxMemoryStreamBase::wxMemoryStreamBase(char *data, size_t length, int iolimit) -{ - m_buffer = data; - m_iolimit = iolimit; - m_persistent = FALSE; - m_length = length; - m_position_i = m_position_o = 0; -} +// ============================================================================ +// implementation +// ============================================================================ + +// ---------------------------------------------------------------------------- +// wxMemoryInputStream +// ---------------------------------------------------------------------------- -wxMemoryStreamBase::~wxMemoryStreamBase() +IMPLEMENT_ABSTRACT_CLASS(wxMemoryInputStream, wxInputStream) + +wxMemoryInputStream::wxMemoryInputStream(const void *data, size_t len) { - free(m_buffer); + m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read); + m_i_streambuf->SetBufferIO(const_cast(data), len); + m_i_streambuf->SetIntPosition(0); // seek to start pos + m_i_streambuf->Fixed(true); + + m_length = len; } -wxInputStream& wxMemoryStreamBase::Read(void *buffer, size_t size) +wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream) { - if (m_iolimit == 2) { - m_eof = TRUE; - return *this; - } - if (m_position_i+size > m_length) - size = m_length-m_position_i; - - memcpy((void *)((unsigned long)buffer+m_position_i), m_buffer, size); - m_position_i += size; - m_lastread = size; - - return *this; + const wxFileOffset lenFile = stream.GetLength(); + if ( lenFile == wxInvalidOffset ) + { + m_i_streambuf = NULL; + m_lasterror = wxSTREAM_EOF; + return; + } + + const size_t len = wx_truncate_cast(size_t, lenFile); + wxASSERT_MSG( len == lenFile + size_t(0), wxT("huge files not supported") ); + + m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read); + m_i_streambuf->SetBufferIO(len); // create buffer + stream.CopyTo(m_i_streambuf->GetBufferStart(), len); + m_i_streambuf->SetIntPosition(0); // seek to start pos + m_i_streambuf->Fixed(true); + m_length = len; } -size_t wxMemoryStreamBase::SeekI(int pos, wxWhenceType whence) +void +wxMemoryInputStream::InitFromStream(wxInputStream& stream, wxFileOffset lenFile) { - if (m_iolimit == 2) - return 0; + if ( lenFile == wxInvalidOffset ) + lenFile = stream.GetLength(); + + if ( lenFile == wxInvalidOffset ) + { + m_i_streambuf = NULL; + m_lasterror = wxSTREAM_EOF; + return; + } - switch (whence) { - case wxBeginPosition: - if ((size_t)pos > m_length) - return m_position_i; - return (m_position_i = pos); + const size_t len = wx_truncate_cast(size_t, lenFile); + wxASSERT_MSG( (wxFileOffset)len == lenFile, wxT("huge files not supported") ); - case wxCurrentPosition: - if ((size_t)(m_position_i+pos) > m_length) - return m_position_i; + m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read); + m_i_streambuf->SetBufferIO(len); // create buffer + stream.Read(m_i_streambuf->GetBufferStart(), len); + m_i_streambuf->SetIntPosition(0); // seek to start pos + m_i_streambuf->Fixed(true); + m_length = stream.LastRead(); +} - return (m_position_i += pos); +bool wxMemoryInputStream::CanRead() const +{ + return m_i_streambuf->GetIntPosition() != m_length; +} - case wxEndPosition: - if ((size_t)(m_length-pos) > m_length) - return m_position_i; +wxMemoryInputStream::~wxMemoryInputStream() +{ + delete m_i_streambuf; +} - return (m_position_i = m_length-pos); - } +char wxMemoryInputStream::Peek() +{ + char *buf = (char *)m_i_streambuf->GetBufferStart(); + size_t pos = m_i_streambuf->GetIntPosition(); + if ( pos == m_length ) + { + m_lasterror = wxSTREAM_READ_ERROR; - return m_position_i; + return 0; + } + + return buf[pos]; } -wxOutputStream& wxMemoryStreamBase::Write(const void *buffer, size_t size) +size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes) { - if (m_iolimit == 1) { - m_bad = TRUE; - return *this; - } - - if (m_position_o+size > m_length) - if (!ChangeBufferSize(m_position_o+size)) { - m_bad = TRUE; - return *this; + size_t pos = m_i_streambuf->GetIntPosition(); + if ( pos == m_length ) + { + m_lasterror = wxSTREAM_EOF; + + return 0; } - memcpy(m_buffer+m_position_o, buffer, size); - m_position_o += size; - m_lastwrite = size; + m_i_streambuf->Read(buffer, nbytes); + m_lasterror = wxSTREAM_NO_ERROR; - return *this; + return m_i_streambuf->GetIntPosition() - pos; } -size_t wxMemoryStreamBase::SeekO(int pos, wxWhenceType whence) +wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) { - if (m_iolimit == 2) - return 0; + return m_i_streambuf->Seek(pos, mode); +} - switch (whence) { - case wxBeginPosition: - if ((size_t)pos > m_length) - return m_position_o; - return (m_position_o = pos); +wxFileOffset wxMemoryInputStream::OnSysTell() const +{ + return m_i_streambuf->Tell(); +} - case wxCurrentPosition: - if ((size_t)(m_position_o+pos) > m_length) - return m_position_o; +// ---------------------------------------------------------------------------- +// wxMemoryOutputStream +// ---------------------------------------------------------------------------- - return (m_position_o += pos); +IMPLEMENT_DYNAMIC_CLASS(wxMemoryOutputStream, wxOutputStream) - case wxEndPosition: - if ((size_t)(m_length-pos) > m_length) - return m_position_o; +wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len) +{ + m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write); + if ( data ) + m_o_streambuf->SetBufferIO(data, len); + m_o_streambuf->Fixed(false); + m_o_streambuf->Flushable(false); +} - return (m_position_o = m_length-pos); - } +wxMemoryOutputStream::~wxMemoryOutputStream() +{ + delete m_o_streambuf; +} + +size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes) +{ + size_t oldpos = m_o_streambuf->GetIntPosition(); + m_o_streambuf->Write(buffer, nbytes); + size_t newpos = m_o_streambuf->GetIntPosition(); - return m_position_o; + // FIXME can someone please explain what this does? (VZ) + if ( !newpos ) + newpos = m_o_streambuf->GetBufferSize(); + + return newpos - oldpos; +} + +wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) +{ + return m_o_streambuf->Seek(pos, mode); +} + +wxFileOffset wxMemoryOutputStream::OnSysTell() const +{ + return m_o_streambuf->Tell(); } -bool wxMemoryStreamBase::ChangeBufferSize(size_t new_size) +size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const { - m_length = new_size; - if (!m_buffer) - m_buffer = (char *)malloc(m_length); - else - m_buffer = (char *)realloc(m_buffer, m_length); + wxCHECK_MSG( buffer, 0, wxT("must have buffer to CopyTo") ); + + if ( len > GetSize() ) + len = GetSize(); - return (m_buffer != NULL); + memcpy(buffer, m_o_streambuf->GetBufferStart(), len); + + return len; } + +#endif // wxUSE_STREAMS