]>
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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();
63 size_t pos
= m_i_streambuf
->GetIntPosition();
64 if ( pos
== m_length
)
66 m_lasterror
= wxSTREAM_READ_ERROR
;
74 bool wxMemoryInputStream::Eof() const
76 return !m_i_streambuf
->GetBytesLeft();
79 size_t wxMemoryInputStream::OnSysRead(void *buffer
, size_t nbytes
)
81 size_t pos
= m_i_streambuf
->GetIntPosition();
82 if ( pos
== m_length
)
84 m_lasterror
= wxSTREAM_EOF
;
89 m_i_streambuf
->Read(buffer
, nbytes
);
90 m_lasterror
= wxSTREAM_NO_ERROR
;
92 return m_i_streambuf
->GetIntPosition() - pos
;
95 wxFileOffset
wxMemoryInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
97 return m_i_streambuf
->Seek(pos
, mode
);
100 wxFileOffset
wxMemoryInputStream::OnSysTell() const
102 return m_i_streambuf
->Tell();
105 // ----------------------------------------------------------------------------
106 // wxMemoryOutputStream
107 // ----------------------------------------------------------------------------
109 wxMemoryOutputStream::wxMemoryOutputStream(void *data
, size_t len
)
111 m_o_streambuf
= new wxStreamBuffer(wxStreamBuffer::write
);
113 m_o_streambuf
->SetBufferIO(data
, len
);
114 m_o_streambuf
->Fixed(false);
115 m_o_streambuf
->Flushable(false);
118 wxMemoryOutputStream::~wxMemoryOutputStream()
120 delete m_o_streambuf
;
123 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer
, size_t nbytes
)
125 size_t oldpos
= m_o_streambuf
->GetIntPosition();
126 m_o_streambuf
->Write(buffer
, nbytes
);
127 size_t newpos
= m_o_streambuf
->GetIntPosition();
129 // FIXME can someone please explain what this does? (VZ)
131 newpos
= m_o_streambuf
->GetBufferSize();
133 return newpos
- oldpos
;
136 wxFileOffset
wxMemoryOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
138 return m_o_streambuf
->Seek(pos
, mode
);
141 wxFileOffset
wxMemoryOutputStream::OnSysTell() const
143 return m_o_streambuf
->Tell();
146 size_t wxMemoryOutputStream::CopyTo(void *buffer
, size_t len
) const
148 wxCHECK_MSG( buffer
, 0, _T("must have buffer to CopyTo") );
150 if ( len
> GetSize() )
153 memcpy(buffer
, m_o_streambuf
->GetBufferStart(), len
);
158 #endif // wxUSE_STREAMS