]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/stream.cpp
include/wx/choice.hpragma warning is only for VC++
[wxWidgets.git] / src / common / stream.cpp
index 9b83250f1c1886bd6c44f35f573c0c8b77904e84..6698b43ad89a3f74494f666555ba952d10d3f825 100644 (file)
@@ -195,6 +195,23 @@ void wxStreamBuffer::PutChar(char c)
   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;
@@ -248,7 +265,7 @@ size_t wxStreamBuffer::Read(void *buffer, size_t size)
       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 {
@@ -371,7 +388,9 @@ off_t wxStreamBuffer::Seek(off_t pos, wxSeekMode mode)
     diff = pos + GetIntPosition();
 
     if ( (diff > last_access) || (diff < 0) ) {
-      ret_off = m_stream->OnSysSeek(pos, wxFromCurrent);
+      // We must take into account the fact that we have read something
+      // previously.
+      ret_off = m_stream->OnSysSeek(diff-last_access, wxFromCurrent);
       ResetBuffer();
       return ret_off;
     } else {
@@ -461,7 +480,9 @@ wxInputStream::~wxInputStream()
 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)
@@ -472,21 +493,27 @@ char *wxInputStream::AllocSpaceWBack(size_t needed_size)
   if (!temp_b)
     return NULL;
   m_wback = temp_b;
+  m_wbackcur += needed_size;
+
+  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 s_toget = m_wbacksize-m_wbackcur;
+  size_t s_toget = m_wbackcur;
+
+  if (!m_wback)
+    return 0;
 
   if (bsize < s_toget)
     s_toget = bsize;
 
-  memcpy(buf, (m_wback+m_wbackcur), s_toget);
+  memcpy(buf, (m_wback+m_wbackcur-bsize), s_toget);
 
-  m_wbackcur += s_toget;
-  if (m_wbackcur == m_wbacksize) {
+  m_wbackcur -= s_toget;
+  if (m_wbackcur == 0) {
     free(m_wback);
     m_wback = (char *)NULL;
     m_wbacksize = 0;
@@ -496,7 +523,7 @@ size_t wxInputStream::GetWBack(char *buf, size_t bsize)
   return s_toget;
 }
 
-size_t wxInputStream::Ungetch(void *buf, size_t bufsize)
+size_t wxInputStream::Ungetch(const void *buf, size_t bufsize)
 {
   char *ptrback;
 
@@ -571,12 +598,16 @@ wxInputStream& wxInputStream::Read(wxOutputStream& stream_out)
 
 off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode)
 {
-  return wxInvalidOffset;
+  //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);
 }
 
 off_t wxInputStream::TellI() const
 {
-  return wxInvalidOffset;
+  return OnSysTell();
 }
 
 // --------------------
@@ -619,12 +650,12 @@ wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in)
 
 off_t wxOutputStream::TellO() const
 {
-  return wxInvalidOffset;
+  return OnSysTell();
 }
 
 off_t wxOutputStream::SeekO(off_t pos, wxSeekMode mode)
 {
-  return wxInvalidOffset;
+  return OnSysSeek(pos, mode);
 }
 
 void wxOutputStream::Sync()
@@ -640,9 +671,56 @@ wxOutputStream& wxOutputStream::operator<<(wxObject& obj)
 }
 #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()
   : wxInputStream()
 {
@@ -691,14 +769,19 @@ wxBufferedInputStream::~wxBufferedInputStream()
   delete m_i_streambuf;
 }
 
+char wxBufferedInputStream::Peek()
+{
+  return m_i_streambuf->Peek();
+}
+
 wxInputStream& wxBufferedInputStream::Read(void *buffer, size_t size)
 {
   size_t retsize;
   char *buf = (char *)buffer;
 
   retsize = GetWBack(buf, size);
+  m_lastcount = retsize;
   if (retsize == size) {
-    m_lastcount = size;
     m_lasterror = wxStream_NOERROR;
     return *this;
   }
@@ -753,6 +836,7 @@ wxBufferedOutputStream::~wxBufferedOutputStream()
 
 wxOutputStream& wxBufferedOutputStream::Write(const void *buffer, size_t size)
 {
+  m_lastcount = 0;
   m_o_streambuf->Write(buffer, size);
   return *this;
 }