added wxMemoryInputStream(wxInputStream&, size_t) ctor (modified patch 1680108)
[wxWidgets.git] / 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
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_STREAMS
28
29 #include "wx/mstream.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/stream.h"
33 #endif //WX_PRECOMP
34
35 #include <stdlib.h>
36
37 // ============================================================================
38 // implementation
39 // ============================================================================
40
41 // ----------------------------------------------------------------------------
42 // wxMemoryInputStream
43 // ----------------------------------------------------------------------------
44
45 wxMemoryInputStream::wxMemoryInputStream(const void *data, size_t len)
46 {
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);
51
52 m_length = len;
53 }
54
55 wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream)
56 {
57 const wxFileOffset lenFile = stream.GetLength();
58 if ( lenFile == wxInvalidOffset )
59 {
60 m_i_streambuf = NULL;
61 m_lasterror = wxSTREAM_EOF;
62 return;
63 }
64
65 const size_t len = wx_truncate_cast(size_t, lenFile);
66 wxASSERT_MSG( len == lenFile + size_t(0), _T("huge files not supported") );
67
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);
73 m_length = len;
74 }
75
76 void
77 wxMemoryInputStream::InitFromStream(wxInputStream& stream, wxFileOffset lenFile)
78 {
79 if ( lenFile == wxInvalidOffset )
80 lenFile = stream.GetLength();
81
82 if ( lenFile == wxInvalidOffset )
83 {
84 m_i_streambuf = NULL;
85 m_lasterror = wxSTREAM_EOF;
86 return;
87 }
88
89 const size_t len = wx_truncate_cast(size_t, lenFile);
90 wxASSERT_MSG( (wxFileOffset)len == lenFile, _T("huge files not supported") );
91
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();
98 }
99
100 wxMemoryInputStream::~wxMemoryInputStream()
101 {
102 delete m_i_streambuf;
103 }
104
105 char wxMemoryInputStream::Peek()
106 {
107 char *buf = (char *)m_i_streambuf->GetBufferStart();
108 size_t pos = m_i_streambuf->GetIntPosition();
109 if ( pos == m_length )
110 {
111 m_lasterror = wxSTREAM_READ_ERROR;
112
113 return 0;
114 }
115
116 return buf[pos];
117 }
118
119 size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
120 {
121 size_t pos = m_i_streambuf->GetIntPosition();
122 if ( pos == m_length )
123 {
124 m_lasterror = wxSTREAM_EOF;
125
126 return 0;
127 }
128
129 m_i_streambuf->Read(buffer, nbytes);
130 m_lasterror = wxSTREAM_NO_ERROR;
131
132 return m_i_streambuf->GetIntPosition() - pos;
133 }
134
135 wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
136 {
137 return m_i_streambuf->Seek(pos, mode);
138 }
139
140 wxFileOffset wxMemoryInputStream::OnSysTell() const
141 {
142 return m_i_streambuf->Tell();
143 }
144
145 // ----------------------------------------------------------------------------
146 // wxMemoryOutputStream
147 // ----------------------------------------------------------------------------
148
149 wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len)
150 {
151 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write);
152 if ( data )
153 m_o_streambuf->SetBufferIO(data, len);
154 m_o_streambuf->Fixed(false);
155 m_o_streambuf->Flushable(false);
156 }
157
158 wxMemoryOutputStream::~wxMemoryOutputStream()
159 {
160 delete m_o_streambuf;
161 }
162
163 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
164 {
165 size_t oldpos = m_o_streambuf->GetIntPosition();
166 m_o_streambuf->Write(buffer, nbytes);
167 size_t newpos = m_o_streambuf->GetIntPosition();
168
169 // FIXME can someone please explain what this does? (VZ)
170 if ( !newpos )
171 newpos = m_o_streambuf->GetBufferSize();
172
173 return newpos - oldpos;
174 }
175
176 wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
177 {
178 return m_o_streambuf->Seek(pos, mode);
179 }
180
181 wxFileOffset wxMemoryOutputStream::OnSysTell() const
182 {
183 return m_o_streambuf->Tell();
184 }
185
186 size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const
187 {
188 wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") );
189
190 if ( len > GetSize() )
191 len = GetSize();
192
193 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
194
195 return len;
196 }
197
198 #endif // wxUSE_STREAMS