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
This commit is contained in:
Vadim Zeitlin 2007-03-17 15:28:28 +00:00
parent badea31707
commit 764471557e
5 changed files with 87 additions and 3 deletions

View 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 wxMemoryInputStream(wxInputStream&) ctor (Stas Sergeev)
wxGTK:

View 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}.
\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}

View File

@ -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

View File

@ -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;

View File

@ -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.