X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/79c3e0e1aeebb64da2ac893e6ed7b27edca01a64..353149f8f21d4698a68ae77623ebc0439893b2d1:/src/common/mstream.cpp diff --git a/src/common/mstream.cpp b/src/common/mstream.cpp index d37a9ddaa3..b0f1ab6a1a 100644 --- a/src/common/mstream.cpp +++ b/src/common/mstream.cpp @@ -15,107 +15,71 @@ // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" -#include -#include -#include #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif -#if !USE_SHARED_LIBRARY -IMPLEMENT_CLASS(wxMemoryInputStream, wxInputStream) -IMPLEMENT_CLASS(wxMemoryOutputStream, wxOutputStream) -IMPLEMENT_CLASS2(wxMemoryStream, wxInputStream, wxOutputStream) -#endif +#if wxUSE_STREAMS + +#include +#include "wx/stream.h" +#include "wx/mstream.h" // ---------------------------------------------------------------------------- -// wxMemoryStreamBase +// wxMemoryInputStream // ---------------------------------------------------------------------------- -wxMemoryStreamBase::wxMemoryStreamBase() -{ - m_buffer = NULL; - m_iolimit = 0; - m_persistent = FALSE; - m_length = 0; -} -wxMemoryStreamBase::~wxMemoryStreamBase() +wxMemoryInputStream::wxMemoryInputStream(const char *data, size_t len) + : wxInputStream() { - if (!m_persistent && m_buffer) - free(m_buffer); + 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; } -bool wxMemoryStreamBase::ChangeBufferSize(size_t new_size) +wxMemoryInputStream::~wxMemoryInputStream() { - if (m_iolimit == 1) - return FALSE; - - m_length = new_size; - if (!m_buffer) - m_buffer = (char *)malloc(m_length); - else - m_buffer = (char *)realloc(m_buffer, m_length); - - return (m_buffer != NULL); + delete m_i_streambuf; } -// ---------------------------------------------------------------------------- -// wxMemoryInputStream -// ---------------------------------------------------------------------------- - -wxMemoryInputStream::wxMemoryInputStream(const char *data, size_t len) +char wxMemoryInputStream::Peek() { - 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; + return m_i_streambuf->GetBufferStart()[m_i_streambuf->GetIntPosition()]; } -wxInputStream& wxMemoryInputStream::Read(void *buffer, size_t size) +bool wxMemoryInputStream::Eof() const { - 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->GetBufferPos() == m_i_streambuf->GetBufferEnd(); } -off_t wxMemoryInputStream::SeekI(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_NOERROR; - 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); - } +off_t wxMemoryInputStream::OnSysSeek(off_t pos, wxSeekMode mode) +{ + return m_i_streambuf->Seek(pos, mode); +} - return m_position_i; +off_t wxMemoryInputStream::OnSysTell() const +{ + return m_i_streambuf->Tell(); } // ---------------------------------------------------------------------------- @@ -123,76 +87,50 @@ off_t wxMemoryInputStream::SeekI(off_t pos, wxSeekMode mode) // ---------------------------------------------------------------------------- wxMemoryOutputStream::wxMemoryOutputStream(char *data, size_t len) + : wxOutputStream() { - 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 = new wxStreamBuffer(wxStreamBuffer::write); + if (data) + m_o_streambuf->SetBufferIO(data, data+len); + m_o_streambuf->Fixed(FALSE); + m_o_streambuf->Flushable(FALSE); } -wxOutputStream& wxMemoryOutputStream::Write(const void *buffer, size_t size) +wxMemoryOutputStream::~wxMemoryOutputStream() { - 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; + delete m_o_streambuf; } -off_t wxMemoryOutputStream::SeekO(off_t pos, wxSeekMode mode) +size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes) { - 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; + size_t bufsize = m_o_streambuf->GetBufferEnd() - m_o_streambuf->GetBufferStart(); + size_t oldpos = m_o_streambuf->GetIntPosition(); + m_o_streambuf->Write(buffer, nbytes); + size_t newpos = m_o_streambuf->GetIntPosition(); + if (newpos == 0) return bufsize - oldpos; + else return newpos - oldpos; } -// ---------------------------------------------------------------------------- -// wxMemoryStream -// ---------------------------------------------------------------------------- +off_t wxMemoryOutputStream::OnSysSeek(off_t pos, wxSeekMode mode) +{ + return m_o_streambuf->Seek(pos, mode); +} -wxMemoryStream::wxMemoryStream(char *data, size_t len) - : wxMemoryInputStream(NULL, 0), wxMemoryOutputStream(NULL, 0) +off_t 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(char *buffer, size_t len) const { + if (!buffer) + return 0; + + if (len > GetSize()) + len = GetSize(); + + memcpy(buffer, m_o_streambuf->GetBufferStart(), len); + return len; } + +#endif