X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/32a4b1d57c479a1c39def13df6c269c93d2fdf1a..47892637d342b05c1e932ee6f71283f1217c2491:/src/common/mstream.cpp diff --git a/src/common/mstream.cpp b/src/common/mstream.cpp index 7f72e82db4..f778677728 100644 --- a/src/common/mstream.cpp +++ b/src/common/mstream.cpp @@ -1,210 +1,174 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: mstream.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 "mstream.h" -#endif +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" -#include -#include -#include #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif -// ---------------------------------------------------------------------------- -// wxMemoryStreamBase -// ---------------------------------------------------------------------------- -wxMemoryStreamBase::wxMemoryStreamBase() -{ - m_buffer = NULL; - m_iolimit = 0; - m_persistent = FALSE; - m_length = 0; -} +#if wxUSE_STREAMS -wxMemoryStreamBase::~wxMemoryStreamBase() -{ - if (!m_persistent && m_buffer) - free(m_buffer); -} +#include "wx/mstream.h" -bool wxMemoryStreamBase::ChangeBufferSize(size_t new_size) -{ - if (m_iolimit == 1) - return FALSE; +#ifndef WX_PRECOMP + #include "wx/stream.h" +#endif //WX_PRECOMP - m_length = new_size; - if (!m_buffer) - m_buffer = (char *)malloc(m_length); - else - m_buffer = (char *)realloc(m_buffer, m_length); +#include - return (m_buffer != NULL); -} +// ============================================================================ +// implementation +// ============================================================================ // ---------------------------------------------------------------------------- // wxMemoryInputStream // ---------------------------------------------------------------------------- -wxMemoryInputStream::wxMemoryInputStream(const char *data, size_t len) +wxMemoryInputStream::wxMemoryInputStream(const void *data, size_t len) { - m_persistent = TRUE; - m_length = len; - m_buffer = (char *)data; // It's bad. - m_position_i = 0; - m_lastread = 0; - m_eof = FALSE; - m_iolimit = 1; - - m_i_streambuf->SetBufferIO(0); + m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read); + m_i_streambuf->SetBufferIO((void *)data, len); // const_cast + m_i_streambuf->SetIntPosition(0); // seek to start pos + m_i_streambuf->Fixed(true); + + m_length = len; } -wxMemoryInputStream::~wxMemoryInputStream() +wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream) { + 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), _T("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; } -char wxMemoryInputStream::Peek() +wxMemoryInputStream::~wxMemoryInputStream() { - // wxStreamBuffer is disabled so just peek the current character. - - return m_buffer[m_position_i]; + delete m_i_streambuf; } -size_t wxMemoryInputStream::DoRead(void *buffer, size_t size) +char wxMemoryInputStream::Peek() { - if (m_iolimit == 2) { - m_eof = TRUE; - return 0; - } - if (m_position_i+size > m_length) - size = m_length-m_position_i; + char *buf = (char *)m_i_streambuf->GetBufferStart(); + size_t pos = m_i_streambuf->GetIntPosition(); + if ( pos == m_length ) + { + m_lasterror = wxSTREAM_READ_ERROR; - memcpy((void *)((unsigned long)buffer+m_position_i), m_buffer, size); - m_position_i += size; + return 0; + } - return size; + return buf[pos]; } -off_t wxMemoryInputStream::DoSeekInput(off_t pos, wxSeekMode mode) +size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes) { - if (m_iolimit == 2) - return 0; - - switch (mode) { - case wxFromStart: - if ((size_t)pos > m_length) - return m_position_i; - return (m_position_i = pos); + size_t pos = m_i_streambuf->GetIntPosition(); + if ( pos == m_length ) + { + m_lasterror = wxSTREAM_EOF; - case wxFromCurrent: - if ((size_t)(m_position_i+pos) > m_length) - return m_position_i; + return 0; + } - return (m_position_i += pos); + m_i_streambuf->Read(buffer, nbytes); + m_lasterror = wxSTREAM_NO_ERROR; - case wxFromEnd: - if ((size_t)(m_length-pos) > m_length) - return m_position_i; + return m_i_streambuf->GetIntPosition() - pos; +} - return (m_position_i = m_length-pos); - } +wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) +{ + return m_i_streambuf->Seek(pos, mode); +} - return m_position_i; +wxFileOffset wxMemoryInputStream::OnSysTell() const +{ + return m_i_streambuf->Tell(); } // ---------------------------------------------------------------------------- // wxMemoryOutputStream // ---------------------------------------------------------------------------- -wxMemoryOutputStream::wxMemoryOutputStream(char *data, size_t len) +wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len) { - m_persistent = FALSE; - m_buffer = data; - m_length = len; - m_position_o = 0; - m_lastwrite = 0; - m_bad = FALSE; - m_iolimit = 2; - - m_o_streambuf->SetBufferIO(0); + 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); } wxMemoryOutputStream::~wxMemoryOutputStream() { - Sync(); + delete m_o_streambuf; } -size_t wxMemoryOutputStream::DoWrite(const void *buffer, size_t size) +size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes) { - if (m_iolimit == 1) { - m_bad = TRUE; - return 0; - } - - if (m_position_o+size > m_length) - if (!ChangeBufferSize(m_position_o+size)) { - m_bad = TRUE; - return 0; - } + size_t oldpos = m_o_streambuf->GetIntPosition(); + m_o_streambuf->Write(buffer, nbytes); + size_t newpos = m_o_streambuf->GetIntPosition(); - memcpy(m_buffer+m_position_o, buffer, size); - m_position_o += size; + // FIXME can someone please explain what this does? (VZ) + if ( !newpos ) + newpos = m_o_streambuf->GetBufferSize(); - return size; + return newpos - oldpos; } -off_t wxMemoryOutputStream::DoSeekOutput(off_t pos, wxSeekMode mode) +wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) { - if (m_iolimit == 1) - return 0; - - switch (mode) { - case wxFromStart: - if ((size_t)pos > m_length) - return m_position_o; - return (m_position_o = pos); - - case wxFromCurrent: - if ((size_t)(m_position_o+pos) > m_length) - return m_position_o; - - return (m_position_o += pos); - - case wxFromEnd: - if ((size_t)(m_length-pos) > m_length) - return m_position_o; - - return (m_position_o = m_length-pos); - } - - return m_position_o; + return m_o_streambuf->Seek(pos, mode); } -// ---------------------------------------------------------------------------- -// wxMemoryStream -// ---------------------------------------------------------------------------- - -wxMemoryStream::wxMemoryStream(char *data, size_t len) - : wxMemoryInputStream(NULL, 0), wxMemoryOutputStream(NULL, 0) +wxFileOffset wxMemoryOutputStream::OnSysTell() const { - m_persistent = FALSE; - m_buffer = data; - m_length = len; - m_iolimit = 0; + return m_o_streambuf->Tell(); } -wxMemoryStream::~wxMemoryStream() +size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const { + wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") ); + + if ( len > GetSize() ) + len = GetSize(); + + memcpy(buffer, m_o_streambuf->GetBufferStart(), len); + + return len; } + +#endif // wxUSE_STREAMS