From df18cc7aa1afdbe74d4b8e398d12ef34d1e78b5c Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sat, 21 Apr 2007 18:11:37 +0000
Subject: [PATCH] added wxStreamBuffer::Truncate() (patch 1687081)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45561 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/changes.txt         |  1 +
 docs/latex/wx/stream.tex | 11 +++++++++++
 include/wx/stream.h      |  1 +
 src/common/stream.cpp    | 22 ++++++++++++++++++++++
 4 files changed, 35 insertions(+)

diff --git a/docs/changes.txt b/docs/changes.txt
index 5805088ffb..3258b885d6 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -81,6 +81,7 @@ All:
 - Implemented wxMemoryInputStream::CanRead()
 - Added wxEXEC_BLOCK flag (Hank Schultz)
 - Add support for wxStream-derived classes to wxRTTI (Stas Sergeev)
+- Added wxStreamBuffer::Truncate() (Stas Sergeev)
 
 All (GUI):
 
diff --git a/docs/latex/wx/stream.tex b/docs/latex/wx/stream.tex
index 9ff128a6ed..9262f10486 100644
--- a/docs/latex/wx/stream.tex
+++ b/docs/latex/wx/stream.tex
@@ -184,6 +184,17 @@ the beginning of the stream. Otherwise, it returns wxInvalidOffset.
 
 Resets to the initial state variables concerning the buffer.
 
+
+\membersection{wxStreamBuffer::Truncate}\label{wxstreambuffertruncate}
+
+\func{void}{Truncate}{\void}
+
+Truncates the buffer to the current position.
+
+Note: Truncate() cannot be used to enlarge the buffer. This is
+usually not needed since the buffer expands automatically.
+
+
 \membersection{wxStreamBuffer::SetBufferIO}\label{wxstreambuffersetbufferio}
 
 \func{void}{SetBufferIO}{\param{char*}{ buffer\_start}, \param{char*}{ buffer\_end}}
diff --git a/include/wx/stream.h b/include/wx/stream.h
index f0c1ce170c..5ef6ccdc4b 100644
--- a/include/wx/stream.h
+++ b/include/wx/stream.h
@@ -434,6 +434,7 @@ public:
 
     // Buffer control
     void ResetBuffer();
+    void Truncate();
 
     // NB: the buffer must always be allocated with malloc() if takeOwn is
     //     true as it will be deallocated by free()
diff --git a/src/common/stream.cpp b/src/common/stream.cpp
index 1f1c94c688..e3d076ad77 100644
--- a/src/common/stream.cpp
+++ b/src/common/stream.cpp
@@ -192,6 +192,28 @@ void wxStreamBuffer::ResetBuffer()
                         : m_buffer_start;
 }
 
+void wxStreamBuffer::Truncate()
+{
+    size_t new_size = m_buffer_pos - m_buffer_start;
+    if ( new_size == m_buffer_size )
+        return;
+
+    if ( !new_size )
+    {
+        FreeBuffer();
+        InitBuffer();
+        return;
+    }
+
+    char *new_start = (char *)realloc(m_buffer_start, new_size);
+    wxCHECK_RET( new_size, _T("shrinking buffer shouldn't fail") );
+
+    m_buffer_start = new_start;
+    m_buffer_size = new_size;
+    m_buffer_end = m_buffer_start + m_buffer_size;
+    m_buffer_pos = m_buffer_end;
+}
+
 // fill the buffer with as much data as possible (only for read buffers)
 bool wxStreamBuffer::FillBuffer()
 {
-- 
2.50.0

