]> git.saurik.com Git - wxWidgets.git/commitdiff
added wxMemoryInputStream(wxInputStream&, size_t) ctor (modified patch 1680108)
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 17 Mar 2007 15:28:28 +0000 (15:28 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 17 Mar 2007 15:28:28 +0000 (15:28 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44871 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
docs/latex/wx/strmmem.tex
include/wx/mstream.h
src/common/mstream.cpp
tests/streams/memstream.cpp

index cb44191273b3d2d9218a783673dfe5d6c66a61c9..e810f0beabe72d64826829a989740fddb2db7265 100644 (file)
@@ -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 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:
 
 
 wxGTK:
 
index 64f28a1e5ff6fa275936362a86bc78db742ece99..c2b85338cbfc5027d2501af365062ed37d09d2e4 100644 (file)
@@ -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}.
 
 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}
 \membersection{wxMemoryInputStream::\destruct{wxMemoryInputStream}}\label{wxmemoryinputstreamdtor}
 
 \func{}{\destruct{wxMemoryInputStream}}{\void}
index ef344236c35740c1b415b2529cc0b0ba00f6909e..e5eb69156673ab2fe2b5d3e682d001ae5d780797 100644 (file)
@@ -25,6 +25,16 @@ class WXDLLIMPEXP_BASE wxMemoryInputStream : public wxInputStream
 public:
     wxMemoryInputStream(const void *data, size_t length);
     wxMemoryInputStream(const wxMemoryOutputStream& stream);
 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; }
     virtual ~wxMemoryInputStream();
     virtual wxFileOffset GetLength() const { return m_length; }
     virtual bool IsSeekable() const { return true; }
@@ -46,9 +56,13 @@ protected:
     wxFileOffset OnSysTell() const;
 
 private:
     wxFileOffset OnSysTell() const;
 
 private:
+    // common part of ctors taking wxInputStream
+    void InitFromStream(wxInputStream& stream, wxFileOffset lenFile);
+
     size_t m_length;
 
     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
 };
 
 class WXDLLIMPEXP_BASE wxMemoryOutputStream : public wxOutputStream
index f7786777284e9b4c4e24f0a1a116347aba08e98d..b9fef96e8c7d250075b920d51451fef72cafb407 100644 (file)
@@ -73,6 +73,30 @@ wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream)
     m_length = len;
 }
 
     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;
 wxMemoryInputStream::~wxMemoryInputStream()
 {
     delete m_i_streambuf;
index 6b5d2e610306bbc8594617227b8144bdcd053dd4..731a9f6a0de8a9ff4599712e87c76eb239aabf71 100644 (file)
@@ -56,11 +56,13 @@ public:
         CPPUNIT_TEST(Output_TellO);
 
         // Other test specific for Memory stream test case.
         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.
         CPPUNIT_TEST(Ctor_InFromOut);
     CPPUNIT_TEST_SUITE_END();
 
 protected:
     // Add own test here.
+    void Ctor_InFromIn();
     void Ctor_InFromOut();
 
 private:
     void Ctor_InFromOut();
 
 private:
@@ -105,20 +107,53 @@ wxMemoryOutputStream *memStream::DoCreateOutStream()
     return pMemOutStream;
 }
 
     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());
 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);
     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.
 }
 
 // Register the stream sub suite, by using some stream helper macro.