]>
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 IMPLEMENT_ABSTRACT_CLASS(wxMemoryInputStream
, wxInputStream
)
47 wxMemoryInputStream::wxMemoryInputStream(const void *data
, size_t len
)
49 m_i_streambuf
= new wxStreamBuffer(wxStreamBuffer::read
);
50 m_i_streambuf
->SetBufferIO((void *)data
, len
); // const_cast
51 m_i_streambuf
->SetIntPosition(0); // seek to start pos
52 m_i_streambuf
->Fixed(true);
57 wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream
& stream
)
59 const wxFileOffset lenFile
= stream
.GetLength();
60 if ( lenFile
== wxInvalidOffset
)
63 m_lasterror
= wxSTREAM_EOF
;
67 const size_t len
= wx_truncate_cast(size_t, lenFile
);
68 wxASSERT_MSG( len
== lenFile
+ size_t(0), _T("huge files not supported") );
70 m_i_streambuf
= new wxStreamBuffer(wxStreamBuffer::read
);
71 m_i_streambuf
->SetBufferIO(len
); // create buffer
72 stream
.CopyTo(m_i_streambuf
->GetBufferStart(), len
);
73 m_i_streambuf
->SetIntPosition(0); // seek to start pos
74 m_i_streambuf
->Fixed(true);
79 wxMemoryInputStream::InitFromStream(wxInputStream
& stream
, wxFileOffset lenFile
)
81 if ( lenFile
== wxInvalidOffset
)
82 lenFile
= stream
.GetLength();
84 if ( lenFile
== wxInvalidOffset
)
87 m_lasterror
= wxSTREAM_EOF
;
91 const size_t len
= wx_truncate_cast(size_t, lenFile
);
92 wxASSERT_MSG( (wxFileOffset
)len
== lenFile
, _T("huge files not supported") );
94 m_i_streambuf
= new wxStreamBuffer(wxStreamBuffer::read
);
95 m_i_streambuf
->SetBufferIO(len
); // create buffer
96 stream
.Read(m_i_streambuf
->GetBufferStart(), len
);
97 m_i_streambuf
->SetIntPosition(0); // seek to start pos
98 m_i_streambuf
->Fixed(true);
99 m_length
= stream
.LastRead();
102 bool wxMemoryInputStream::CanRead() const
104 return m_i_streambuf
->GetIntPosition() != m_length
;
107 wxMemoryInputStream::~wxMemoryInputStream()
109 delete m_i_streambuf
;
112 char wxMemoryInputStream::Peek()
114 char *buf
= (char *)m_i_streambuf
->GetBufferStart();
115 size_t pos
= m_i_streambuf
->GetIntPosition();
116 if ( pos
== m_length
)
118 m_lasterror
= wxSTREAM_READ_ERROR
;
126 size_t wxMemoryInputStream::OnSysRead(void *buffer
, size_t nbytes
)
128 size_t pos
= m_i_streambuf
->GetIntPosition();
129 if ( pos
== m_length
)
131 m_lasterror
= wxSTREAM_EOF
;
136 m_i_streambuf
->Read(buffer
, nbytes
);
137 m_lasterror
= wxSTREAM_NO_ERROR
;
139 return m_i_streambuf
->GetIntPosition() - pos
;
142 wxFileOffset
wxMemoryInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
144 return m_i_streambuf
->Seek(pos
, mode
);
147 wxFileOffset
wxMemoryInputStream::OnSysTell() const
149 return m_i_streambuf
->Tell();
152 // ----------------------------------------------------------------------------
153 // wxMemoryOutputStream
154 // ----------------------------------------------------------------------------
156 IMPLEMENT_DYNAMIC_CLASS(wxMemoryOutputStream
, wxOutputStream
)
158 wxMemoryOutputStream::wxMemoryOutputStream(void *data
, size_t len
)
160 m_o_streambuf
= new wxStreamBuffer(wxStreamBuffer::write
);
162 m_o_streambuf
->SetBufferIO(data
, len
);
163 m_o_streambuf
->Fixed(false);
164 m_o_streambuf
->Flushable(false);
167 wxMemoryOutputStream::~wxMemoryOutputStream()
169 delete m_o_streambuf
;
172 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer
, size_t nbytes
)
174 size_t oldpos
= m_o_streambuf
->GetIntPosition();
175 m_o_streambuf
->Write(buffer
, nbytes
);
176 size_t newpos
= m_o_streambuf
->GetIntPosition();
178 // FIXME can someone please explain what this does? (VZ)
180 newpos
= m_o_streambuf
->GetBufferSize();
182 return newpos
- oldpos
;
185 wxFileOffset
wxMemoryOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
187 return m_o_streambuf
->Seek(pos
, mode
);
190 wxFileOffset
wxMemoryOutputStream::OnSysTell() const
192 return m_o_streambuf
->Tell();
195 size_t wxMemoryOutputStream::CopyTo(void *buffer
, size_t len
) const
197 wxCHECK_MSG( buffer
, 0, _T("must have buffer to CopyTo") );
199 if ( len
> GetSize() )
202 memcpy(buffer
, m_o_streambuf
->GetBufferStart(), len
);
207 #endif // wxUSE_STREAMS