]>
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
7 // Copyright: (c) Guilhem Lavaux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/mstream.h"
31 #include "wx/stream.h"
36 // ============================================================================
38 // ============================================================================
40 // ----------------------------------------------------------------------------
41 // wxMemoryInputStream
42 // ----------------------------------------------------------------------------
44 IMPLEMENT_ABSTRACT_CLASS(wxMemoryInputStream
, wxInputStream
)
46 wxMemoryInputStream::wxMemoryInputStream(const void *data
, size_t len
)
48 m_i_streambuf
= new wxStreamBuffer(wxStreamBuffer::read
);
49 m_i_streambuf
->SetBufferIO(const_cast<void *>(data
), len
);
50 m_i_streambuf
->SetIntPosition(0); // seek to start pos
51 m_i_streambuf
->Fixed(true);
56 wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream
& stream
)
58 const wxFileOffset lenFile
= stream
.GetLength();
59 if ( lenFile
== wxInvalidOffset
)
62 m_lasterror
= wxSTREAM_EOF
;
66 const size_t len
= wx_truncate_cast(size_t, lenFile
);
67 wxASSERT_MSG( len
== lenFile
+ size_t(0), wxT("huge files not supported") );
69 m_i_streambuf
= new wxStreamBuffer(wxStreamBuffer::read
);
70 m_i_streambuf
->SetBufferIO(len
); // create buffer
71 stream
.CopyTo(m_i_streambuf
->GetBufferStart(), len
);
72 m_i_streambuf
->SetIntPosition(0); // seek to start pos
73 m_i_streambuf
->Fixed(true);
78 wxMemoryInputStream::InitFromStream(wxInputStream
& stream
, wxFileOffset lenFile
)
80 if ( lenFile
== wxInvalidOffset
)
81 lenFile
= stream
.GetLength();
83 if ( lenFile
== wxInvalidOffset
)
86 m_lasterror
= wxSTREAM_EOF
;
90 const size_t len
= wx_truncate_cast(size_t, lenFile
);
91 wxASSERT_MSG( (wxFileOffset
)len
== lenFile
, wxT("huge files not supported") );
93 m_i_streambuf
= new wxStreamBuffer(wxStreamBuffer::read
);
94 m_i_streambuf
->SetBufferIO(len
); // create buffer
95 stream
.Read(m_i_streambuf
->GetBufferStart(), len
);
96 m_i_streambuf
->SetIntPosition(0); // seek to start pos
97 m_i_streambuf
->Fixed(true);
98 m_length
= stream
.LastRead();
101 bool wxMemoryInputStream::CanRead() const
103 return m_i_streambuf
->GetIntPosition() != m_length
;
106 wxMemoryInputStream::~wxMemoryInputStream()
108 delete m_i_streambuf
;
111 char wxMemoryInputStream::Peek()
113 char *buf
= (char *)m_i_streambuf
->GetBufferStart();
114 size_t pos
= m_i_streambuf
->GetIntPosition();
115 if ( pos
== m_length
)
117 m_lasterror
= wxSTREAM_READ_ERROR
;
125 size_t wxMemoryInputStream::OnSysRead(void *buffer
, size_t nbytes
)
127 size_t pos
= m_i_streambuf
->GetIntPosition();
128 if ( pos
== m_length
)
130 m_lasterror
= wxSTREAM_EOF
;
135 m_i_streambuf
->Read(buffer
, nbytes
);
136 m_lasterror
= wxSTREAM_NO_ERROR
;
138 return m_i_streambuf
->GetIntPosition() - pos
;
141 wxFileOffset
wxMemoryInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
143 return m_i_streambuf
->Seek(pos
, mode
);
146 wxFileOffset
wxMemoryInputStream::OnSysTell() const
148 return m_i_streambuf
->Tell();
151 // ----------------------------------------------------------------------------
152 // wxMemoryOutputStream
153 // ----------------------------------------------------------------------------
155 IMPLEMENT_DYNAMIC_CLASS(wxMemoryOutputStream
, wxOutputStream
)
157 wxMemoryOutputStream::wxMemoryOutputStream(void *data
, size_t len
)
159 m_o_streambuf
= new wxStreamBuffer(wxStreamBuffer::write
);
161 m_o_streambuf
->SetBufferIO(data
, len
);
162 m_o_streambuf
->Fixed(false);
163 m_o_streambuf
->Flushable(false);
166 wxMemoryOutputStream::~wxMemoryOutputStream()
168 delete m_o_streambuf
;
171 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer
, size_t nbytes
)
173 size_t oldpos
= m_o_streambuf
->GetIntPosition();
174 m_o_streambuf
->Write(buffer
, nbytes
);
175 size_t newpos
= m_o_streambuf
->GetIntPosition();
177 // FIXME can someone please explain what this does? (VZ)
179 newpos
= m_o_streambuf
->GetBufferSize();
181 return newpos
- oldpos
;
184 wxFileOffset
wxMemoryOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
186 return m_o_streambuf
->Seek(pos
, mode
);
189 wxFileOffset
wxMemoryOutputStream::OnSysTell() const
191 return m_o_streambuf
->Tell();
194 size_t wxMemoryOutputStream::CopyTo(void *buffer
, size_t len
) const
196 wxCHECK_MSG( buffer
, 0, wxT("must have buffer to CopyTo") );
198 if ( len
> GetSize() )
201 memcpy(buffer
, m_o_streambuf
->GetBufferStart(), len
);
206 #endif // wxUSE_STREAMS