]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/stream.cpp
unused param warning fixed
[wxWidgets.git] / src / common / stream.cpp
index 774f0b129b3e6eff63343979fd16f246b007e688..ed5190b1dc8480c49c0b3b40eac53bd2a987992c 100644 (file)
@@ -27,9 +27,9 @@
 #if wxUSE_STREAMS
 
 #include <ctype.h>
 #if wxUSE_STREAMS
 
 #include <ctype.h>
-#include <wx/stream.h>
-#include <wx/datstrm.h>
-#include <wx/objstrm.h>
+#include "wx/stream.h"
+#include "wx/datstrm.h"
+#include "wx/objstrm.h"
 
 #define BUF_TEMP_SIZE 10000
 
 
 #define BUF_TEMP_SIZE 10000
 
@@ -165,10 +165,17 @@ void wxStreamBuffer::PutToBuffer(const void *buffer, size_t size)
   size_t s_toput = m_buffer_end-m_buffer_pos;
 
   if (s_toput < size && !m_fixed) {
   size_t s_toput = m_buffer_end-m_buffer_pos;
 
   if (s_toput < size && !m_fixed) {
-    m_buffer_start = (char *)realloc(m_buffer_start, m_buffer_size+size);
-    // I round a bit
-    m_buffer_size += size;
-    m_buffer_end = m_buffer_start+m_buffer_size;
+    if (!m_buffer_start)
+      SetBufferIO(size);
+    else {
+      size_t delta = m_buffer_pos-m_buffer_start;
+
+      m_buffer_start = (char *)realloc(m_buffer_start, m_buffer_size+size);
+      m_buffer_pos   = m_buffer_start + delta;
+      // I round a bit
+      m_buffer_size += size;
+      m_buffer_end   = m_buffer_start+m_buffer_size;
+    }
     s_toput = size;
   }
   if (s_toput > size)
     s_toput = size;
   }
   if (s_toput > size)
@@ -195,6 +202,23 @@ void wxStreamBuffer::PutChar(char c)
   m_stream->m_lastcount = 1;
 }
 
   m_stream->m_lastcount = 1;
 }
 
+char wxStreamBuffer::Peek()
+{
+  char c;
+
+  wxASSERT(m_stream != NULL && m_buffer_size != 0);
+
+  if (!GetDataLeft()) {
+    CHECK_ERROR(wxStream_READ_ERR);
+    return 0;
+  }
+
+  GetFromBuffer(&c, 1);
+  m_buffer_pos--;
+
+  return c;
+}
+
 char wxStreamBuffer::GetChar()
 {
   char c;
 char wxStreamBuffer::GetChar()
 {
   char c;
@@ -248,7 +272,7 @@ size_t wxStreamBuffer::Read(void *buffer, size_t size)
       buffer = (char *)buffer + buf_left; // ANSI C++ violation.
 
       if (!FillBuffer()) {
       buffer = (char *)buffer + buf_left; // ANSI C++ violation.
 
       if (!FillBuffer()) {
-        CHECK_ERROR(wxStream_READ_ERR);
+        CHECK_ERROR(wxStream_EOF);
         return (m_stream->m_lastcount = orig_size-size);
       }
     } else {
         return (m_stream->m_lastcount = orig_size-size);
       }
     } else {
@@ -289,7 +313,7 @@ size_t wxStreamBuffer::Write(const void *buffer, size_t size)
   // ------------------
 
   m_stream->m_lasterror = wxStream_NOERROR;
   // ------------------
 
   m_stream->m_lasterror = wxStream_NOERROR;
-  if (!m_buffer_size)
+  if (!m_buffer_size && m_fixed)
     return (m_stream->m_lastcount = m_stream->OnSysWrite(buffer, size));
 
   // ------------------
     return (m_stream->m_lastcount = m_stream->OnSysWrite(buffer, size));
 
   // ------------------
@@ -303,7 +327,9 @@ size_t wxStreamBuffer::Write(const void *buffer, size_t size)
 
     // First case: the buffer to write is larger than the stream buffer,
     //             we split it
 
     // First case: the buffer to write is larger than the stream buffer,
     //             we split it
-    if (size > buf_left) {
+    // NB: If stream buffer isn't fixed (as for wxMemoryOutputStream),
+    // we always go to the second case.
+    if (size > buf_left && m_fixed) {
       PutToBuffer(buffer, buf_left);
       size -= buf_left;
       buffer = (char *)buffer + buf_left; // ANSI C++ violation.
       PutToBuffer(buffer, buf_left);
       size -= buf_left;
       buffer = (char *)buffer + buf_left; // ANSI C++ violation.
@@ -373,11 +399,7 @@ off_t wxStreamBuffer::Seek(off_t pos, wxSeekMode mode)
     if ( (diff > last_access) || (diff < 0) ) {
       // We must take into account the fact that we have read something
       // previously.
     if ( (diff > last_access) || (diff < 0) ) {
       // We must take into account the fact that we have read something
       // previously.
-      if (diff < 0)
-        ret_off = m_stream->OnSysSeek(-(last_access-diff), wxFromCurrent);
-           // lastaccess + abs(diff) = lastaccess - diff here
-      else
-        ret_off = m_stream->OnSysSeek(diff-last_access, wxFromCurrent);
+      ret_off = m_stream->OnSysSeek(diff-last_access, wxFromCurrent);
       ResetBuffer();
       return ret_off;
     } else {
       ResetBuffer();
       return ret_off;
     } else {
@@ -467,7 +489,9 @@ wxInputStream::~wxInputStream()
 char *wxInputStream::AllocSpaceWBack(size_t needed_size)
 {
   char *temp_b;
 char *wxInputStream::AllocSpaceWBack(size_t needed_size)
 {
   char *temp_b;
+  size_t old_size;
 
 
+  old_size = m_wbacksize;
   m_wbacksize += needed_size;
 
   if (!m_wback)
   m_wbacksize += needed_size;
 
   if (!m_wback)
@@ -478,8 +502,10 @@ char *wxInputStream::AllocSpaceWBack(size_t needed_size)
   if (!temp_b)
     return NULL;
   m_wback = temp_b;
   if (!temp_b)
     return NULL;
   m_wback = temp_b;
+
+  memmove(m_wback+needed_size, m_wback, old_size);
   
   
-  return (char *)(m_wback+(m_wbacksize-needed_size));
+  return (char *)(m_wback);
 }
 
 size_t wxInputStream::GetWBack(char *buf, size_t bsize)
 }
 
 size_t wxInputStream::GetWBack(char *buf, size_t bsize)
@@ -580,6 +606,10 @@ wxInputStream& wxInputStream::Read(wxOutputStream& stream_out)
 
 off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode)
 {
 
 off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode)
 {
+  //should be check and improve, just to remove a slight bug !
+  // I don't know whether it should be put as well in wxFileInputStream::OnSysSeek ?
+  if (m_lasterror==wxSTREAM_EOF) m_lasterror=wxSTREAM_NOERROR;
+
   return OnSysSeek(pos, mode);
 }
 
   return OnSysSeek(pos, mode);
 }
 
@@ -649,9 +679,56 @@ wxOutputStream& wxOutputStream::operator<<(wxObject& obj)
 }
 #endif
 
 }
 #endif
 
+// ----------------------------------------------------------------------------
+// wxCountingOutputStream
+// ----------------------------------------------------------------------------
+
+wxCountingOutputStream::wxCountingOutputStream ()
+  : wxOutputStream()
+{
+   m_currentPos = 0;
+}
+
+size_t wxCountingOutputStream::GetSize() const
+{
+  return m_lastcount;
+}
+
+size_t wxCountingOutputStream::OnSysWrite(const void *WXUNUSED(buffer), size_t size)
+{
+  m_currentPos += size;
+  if (m_currentPos > m_lastcount) m_lastcount = m_currentPos;
+  return m_currentPos;
+}
+
+off_t wxCountingOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
+{
+  if (mode == wxFromStart)
+  {
+    m_currentPos = pos;
+  }
+  if (mode == wxFromEnd)
+  {
+    m_currentPos = m_lastcount + pos;
+  }
+  else
+  {
+    m_currentPos += pos;
+  }
+  if (m_currentPos > m_lastcount) m_lastcount = m_currentPos;
+  
+  return m_currentPos;  // ?
+}
+
+off_t wxCountingOutputStream::OnSysTell() const
+{
+  return m_currentPos;  // ?
+}
+  
 // ----------------------------------------------------------------------------
 // wxFilterInputStream
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // wxFilterInputStream
 // ----------------------------------------------------------------------------
+
 wxFilterInputStream::wxFilterInputStream()
   : wxInputStream()
 {
 wxFilterInputStream::wxFilterInputStream()
   : wxInputStream()
 {
@@ -700,6 +777,11 @@ wxBufferedInputStream::~wxBufferedInputStream()
   delete m_i_streambuf;
 }
 
   delete m_i_streambuf;
 }
 
+char wxBufferedInputStream::Peek()
+{
+  return m_i_streambuf->Peek();
+}
+
 wxInputStream& wxBufferedInputStream::Read(void *buffer, size_t size)
 {
   size_t retsize;
 wxInputStream& wxBufferedInputStream::Read(void *buffer, size_t size)
 {
   size_t retsize;