]>
Commit | Line | Data |
---|---|---|
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 | bool wxMemoryInputStream::CanRead() const | |
101 | { | |
102 | return m_i_streambuf->GetIntPosition() != m_length; | |
103 | } | |
104 | ||
105 | wxMemoryInputStream::~wxMemoryInputStream() | |
106 | { | |
107 | delete m_i_streambuf; | |
108 | } | |
109 | ||
110 | char wxMemoryInputStream::Peek() | |
111 | { | |
112 | char *buf = (char *)m_i_streambuf->GetBufferStart(); | |
113 | size_t pos = m_i_streambuf->GetIntPosition(); | |
114 | if ( pos == m_length ) | |
115 | { | |
116 | m_lasterror = wxSTREAM_READ_ERROR; | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
121 | return buf[pos]; | |
122 | } | |
123 | ||
124 | size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes) | |
125 | { | |
126 | size_t pos = m_i_streambuf->GetIntPosition(); | |
127 | if ( pos == m_length ) | |
128 | { | |
129 | m_lasterror = wxSTREAM_EOF; | |
130 | ||
131 | return 0; | |
132 | } | |
133 | ||
134 | m_i_streambuf->Read(buffer, nbytes); | |
135 | m_lasterror = wxSTREAM_NO_ERROR; | |
136 | ||
137 | return m_i_streambuf->GetIntPosition() - pos; | |
138 | } | |
139 | ||
140 | wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) | |
141 | { | |
142 | return m_i_streambuf->Seek(pos, mode); | |
143 | } | |
144 | ||
145 | wxFileOffset wxMemoryInputStream::OnSysTell() const | |
146 | { | |
147 | return m_i_streambuf->Tell(); | |
148 | } | |
149 | ||
150 | // ---------------------------------------------------------------------------- | |
151 | // wxMemoryOutputStream | |
152 | // ---------------------------------------------------------------------------- | |
153 | ||
154 | wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len) | |
155 | { | |
156 | m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write); | |
157 | if ( data ) | |
158 | m_o_streambuf->SetBufferIO(data, len); | |
159 | m_o_streambuf->Fixed(false); | |
160 | m_o_streambuf->Flushable(false); | |
161 | } | |
162 | ||
163 | wxMemoryOutputStream::~wxMemoryOutputStream() | |
164 | { | |
165 | delete m_o_streambuf; | |
166 | } | |
167 | ||
168 | size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes) | |
169 | { | |
170 | size_t oldpos = m_o_streambuf->GetIntPosition(); | |
171 | m_o_streambuf->Write(buffer, nbytes); | |
172 | size_t newpos = m_o_streambuf->GetIntPosition(); | |
173 | ||
174 | // FIXME can someone please explain what this does? (VZ) | |
175 | if ( !newpos ) | |
176 | newpos = m_o_streambuf->GetBufferSize(); | |
177 | ||
178 | return newpos - oldpos; | |
179 | } | |
180 | ||
181 | wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) | |
182 | { | |
183 | return m_o_streambuf->Seek(pos, mode); | |
184 | } | |
185 | ||
186 | wxFileOffset wxMemoryOutputStream::OnSysTell() const | |
187 | { | |
188 | return m_o_streambuf->Tell(); | |
189 | } | |
190 | ||
191 | size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const | |
192 | { | |
193 | wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") ); | |
194 | ||
195 | if ( len > GetSize() ) | |
196 | len = GetSize(); | |
197 | ||
198 | memcpy(buffer, m_o_streambuf->GetBufferStart(), len); | |
199 | ||
200 | return len; | |
201 | } | |
202 | ||
203 | #endif // wxUSE_STREAMS |