]>
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 | ||
67c8c225 | 45 | wxMemoryInputStream::wxMemoryInputStream(const void *data, size_t len) |
79c3e0e1 | 46 | { |
67c8c225 VZ |
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 | |
4e32eea1 | 50 | m_i_streambuf->Fixed(true); |
c980c992 | 51 | |
67c8c225 | 52 | m_length = len; |
3d4c6a21 GL |
53 | } |
54 | ||
96461cc2 VZ |
55 | wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream) |
56 | { | |
17a1ebd1 VZ |
57 | const wxFileOffset lenFile = stream.GetLength(); |
58 | if ( lenFile == wxInvalidOffset ) | |
59 | { | |
96461cc2 VZ |
60 | m_i_streambuf = NULL; |
61 | m_lasterror = wxSTREAM_EOF; | |
62 | return; | |
63 | } | |
17a1ebd1 VZ |
64 | |
65 | const size_t len = wx_truncate_cast(size_t, lenFile); | |
4a10ea8b | 66 | wxASSERT_MSG( len == lenFile + size_t(0), _T("huge files not supported") ); |
17a1ebd1 | 67 | |
96461cc2 VZ |
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 | ||
76447155 VZ |
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 | ||
2d76b6d8 VZ |
100 | bool wxMemoryInputStream::CanRead() const |
101 | { | |
102 | return m_i_streambuf->GetIntPosition() != m_length; | |
103 | } | |
104 | ||
0cd9bfe8 GL |
105 | wxMemoryInputStream::~wxMemoryInputStream() |
106 | { | |
67c8c225 | 107 | delete m_i_streambuf; |
0cd9bfe8 GL |
108 | } |
109 | ||
32a4b1d5 GL |
110 | char wxMemoryInputStream::Peek() |
111 | { | |
67c8c225 | 112 | char *buf = (char *)m_i_streambuf->GetBufferStart(); |
6c783b03 MB |
113 | size_t pos = m_i_streambuf->GetIntPosition(); |
114 | if ( pos == m_length ) | |
115 | { | |
116 | m_lasterror = wxSTREAM_READ_ERROR; | |
67c8c225 | 117 | |
6c783b03 MB |
118 | return 0; |
119 | } | |
120 | ||
121 | return buf[pos]; | |
c980c992 GL |
122 | } |
123 | ||
124 | size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes) | |
83141d3a VZ |
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); | |
2b5f62a0 | 135 | m_lasterror = wxSTREAM_NO_ERROR; |
83141d3a VZ |
136 | |
137 | return m_i_streambuf->GetIntPosition() - pos; | |
c980c992 GL |
138 | } |
139 | ||
4004775e | 140 | wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
c980c992 | 141 | { |
67c8c225 | 142 | return m_i_streambuf->Seek(pos, mode); |
c980c992 GL |
143 | } |
144 | ||
4004775e | 145 | wxFileOffset wxMemoryInputStream::OnSysTell() const |
c980c992 | 146 | { |
67c8c225 | 147 | return m_i_streambuf->Tell(); |
3d4c6a21 GL |
148 | } |
149 | ||
79c3e0e1 GL |
150 | // ---------------------------------------------------------------------------- |
151 | // wxMemoryOutputStream | |
152 | // ---------------------------------------------------------------------------- | |
153 | ||
67c8c225 | 154 | wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len) |
79c3e0e1 | 155 | { |
67c8c225 VZ |
156 | m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write); |
157 | if ( data ) | |
158 | m_o_streambuf->SetBufferIO(data, len); | |
4e32eea1 WS |
159 | m_o_streambuf->Fixed(false); |
160 | m_o_streambuf->Flushable(false); | |
79c3e0e1 GL |
161 | } |
162 | ||
0cd9bfe8 | 163 | wxMemoryOutputStream::~wxMemoryOutputStream() |
79c3e0e1 | 164 | { |
67c8c225 | 165 | delete m_o_streambuf; |
3d4c6a21 | 166 | } |
ce4169a4 | 167 | |
c980c992 GL |
168 | size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes) |
169 | { | |
67c8c225 VZ |
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; | |
c980c992 GL |
179 | } |
180 | ||
4004775e | 181 | wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
c980c992 | 182 | { |
67c8c225 | 183 | return m_o_streambuf->Seek(pos, mode); |
c980c992 GL |
184 | } |
185 | ||
4004775e | 186 | wxFileOffset wxMemoryOutputStream::OnSysTell() const |
c980c992 | 187 | { |
67c8c225 | 188 | return m_o_streambuf->Tell(); |
c980c992 GL |
189 | } |
190 | ||
67c8c225 | 191 | size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const |
a324a7bc | 192 | { |
67c8c225 | 193 | wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") ); |
a324a7bc | 194 | |
67c8c225 VZ |
195 | if ( len > GetSize() ) |
196 | len = GetSize(); | |
a324a7bc | 197 | |
67c8c225 VZ |
198 | memcpy(buffer, m_o_streambuf->GetBufferStart(), len); |
199 | ||
200 | return len; | |
a324a7bc | 201 | } |
c980c992 | 202 | |
67c8c225 | 203 | #endif // wxUSE_STREAMS |