]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/mstream.cpp
added wxEXEC_NOHIDE flag, modified wxExecute() to take a combination of bit flags...
[wxWidgets.git] / src / common / mstream.cpp
index 2bfa364e1ea80409943ddad0e0d766d5f03a634c..9226ce4461282164c3f847f24f1b9feb85ec5fac 100644 (file)
 /////////////////////////////////////////////////////////////////////////////
-// 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
 /////////////////////////////////////////////////////////////////////////////
 
+// ============================================================================
+// declarations
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
 #ifdef __GNUG__
 #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 <stdlib.h>
 #include "wx/stream.h"
 #include "wx/mstream.h"
 
-#if !USE_SHARED_LIBRARY
-IMPLEMENT_CLASS(wxMemoryStreamBase, wxStream)
-IMPLEMENT_CLASS(wxMemoryInputStream, wxMemoryStreamBase)
-//IMPLEMENT_DYNAMIC_CLASS(wxMemoryOutputStream, wxMemoryStreamBase)
-//IMPLEMENT_DYNAMIC_CLASS(wxMemoryStream, wxMemoryStreamBase)
-#endif
+// ============================================================================
+// implementation
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// wxMemoryInputStream
+// ----------------------------------------------------------------------------
 
-wxMemoryStreamBase::wxMemoryStreamBase(char *data, size_t length, int iolimit)
+wxMemoryInputStream::wxMemoryInputStream(const void *data, size_t len)
 {
-  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((void *)data, len); // const_cast
+    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;
+    char *buf = (char *)m_i_streambuf->GetBufferStart();
+
+    return buf[m_i_streambuf->GetIntPosition()];
 }
 
-size_t wxMemoryStreamBase::SeekI(int pos, wxWhenceType whence)
+bool wxMemoryInputStream::Eof() const
 {
-  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);
+    return !m_i_streambuf->GetBytesLeft();
+}
 
-  case wxCurrentPosition:
-    if ((size_t)(m_position_i+pos) > m_length)
-      return m_position_i;
+size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
+{
+    size_t pos = m_i_streambuf->GetIntPosition();
+    if ( pos == m_length )
+    {
+        m_lasterror = wxSTREAM_EOF;
 
-    return (m_position_i += pos);
+        return 0;
+    }
 
-  case wxEndPosition:
-    if ((size_t)(m_length-pos) > m_length)
-      return m_position_i;
+    m_i_streambuf->Read(buffer, nbytes);
+    m_lasterror = wxSTREAM_NOERROR;
 
-    return (m_position_i = m_length-pos);
-  }
+    return m_i_streambuf->GetIntPosition() - pos;
+}
 
-  return m_position_i;
+off_t wxMemoryInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
+{
+    return m_i_streambuf->Seek(pos, mode);
 }
 
-wxOutputStream& wxMemoryStreamBase::Write(const void *buffer, size_t size)
+off_t wxMemoryInputStream::OnSysTell() const
 {
-  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;
-    }
+    return m_i_streambuf->Tell();
+}
 
-  memcpy(m_buffer+m_position_o, buffer, size);
-  m_position_o += size;
-  m_lastwrite = size;
+// ----------------------------------------------------------------------------
+// wxMemoryOutputStream
+// ----------------------------------------------------------------------------
 
-  return *this;
+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);
 }
 
-size_t wxMemoryStreamBase::SeekO(int pos, wxWhenceType whence)
+wxMemoryOutputStream::~wxMemoryOutputStream()
 {
-  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);
+    delete m_o_streambuf;
+}
 
-  case wxCurrentPosition:
-    if ((size_t)(m_position_o+pos) > m_length)
-      return m_position_o;
+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 += pos);
+    // FIXME can someone please explain what this does? (VZ)
+    if ( !newpos )
+        newpos = m_o_streambuf->GetBufferSize();
 
-  case wxEndPosition:
-    if ((size_t)(m_length-pos) > m_length)
-      return m_position_o;
+    return newpos - oldpos;
+}
 
-    return (m_position_o = m_length-pos);
-  }
+off_t wxMemoryOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
+{
+    return m_o_streambuf->Seek(pos, mode);
+}
 
-  return m_position_o;
+off_t 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, _T("must have buffer to CopyTo") );
 
-  return (m_buffer != NULL);
+    if ( len > GetSize() )
+        len = GetSize();
+
+    memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
+
+    return len;
 }
+
+#endif // wxUSE_STREAMS