]>
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);
77 wxMemoryInputStream::InitFromStream(wxInputStream
& stream
, wxFileOffset lenFile
)
79 if ( lenFile
== wxInvalidOffset
)
80 lenFile
= stream
.GetLength();
82 if ( lenFile
== wxInvalidOffset
)
85 m_lasterror
= wxSTREAM_EOF
;
89 const size_t len
= wx_truncate_cast(size_t, lenFile
);
90 wxASSERT_MSG( (wxFileOffset
)len
== lenFile
, _T("huge files not supported") );
92 m_i_streambuf
= new wxStreamBuffer(wxStreamBuffer::read
);
93 m_i_streambuf
->SetBufferIO(len
); // create buffer
94 stream
.Read(m_i_streambuf
->GetBufferStart(), len
);
95 m_i_streambuf
->SetIntPosition(0); // seek to start pos
96 m_i_streambuf
->Fixed(true);
97 m_length
= stream
.LastRead();
100 bool wxMemoryInputStream::CanRead() const
102 return m_i_streambuf
->GetIntPosition() != m_length
;
105 wxMemoryInputStream::~wxMemoryInputStream()
107 delete m_i_streambuf
;
110 char wxMemoryInputStream::Peek()
112 char *buf
= (char *)m_i_streambuf
->GetBufferStart();
113 size_t pos
= m_i_streambuf
->GetIntPosition();
114 if ( pos
== m_length
)
116 m_lasterror
= wxSTREAM_READ_ERROR
;
124 size_t wxMemoryInputStream::OnSysRead(void *buffer
, size_t nbytes
)
126 size_t pos
= m_i_streambuf
->GetIntPosition();
127 if ( pos
== m_length
)
129 m_lasterror
= wxSTREAM_EOF
;
134 m_i_streambuf
->Read(buffer
, nbytes
);
135 m_lasterror
= wxSTREAM_NO_ERROR
;
137 return m_i_streambuf
->GetIntPosition() - pos
;
140 wxFileOffset
wxMemoryInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
142 return m_i_streambuf
->Seek(pos
, mode
);
145 wxFileOffset
wxMemoryInputStream::OnSysTell() const
147 return m_i_streambuf
->Tell();
150 // ----------------------------------------------------------------------------
151 // wxMemoryOutputStream
152 // ----------------------------------------------------------------------------
154 wxMemoryOutputStream::wxMemoryOutputStream(void *data
, size_t len
)
156 m_o_streambuf
= new wxStreamBuffer(wxStreamBuffer::write
);
158 m_o_streambuf
->SetBufferIO(data
, len
);
159 m_o_streambuf
->Fixed(false);
160 m_o_streambuf
->Flushable(false);
163 wxMemoryOutputStream::~wxMemoryOutputStream()
165 delete m_o_streambuf
;
168 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer
, size_t nbytes
)
170 size_t oldpos
= m_o_streambuf
->GetIntPosition();
171 m_o_streambuf
->Write(buffer
, nbytes
);
172 size_t newpos
= m_o_streambuf
->GetIntPosition();
174 // FIXME can someone please explain what this does? (VZ)
176 newpos
= m_o_streambuf
->GetBufferSize();
178 return newpos
- oldpos
;
181 wxFileOffset
wxMemoryOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
183 return m_o_streambuf
->Seek(pos
, mode
);
186 wxFileOffset
wxMemoryOutputStream::OnSysTell() const
188 return m_o_streambuf
->Tell();
191 size_t wxMemoryOutputStream::CopyTo(void *buffer
, size_t len
) const
193 wxCHECK_MSG( buffer
, 0, _T("must have buffer to CopyTo") );
195 if ( len
> GetSize() )
198 memcpy(buffer
, m_o_streambuf
->GetBufferStart(), len
);
203 #endif // wxUSE_STREAMS