]>
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 // ----------------------------------------------------------------------------
21 #pragma implementation "mstream.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/stream.h"
35 #include "wx/mstream.h"
37 // ============================================================================
39 // ============================================================================
41 // ----------------------------------------------------------------------------
42 // wxMemoryInputStream
43 // ----------------------------------------------------------------------------
45 wxMemoryInputStream::wxMemoryInputStream(const void *data
, size_t len
)
47 m_i_streambuf
= new wxStreamBuffer(wxStreamBuffer::read
);
48 m_i_streambuf
->SetBufferIO((void *)data
, len
); // const_cast
49 m_i_streambuf
->SetIntPosition(0); // seek to start pos
50 m_i_streambuf
->Fixed(TRUE
);
55 wxMemoryInputStream::~wxMemoryInputStream()
60 char wxMemoryInputStream::Peek()
62 char *buf
= (char *)m_i_streambuf
->GetBufferStart();
64 return buf
[m_i_streambuf
->GetIntPosition()];
67 bool wxMemoryInputStream::Eof() const
69 return !m_i_streambuf
->GetBytesLeft();
72 size_t wxMemoryInputStream::OnSysRead(void *buffer
, size_t nbytes
)
74 size_t pos
= m_i_streambuf
->GetIntPosition();
75 if ( pos
== m_length
)
77 m_lasterror
= wxSTREAM_EOF
;
82 m_i_streambuf
->Read(buffer
, nbytes
);
83 m_lasterror
= wxSTREAM_NO_ERROR
;
85 return m_i_streambuf
->GetIntPosition() - pos
;
88 off_t
wxMemoryInputStream::OnSysSeek(off_t pos
, wxSeekMode mode
)
90 return m_i_streambuf
->Seek(pos
, mode
);
93 off_t
wxMemoryInputStream::OnSysTell() const
95 return m_i_streambuf
->Tell();
98 // ----------------------------------------------------------------------------
99 // wxMemoryOutputStream
100 // ----------------------------------------------------------------------------
102 wxMemoryOutputStream::wxMemoryOutputStream(void *data
, size_t len
)
104 m_o_streambuf
= new wxStreamBuffer(wxStreamBuffer::write
);
106 m_o_streambuf
->SetBufferIO(data
, len
);
107 m_o_streambuf
->Fixed(FALSE
);
108 m_o_streambuf
->Flushable(FALSE
);
111 wxMemoryOutputStream::~wxMemoryOutputStream()
113 delete m_o_streambuf
;
116 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer
, size_t nbytes
)
118 size_t oldpos
= m_o_streambuf
->GetIntPosition();
119 m_o_streambuf
->Write(buffer
, nbytes
);
120 size_t newpos
= m_o_streambuf
->GetIntPosition();
122 // FIXME can someone please explain what this does? (VZ)
124 newpos
= m_o_streambuf
->GetBufferSize();
126 return newpos
- oldpos
;
129 off_t
wxMemoryOutputStream::OnSysSeek(off_t pos
, wxSeekMode mode
)
131 return m_o_streambuf
->Seek(pos
, mode
);
134 off_t
wxMemoryOutputStream::OnSysTell() const
136 return m_o_streambuf
->Tell();
139 size_t wxMemoryOutputStream::CopyTo(void *buffer
, size_t len
) const
141 wxCHECK_MSG( buffer
, 0, _T("must have buffer to CopyTo") );
143 if ( len
> GetSize() )
146 memcpy(buffer
, m_o_streambuf
->GetBufferStart(), len
);
151 #endif // wxUSE_STREAMS