X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3d4c6a214ac5b00dbf5e314e369471135db81c0d..678d9f2a42faa84cf934bbb75a5b4414a094012e:/src/common/mstream.cpp diff --git a/src/common/mstream.cpp b/src/common/mstream.cpp index a12041ee0e..4555d35062 100644 --- a/src/common/mstream.cpp +++ b/src/common/mstream.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: mmstream.cpp +// Name: mstream.cpp // Purpose: "Memory stream" classes // Author: Guilhem Lavaux // Modified by: @@ -10,124 +10,94 @@ ///////////////////////////////////////////////////////////////////////////// #ifdef __GNUG__ -#pragma implementation "mmstream.h" +#pragma implementation "mstream.h" #endif +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#if wxUSE_STREAMS + #include #include -#include +#include -wxMemoryStreamBase::wxMemoryStreamBase(char *data, size_t length, int iolimit) +// ---------------------------------------------------------------------------- +// wxMemoryInputStream +// ---------------------------------------------------------------------------- + +wxMemoryInputStream::wxMemoryInputStream(const char *data, size_t len) + : wxInputStream() { - m_buffer = data; - m_iolimit = iolimit; - m_persistent = FALSE; - m_length = length; - m_position_i = m_position_o = 0; + m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read); + m_i_streambuf->SetBufferIO((char*) data, (char*) (data+len)); + m_i_streambuf->SetIntPosition(0); // seek to start pos + m_i_streambuf->Fixed(TRUE); + + m_length = len; } -wxMemoryStreamBase::~wxMemoryStreamBase() +wxMemoryInputStream::~wxMemoryInputStream() { - free(m_buffer); + delete m_i_streambuf; } -wxInputStream& wxMemoryStreamBase::Read(void *buffer, size_t size) +char wxMemoryInputStream::Peek() { - 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; + return m_i_streambuf->GetBufferStart()[m_i_streambuf->GetIntPosition()]; } -size_t wxMemoryStreamBase::SeekI(int pos, wxWhenceType whence) +size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes) { - if (m_iolimit == 2) - return 0; - - switch (whence) { - case wxBeginPosition: - if ((size_t)pos > m_length) - return m_position_i; - return (m_position_i = pos); - - case wxCurrentPosition: - if ((size_t)(m_position_i+pos) > m_length) - return m_position_i; - - return (m_position_i += pos); - - case wxEndPosition: - if ((size_t)(m_length-pos) > m_length) - return m_position_i; - - return (m_position_i = m_length-pos); - } - - return m_position_i; + return m_i_streambuf->Read(buffer, nbytes); } -wxOutputStream& wxMemoryStreamBase::Write(const void *buffer, size_t size) +off_t wxMemoryInputStream::OnSysSeek(off_t pos, wxSeekMode mode) { - 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; - } - - memcpy(m_buffer+m_position_o, buffer, size); - m_position_o += size; - m_lastwrite = size; - - return *this; + return m_i_streambuf->Seek(pos, mode); } -size_t wxMemoryStreamBase::SeekO(int pos, wxWhenceType whence) +off_t wxMemoryInputStream::OnSysTell() const { - if (m_iolimit == 2) - return 0; - - switch (whence) { - case wxBeginPosition: - if ((size_t)pos > m_length) - return m_position_o; - return (m_position_o = pos); - - case wxCurrentPosition: - if ((size_t)(m_position_o+pos) > m_length) - return m_position_o; + return m_i_streambuf->Tell(); +} - return (m_position_o += pos); +// ---------------------------------------------------------------------------- +// wxMemoryOutputStream +// ---------------------------------------------------------------------------- - case wxEndPosition: - if ((size_t)(m_length-pos) > m_length) - return m_position_o; +wxMemoryOutputStream::wxMemoryOutputStream(char *data, size_t len) + : wxOutputStream() +{ + m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write); + if (data) + m_o_streambuf->SetBufferIO(data, data+len); + m_o_streambuf->Fixed(TRUE); +} - return (m_position_o = m_length-pos); - } +wxMemoryOutputStream::~wxMemoryOutputStream() +{ + delete m_o_streambuf; +} - return m_position_o; +size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes) +{ + return m_o_streambuf->Write(buffer, nbytes); } -bool wxMemoryStreamBase::ChangeBufferSize(size_t new_size) +off_t wxMemoryOutputStream::OnSysSeek(off_t pos, wxSeekMode mode) { - m_length = new_size; - if (!m_buffer) - m_buffer = (char *)malloc(m_length); - else - m_buffer = (char *)realloc(m_buffer, m_length); + return m_o_streambuf->Seek(pos, mode); +} - return (m_buffer != NULL); +off_t wxMemoryOutputStream::OnSysTell() const +{ + return m_o_streambuf->Tell(); } + + +#endif