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