]>
Commit | Line | Data |
---|---|---|
3d4c6a21 | 1 | ///////////////////////////////////////////////////////////////////////////// |
67c8c225 | 2 | // Name: src/common/mstream.cpp |
3d4c6a21 GL |
3 | // Purpose: "Memory stream" classes |
4 | // Author: Guilhem Lavaux | |
67c8c225 | 5 | // Modified by: VZ (23.11.00): general code review |
3d4c6a21 GL |
6 | // Created: 04/01/98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
65571936 | 9 | // Licence: wxWindows licence |
3d4c6a21 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
67c8c225 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
79c3e0e1 GL |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
79c3e0e1 GL |
22 | |
23 | #ifdef __BORLANDC__ | |
530ecef0 | 24 | #pragma hdrstop |
79c3e0e1 | 25 | #endif |
3d4c6a21 | 26 | |
ce4169a4 RR |
27 | #if wxUSE_STREAMS |
28 | ||
3096bd2f | 29 | #include "wx/mstream.h" |
ce4169a4 | 30 | |
530ecef0 WS |
31 | #ifndef WX_PRECOMP |
32 | #include "wx/stream.h" | |
33 | #endif //WX_PRECOMP | |
34 | ||
35 | #include <stdlib.h> | |
36 | ||
67c8c225 VZ |
37 | // ============================================================================ |
38 | // implementation | |
39 | // ============================================================================ | |
40 | ||
79c3e0e1 GL |
41 | // ---------------------------------------------------------------------------- |
42 | // wxMemoryInputStream | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
6285e36b VZ |
45 | IMPLEMENT_ABSTRACT_CLASS(wxMemoryInputStream, wxInputStream) |
46 | ||
67c8c225 | 47 | wxMemoryInputStream::wxMemoryInputStream(const void *data, size_t len) |
79c3e0e1 | 48 | { |
67c8c225 VZ |
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 | |
4e32eea1 | 52 | m_i_streambuf->Fixed(true); |
c980c992 | 53 | |
67c8c225 | 54 | m_length = len; |
3d4c6a21 GL |
55 | } |
56 | ||
96461cc2 VZ |
57 | wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream) |
58 | { | |
17a1ebd1 VZ |
59 | const wxFileOffset lenFile = stream.GetLength(); |
60 | if ( lenFile == wxInvalidOffset ) | |
61 | { | |
96461cc2 VZ |
62 | m_i_streambuf = NULL; |
63 | m_lasterror = wxSTREAM_EOF; | |
64 | return; | |
65 | } | |
17a1ebd1 VZ |
66 | |
67 | const size_t len = wx_truncate_cast(size_t, lenFile); | |
4a10ea8b | 68 | wxASSERT_MSG( len == lenFile + size_t(0), _T("huge files not supported") ); |
17a1ebd1 | 69 | |
96461cc2 VZ |
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); | |
75 | m_length = len; | |
76 | } | |
77 | ||
76447155 VZ |
78 | void |
79 | wxMemoryInputStream::InitFromStream(wxInputStream& stream, wxFileOffset lenFile) | |
80 | { | |
81 | if ( lenFile == wxInvalidOffset ) | |
82 | lenFile = stream.GetLength(); | |
83 | ||
84 | if ( lenFile == wxInvalidOffset ) | |
85 | { | |
86 | m_i_streambuf = NULL; | |
87 | m_lasterror = wxSTREAM_EOF; | |
88 | return; | |
89 | } | |
90 | ||
91 | const size_t len = wx_truncate_cast(size_t, lenFile); | |
92 | wxASSERT_MSG( (wxFileOffset)len == lenFile, _T("huge files not supported") ); | |
93 | ||
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(); | |
100 | } | |
101 | ||
2d76b6d8 VZ |
102 | bool wxMemoryInputStream::CanRead() const |
103 | { | |
104 | return m_i_streambuf->GetIntPosition() != m_length; | |
105 | } | |
106 | ||
0cd9bfe8 GL |
107 | wxMemoryInputStream::~wxMemoryInputStream() |
108 | { | |
67c8c225 | 109 | delete m_i_streambuf; |
0cd9bfe8 GL |
110 | } |
111 | ||
32a4b1d5 GL |
112 | char wxMemoryInputStream::Peek() |
113 | { | |
67c8c225 | 114 | char *buf = (char *)m_i_streambuf->GetBufferStart(); |
6c783b03 MB |
115 | size_t pos = m_i_streambuf->GetIntPosition(); |
116 | if ( pos == m_length ) | |
117 | { | |
118 | m_lasterror = wxSTREAM_READ_ERROR; | |
67c8c225 | 119 | |
6c783b03 MB |
120 | return 0; |
121 | } | |
122 | ||
123 | return buf[pos]; | |
c980c992 GL |
124 | } |
125 | ||
126 | size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes) | |
83141d3a VZ |
127 | { |
128 | size_t pos = m_i_streambuf->GetIntPosition(); | |
129 | if ( pos == m_length ) | |
130 | { | |
131 | m_lasterror = wxSTREAM_EOF; | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
136 | m_i_streambuf->Read(buffer, nbytes); | |
2b5f62a0 | 137 | m_lasterror = wxSTREAM_NO_ERROR; |
83141d3a VZ |
138 | |
139 | return m_i_streambuf->GetIntPosition() - pos; | |
c980c992 GL |
140 | } |
141 | ||
4004775e | 142 | wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
c980c992 | 143 | { |
67c8c225 | 144 | return m_i_streambuf->Seek(pos, mode); |
c980c992 GL |
145 | } |
146 | ||
4004775e | 147 | wxFileOffset wxMemoryInputStream::OnSysTell() const |
c980c992 | 148 | { |
67c8c225 | 149 | return m_i_streambuf->Tell(); |
3d4c6a21 GL |
150 | } |
151 | ||
79c3e0e1 GL |
152 | // ---------------------------------------------------------------------------- |
153 | // wxMemoryOutputStream | |
154 | // ---------------------------------------------------------------------------- | |
155 | ||
6285e36b VZ |
156 | IMPLEMENT_DYNAMIC_CLASS(wxMemoryOutputStream, wxOutputStream) |
157 | ||
67c8c225 | 158 | wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len) |
79c3e0e1 | 159 | { |
67c8c225 VZ |
160 | m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write); |
161 | if ( data ) | |
162 | m_o_streambuf->SetBufferIO(data, len); | |
4e32eea1 WS |
163 | m_o_streambuf->Fixed(false); |
164 | m_o_streambuf->Flushable(false); | |
79c3e0e1 GL |
165 | } |
166 | ||
0cd9bfe8 | 167 | wxMemoryOutputStream::~wxMemoryOutputStream() |
79c3e0e1 | 168 | { |
67c8c225 | 169 | delete m_o_streambuf; |
3d4c6a21 | 170 | } |
ce4169a4 | 171 | |
c980c992 GL |
172 | size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes) |
173 | { | |
67c8c225 VZ |
174 | size_t oldpos = m_o_streambuf->GetIntPosition(); |
175 | m_o_streambuf->Write(buffer, nbytes); | |
176 | size_t newpos = m_o_streambuf->GetIntPosition(); | |
177 | ||
178 | // FIXME can someone please explain what this does? (VZ) | |
179 | if ( !newpos ) | |
180 | newpos = m_o_streambuf->GetBufferSize(); | |
181 | ||
182 | return newpos - oldpos; | |
c980c992 GL |
183 | } |
184 | ||
4004775e | 185 | wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
c980c992 | 186 | { |
67c8c225 | 187 | return m_o_streambuf->Seek(pos, mode); |
c980c992 GL |
188 | } |
189 | ||
4004775e | 190 | wxFileOffset wxMemoryOutputStream::OnSysTell() const |
c980c992 | 191 | { |
67c8c225 | 192 | return m_o_streambuf->Tell(); |
c980c992 GL |
193 | } |
194 | ||
67c8c225 | 195 | size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const |
a324a7bc | 196 | { |
67c8c225 | 197 | wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") ); |
a324a7bc | 198 | |
67c8c225 VZ |
199 | if ( len > GetSize() ) |
200 | len = GetSize(); | |
a324a7bc | 201 | |
67c8c225 VZ |
202 | memcpy(buffer, m_o_streambuf->GetBufferStart(), len); |
203 | ||
204 | return len; | |
a324a7bc | 205 | } |
c980c992 | 206 | |
67c8c225 | 207 | #endif // wxUSE_STREAMS |