]>
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"
29 #include "wx/mstream.h"
32 #include "wx/stream.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(const wxMemoryOutputStream
& stream
)
57 const wxFileOffset lenFile
= stream
.GetLength();
58 if ( lenFile
== wxInvalidOffset
)
61 m_lasterror
= wxSTREAM_EOF
;
65 const size_t len
= wx_truncate_cast(size_t, lenFile
);
66 wxASSERT_MSG( len
== lenFile
+ size_t(0), _T("huge files not supported") );
68 m_i_streambuf
= new wxStreamBuffer(wxStreamBuffer::read
);
69 m_i_streambuf
->SetBufferIO(len
); // create buffer
70 stream
.CopyTo(m_i_streambuf
->GetBufferStart(), len
);
71 m_i_streambuf
->SetIntPosition(0); // seek to start pos
72 m_i_streambuf
->Fixed(true);
76 wxMemoryInputStream::~wxMemoryInputStream()
81 char wxMemoryInputStream::Peek()
83 char *buf
= (char *)m_i_streambuf
->GetBufferStart();
84 size_t pos
= m_i_streambuf
->GetIntPosition();
85 if ( pos
== m_length
)
87 m_lasterror
= wxSTREAM_READ_ERROR
;
95 size_t wxMemoryInputStream::OnSysRead(void *buffer
, size_t nbytes
)
97 size_t pos
= m_i_streambuf
->GetIntPosition();
98 if ( pos
== m_length
)
100 m_lasterror
= wxSTREAM_EOF
;
105 m_i_streambuf
->Read(buffer
, nbytes
);
106 m_lasterror
= wxSTREAM_NO_ERROR
;
108 return m_i_streambuf
->GetIntPosition() - pos
;
111 wxFileOffset
wxMemoryInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
113 return m_i_streambuf
->Seek(pos
, mode
);
116 wxFileOffset
wxMemoryInputStream::OnSysTell() const
118 return m_i_streambuf
->Tell();
121 // ----------------------------------------------------------------------------
122 // wxMemoryOutputStream
123 // ----------------------------------------------------------------------------
125 wxMemoryOutputStream::wxMemoryOutputStream(void *data
, size_t len
)
127 m_o_streambuf
= new wxStreamBuffer(wxStreamBuffer::write
);
129 m_o_streambuf
->SetBufferIO(data
, len
);
130 m_o_streambuf
->Fixed(false);
131 m_o_streambuf
->Flushable(false);
134 wxMemoryOutputStream::~wxMemoryOutputStream()
136 delete m_o_streambuf
;
139 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer
, size_t nbytes
)
141 size_t oldpos
= m_o_streambuf
->GetIntPosition();
142 m_o_streambuf
->Write(buffer
, nbytes
);
143 size_t newpos
= m_o_streambuf
->GetIntPosition();
145 // FIXME can someone please explain what this does? (VZ)
147 newpos
= m_o_streambuf
->GetBufferSize();
149 return newpos
- oldpos
;
152 wxFileOffset
wxMemoryOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
154 return m_o_streambuf
->Seek(pos
, mode
);
157 wxFileOffset
wxMemoryOutputStream::OnSysTell() const
159 return m_o_streambuf
->Tell();
162 size_t wxMemoryOutputStream::CopyTo(void *buffer
, size_t len
) const
164 wxCHECK_MSG( buffer
, 0, _T("must have buffer to CopyTo") );
166 if ( len
> GetSize() )
169 memcpy(buffer
, m_o_streambuf
->GetBufferStart(), len
);
174 #endif // wxUSE_STREAMS