From 764471557e06c8816b8cc8eb9924a1e3c78509d8 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sat, 17 Mar 2007 15:28:28 +0000
Subject: [PATCH] added wxMemoryInputStream(wxInputStream&, size_t) ctor
 (modified patch 1680108)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44871 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/changes.txt            |  1 +
 docs/latex/wx/strmmem.tex   | 10 ++++++++++
 include/wx/mstream.h        | 16 ++++++++++++++-
 src/common/mstream.cpp      | 24 +++++++++++++++++++++++
 tests/streams/memstream.cpp | 39 +++++++++++++++++++++++++++++++++++--
 5 files changed, 87 insertions(+), 3 deletions(-)

diff --git a/docs/changes.txt b/docs/changes.txt
index cb44191273..e810f0beab 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -70,6 +70,7 @@ All:
 - Added wxJoystick::GetButtonState/Position() (Frank C Szczerba)
 - Added wxGridUpdateLocker helper class (Evgeniy Tarassov)
 - Support wxGRID_AUTOSIZE in wxGrid::SetRow/ColLabelSize() (Evgeniy Tarassov)
+- Added wxMemoryInputStream(wxInputStream&) ctor (Stas Sergeev)
 
 wxGTK:
 
diff --git a/docs/latex/wx/strmmem.tex b/docs/latex/wx/strmmem.tex
index 64f28a1e5f..c2b85338cb 100644
--- a/docs/latex/wx/strmmem.tex
+++ b/docs/latex/wx/strmmem.tex
@@ -33,6 +33,16 @@ buffer, i.e. the buffer will not be deleted in its destructor.
 Creates a new read-only memory stream, initializing it with the
 data from the given output stream \arg{stream}.
 
+\func{}{wxMemoryInputStream}{\param{wxInputStream\&}{ stream}, \param{wxFileOffset}{ len = wxInvalidOffset}}
+
+Creates a new read-only memory stream, initializing it with the
+data from the given input stream \arg{stream}.
+
+The \arg{len} argument specifies the amount of data to read from
+the \arg{stream}. Setting it to {\it wxInvalidOffset} means that
+the \arg{stream} is to be read entirely (i.e. till the EOF is reached).
+
+
 \membersection{wxMemoryInputStream::\destruct{wxMemoryInputStream}}\label{wxmemoryinputstreamdtor}
 
 \func{}{\destruct{wxMemoryInputStream}}{\void}
diff --git a/include/wx/mstream.h b/include/wx/mstream.h
index ef344236c3..e5eb691566 100644
--- a/include/wx/mstream.h
+++ b/include/wx/mstream.h
@@ -25,6 +25,16 @@ class WXDLLIMPEXP_BASE wxMemoryInputStream : public wxInputStream
 public:
     wxMemoryInputStream(const void *data, size_t length);
     wxMemoryInputStream(const wxMemoryOutputStream& stream);
+    wxMemoryInputStream(wxInputStream& stream,
+                        wxFileOffset lenFile = wxInvalidOffset)
+    {
+        InitFromStream(stream, lenFile);
+    }
+    wxMemoryInputStream(wxMemoryInputStream& stream)
+    {
+        InitFromStream(stream, wxInvalidOffset);
+    }
+
     virtual ~wxMemoryInputStream();
     virtual wxFileOffset GetLength() const { return m_length; }
     virtual bool IsSeekable() const { return true; }
@@ -46,9 +56,13 @@ protected:
     wxFileOffset OnSysTell() const;
 
 private:
+    // common part of ctors taking wxInputStream
+    void InitFromStream(wxInputStream& stream, wxFileOffset lenFile);
+
     size_t m_length;
 
-    DECLARE_NO_COPY_CLASS(wxMemoryInputStream)
+    // copy ctor is implemented above: it copies the other stream in this one
+    DECLARE_NO_ASSIGN_CLASS(wxMemoryInputStream)
 };
 
 class WXDLLIMPEXP_BASE wxMemoryOutputStream : public wxOutputStream
diff --git a/src/common/mstream.cpp b/src/common/mstream.cpp
index f778677728..b9fef96e8c 100644
--- a/src/common/mstream.cpp
+++ b/src/common/mstream.cpp
@@ -73,6 +73,30 @@ wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream)
     m_length = len;
 }
 
+void
+wxMemoryInputStream::InitFromStream(wxInputStream& stream, wxFileOffset lenFile)
+{
+    if ( lenFile == wxInvalidOffset )
+        lenFile = stream.GetLength();
+
+    if ( lenFile == wxInvalidOffset )
+    {
+        m_i_streambuf = NULL;
+        m_lasterror = wxSTREAM_EOF;
+        return;
+    }
+
+    const size_t len = wx_truncate_cast(size_t, lenFile);
+    wxASSERT_MSG( (wxFileOffset)len == lenFile, _T("huge files not supported") );
+
+    m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read);
+    m_i_streambuf->SetBufferIO(len); // create buffer
+    stream.Read(m_i_streambuf->GetBufferStart(), len);
+    m_i_streambuf->SetIntPosition(0); // seek to start pos
+    m_i_streambuf->Fixed(true);
+    m_length = stream.LastRead();
+}
+
 wxMemoryInputStream::~wxMemoryInputStream()
 {
     delete m_i_streambuf;
diff --git a/tests/streams/memstream.cpp b/tests/streams/memstream.cpp
index 6b5d2e6103..731a9f6a0d 100644
--- a/tests/streams/memstream.cpp
+++ b/tests/streams/memstream.cpp
@@ -56,11 +56,13 @@ public:
         CPPUNIT_TEST(Output_TellO);
 
         // Other test specific for Memory stream test case.
+        CPPUNIT_TEST(Ctor_InFromIn);
         CPPUNIT_TEST(Ctor_InFromOut);
     CPPUNIT_TEST_SUITE_END();
 
 protected:
     // Add own test here.
+    void Ctor_InFromIn();
     void Ctor_InFromOut();
 
 private:
@@ -105,20 +107,53 @@ wxMemoryOutputStream *memStream::DoCreateOutStream()
     return pMemOutStream;
 }
 
+void memStream::Ctor_InFromIn()
+{
+    wxMemoryInputStream *pMemInStream1 = DoCreateInStream();
+    wxMemoryInputStream *pMemInStream2 = new wxMemoryInputStream(*pMemInStream1);
+    CPPUNIT_ASSERT(pMemInStream2->IsOk());
+    CPPUNIT_ASSERT_EQUAL(pMemInStream1->GetLength(), pMemInStream2->GetLength());
+    size_t len = pMemInStream2->GetLength();
+    char *dat = new char[len];
+    pMemInStream2->Read(dat, len);
+    CPPUNIT_ASSERT_EQUAL(len, pMemInStream2->LastRead());
+    wxStreamBuffer *buf = pMemInStream1->GetInputStreamBuffer();
+    void *pIn = buf->GetBufferStart();
+    CPPUNIT_ASSERT(memcmp(pIn, dat, len) == 0);
+    delete pMemInStream2;
+
+    size_t len2 = len / 2;
+    CPPUNIT_ASSERT(len2);
+    CPPUNIT_ASSERT(pMemInStream1->SeekI(-len2, wxFromCurrent) != wxInvalidOffset);
+    pIn = buf->GetBufferPos();
+    pMemInStream2 = new wxMemoryInputStream(*pMemInStream1, len2);
+    CPPUNIT_ASSERT(pMemInStream2->IsOk());
+    CPPUNIT_ASSERT_EQUAL((wxFileOffset)len2, pMemInStream2->GetLength());
+    pMemInStream2->Read(dat, len2);
+    CPPUNIT_ASSERT_EQUAL(len2, pMemInStream2->LastRead());
+    CPPUNIT_ASSERT(memcmp(pIn, dat, len2) == 0);
+
+    delete[] dat;
+    delete pMemInStream2;
+    delete pMemInStream1;
+}
+
 void memStream::Ctor_InFromOut()
 {
     wxMemoryOutputStream *pMemOutStream = DoCreateOutStream();
     pMemOutStream->Write(GetDataBuffer(), DATABUFFER_SIZE);
     wxMemoryInputStream *pMemInStream = new wxMemoryInputStream(*pMemOutStream);
     CPPUNIT_ASSERT(pMemInStream->IsOk());
-    CPPUNIT_ASSERT(pMemInStream->GetLength() == pMemOutStream->GetLength());
-    int len = pMemInStream->GetLength();
+    CPPUNIT_ASSERT_EQUAL(pMemInStream->GetLength(), pMemOutStream->GetLength());
+    size_t len = pMemInStream->GetLength();
     wxStreamBuffer *in = pMemInStream->GetInputStreamBuffer();
     wxStreamBuffer *out = pMemOutStream->GetOutputStreamBuffer();
     void *pIn = in->GetBufferStart();
     void *pOut = out->GetBufferStart();
     CPPUNIT_ASSERT(pIn != pOut);
     CPPUNIT_ASSERT(memcmp(pIn, pOut, len) == 0);
+    delete pMemInStream;
+    delete pMemOutStream;
 }
 
 // Register the stream sub suite, by using some stream helper macro.
-- 
2.50.0

