From e9f692911da77538e58ed4539be6f7ac3774d320 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 9 Jan 2002 20:18:20 +0000 Subject: [PATCH] bug fix for wxBufferedInputStream::LastCount() after Read() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13483 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/stream.cpp | 46 ++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/common/stream.cpp b/src/common/stream.cpp index 048544a225..3cf1db7f27 100644 --- a/src/common/stream.cpp +++ b/src/common/stream.cpp @@ -719,25 +719,32 @@ char *wxInputStream::AllocSpaceWBack(size_t needed_size) size_t wxInputStream::GetWBack(void *buf, size_t bsize) { - size_t toget = m_wbacksize-m_wbackcur; - if (!m_wback) return 0; - if (bsize < toget) - toget = bsize; + // how many bytes do we have in the buffer? + size_t toget = m_wbacksize - m_wbackcur; + + if ( bsize < toget ) + { + // we won't read everything + toget = bsize; + } - memcpy(buf, (m_wback+m_wbackcur), toget); + // copy the data from the cache + memcpy(buf, m_wback + m_wbackcur, toget); m_wbackcur += toget; - if (m_wbackcur == m_wbacksize) + if ( m_wbackcur == m_wbacksize ) { + // TODO: should we really free it here all the time? maybe keep it? free(m_wback); m_wback = NULL; m_wbacksize = 0; m_wbackcur = 0; } + // return the number of bytes copied return toget; } @@ -1028,19 +1035,26 @@ char wxBufferedInputStream::Peek() wxInputStream& wxBufferedInputStream::Read(void *buf, size_t size) { - size_t retsize; + // reset the error flag + m_lasterror = wxStream_NOERROR; - retsize = GetWBack(buf, size); - m_lastcount = retsize; - if ( retsize == size ) + // first read from the already cached data + m_lastcount = GetWBack(buf, size); + + // do we have to read anything more? + if ( m_lastcount < size ) { - m_lasterror = wxStream_NOERROR; - return *this; - } - size -= retsize; - buf = (char *)buf + retsize; + size -= m_lastcount; + buf = (char *)buf + m_lastcount; - m_i_streambuf->Read(buf, size); + // the call to wxStreamBuffer::Read() below will reset our m_lastcount, + // so save it + size_t countOld = m_lastcount; + + m_i_streambuf->Read(buf, size); + + m_lastcount += countOld; + } return *this; } -- 2.47.2