]>
git.saurik.com Git - wxWidgets.git/blob - src/common/mstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/mstream.cpp
3 // Purpose: "Memory stream" classes
4 // Author: Guilhem Lavaux
5 // Modified by: VZ (23.11.00): general code review
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
30 #include "wx/stream.h"
31 #include "wx/mstream.h"
33 // ============================================================================
35 // ============================================================================
37 // ----------------------------------------------------------------------------
38 // wxMemoryInputStream
39 // ----------------------------------------------------------------------------
41 wxMemoryInputStream::wxMemoryInputStream(const void *data
, size_t len
)
43 m_i_streambuf
= new wxStreamBuffer(wxStreamBuffer::read
);
44 m_i_streambuf
->SetBufferIO((void *)data
, len
); // const_cast
45 m_i_streambuf
->SetIntPosition(0); // seek to start pos
46 m_i_streambuf
->Fixed(true);
51 wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream
& stream
)
53 const wxFileOffset lenFile
= stream
.GetLength();
54 if ( lenFile
== wxInvalidOffset
)
57 m_lasterror
= wxSTREAM_EOF
;
61 const size_t len
= wx_truncate_cast(size_t, lenFile
);
62 wxASSERT_MSG( len
== lenFile
+ size_t(0), _T("huge files not supported") );
64 m_i_streambuf
= new wxStreamBuffer(wxStreamBuffer::read
);
65 m_i_streambuf
->SetBufferIO(len
); // create buffer
66 stream
.CopyTo(m_i_streambuf
->GetBufferStart(), len
);
67 m_i_streambuf
->SetIntPosition(0); // seek to start pos
68 m_i_streambuf
->Fixed(true);
72 wxMemoryInputStream::~wxMemoryInputStream()
77 char wxMemoryInputStream::Peek()
79 char *buf
= (char *)m_i_streambuf
->GetBufferStart();
80 size_t pos
= m_i_streambuf
->GetIntPosition();
81 if ( pos
== m_length
)
83 m_lasterror
= wxSTREAM_READ_ERROR
;
91 size_t wxMemoryInputStream::OnSysRead(void *buffer
, size_t nbytes
)
93 size_t pos
= m_i_streambuf
->GetIntPosition();
94 if ( pos
== m_length
)
96 m_lasterror
= wxSTREAM_EOF
;
101 m_i_streambuf
->Read(buffer
, nbytes
);
102 m_lasterror
= wxSTREAM_NO_ERROR
;
104 return m_i_streambuf
->GetIntPosition() - pos
;
107 wxFileOffset
wxMemoryInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
109 return m_i_streambuf
->Seek(pos
, mode
);
112 wxFileOffset
wxMemoryInputStream::OnSysTell() const
114 return m_i_streambuf
->Tell();
117 // ----------------------------------------------------------------------------
118 // wxMemoryOutputStream
119 // ----------------------------------------------------------------------------
121 wxMemoryOutputStream::wxMemoryOutputStream(void *data
, size_t len
)
123 m_o_streambuf
= new wxStreamBuffer(wxStreamBuffer::write
);
125 m_o_streambuf
->SetBufferIO(data
, len
);
126 m_o_streambuf
->Fixed(false);
127 m_o_streambuf
->Flushable(false);
130 wxMemoryOutputStream::~wxMemoryOutputStream()
132 delete m_o_streambuf
;
135 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer
, size_t nbytes
)
137 size_t oldpos
= m_o_streambuf
->GetIntPosition();
138 m_o_streambuf
->Write(buffer
, nbytes
);
139 size_t newpos
= m_o_streambuf
->GetIntPosition();
141 // FIXME can someone please explain what this does? (VZ)
143 newpos
= m_o_streambuf
->GetBufferSize();
145 return newpos
- oldpos
;
148 wxFileOffset
wxMemoryOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
150 return m_o_streambuf
->Seek(pos
, mode
);
153 wxFileOffset
wxMemoryOutputStream::OnSysTell() const
155 return m_o_streambuf
->Tell();
158 size_t wxMemoryOutputStream::CopyTo(void *buffer
, size_t len
) const
160 wxCHECK_MSG( buffer
, 0, _T("must have buffer to CopyTo") );
162 if ( len
> GetSize() )
165 memcpy(buffer
, m_o_streambuf
->GetBufferStart(), len
);
170 #endif // wxUSE_STREAMS