]>
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 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
32fc4afb | 21 | #pragma implementation "mstream.h" |
3d4c6a21 GL |
22 | #endif |
23 | ||
79c3e0e1 GL |
24 | // For compilers that support precompilation, includes "wx.h". |
25 | #include "wx/wxprec.h" | |
79c3e0e1 GL |
26 | |
27 | #ifdef __BORLANDC__ | |
ce4169a4 | 28 | #pragma hdrstop |
79c3e0e1 | 29 | #endif |
3d4c6a21 | 30 | |
ce4169a4 RR |
31 | #if wxUSE_STREAMS |
32 | ||
33 | #include <stdlib.h> | |
3096bd2f VZ |
34 | #include "wx/stream.h" |
35 | #include "wx/mstream.h" | |
ce4169a4 | 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 | { | |
d64dc197 | 57 | ssize_t len = (ssize_t)stream.GetLength(); |
96461cc2 VZ |
58 | if (len == wxInvalidOffset) { |
59 | m_i_streambuf = NULL; | |
60 | m_lasterror = wxSTREAM_EOF; | |
61 | return; | |
62 | } | |
63 | m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read); | |
64 | m_i_streambuf->SetBufferIO(len); // create buffer | |
65 | stream.CopyTo(m_i_streambuf->GetBufferStart(), len); | |
66 | m_i_streambuf->SetIntPosition(0); // seek to start pos | |
67 | m_i_streambuf->Fixed(true); | |
68 | m_length = len; | |
69 | } | |
70 | ||
0cd9bfe8 GL |
71 | wxMemoryInputStream::~wxMemoryInputStream() |
72 | { | |
67c8c225 | 73 | delete m_i_streambuf; |
0cd9bfe8 GL |
74 | } |
75 | ||
32a4b1d5 GL |
76 | char wxMemoryInputStream::Peek() |
77 | { | |
67c8c225 | 78 | char *buf = (char *)m_i_streambuf->GetBufferStart(); |
6c783b03 MB |
79 | size_t pos = m_i_streambuf->GetIntPosition(); |
80 | if ( pos == m_length ) | |
81 | { | |
82 | m_lasterror = wxSTREAM_READ_ERROR; | |
67c8c225 | 83 | |
6c783b03 MB |
84 | return 0; |
85 | } | |
86 | ||
87 | return buf[pos]; | |
c980c992 GL |
88 | } |
89 | ||
83141d3a VZ |
90 | bool wxMemoryInputStream::Eof() const |
91 | { | |
67c8c225 | 92 | return !m_i_streambuf->GetBytesLeft(); |
83141d3a VZ |
93 | } |
94 | ||
c980c992 | 95 | size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes) |
83141d3a VZ |
96 | { |
97 | size_t pos = m_i_streambuf->GetIntPosition(); | |
98 | if ( pos == m_length ) | |
99 | { | |
100 | m_lasterror = wxSTREAM_EOF; | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
105 | m_i_streambuf->Read(buffer, nbytes); | |
2b5f62a0 | 106 | m_lasterror = wxSTREAM_NO_ERROR; |
83141d3a VZ |
107 | |
108 | return m_i_streambuf->GetIntPosition() - pos; | |
c980c992 GL |
109 | } |
110 | ||
4004775e | 111 | wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
c980c992 | 112 | { |
67c8c225 | 113 | return m_i_streambuf->Seek(pos, mode); |
c980c992 GL |
114 | } |
115 | ||
4004775e | 116 | wxFileOffset wxMemoryInputStream::OnSysTell() const |
c980c992 | 117 | { |
67c8c225 | 118 | return m_i_streambuf->Tell(); |
3d4c6a21 GL |
119 | } |
120 | ||
79c3e0e1 GL |
121 | // ---------------------------------------------------------------------------- |
122 | // wxMemoryOutputStream | |
123 | // ---------------------------------------------------------------------------- | |
124 | ||
67c8c225 | 125 | wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len) |
79c3e0e1 | 126 | { |
67c8c225 VZ |
127 | m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write); |
128 | if ( data ) | |
129 | m_o_streambuf->SetBufferIO(data, len); | |
4e32eea1 WS |
130 | m_o_streambuf->Fixed(false); |
131 | m_o_streambuf->Flushable(false); | |
79c3e0e1 GL |
132 | } |
133 | ||
0cd9bfe8 | 134 | wxMemoryOutputStream::~wxMemoryOutputStream() |
79c3e0e1 | 135 | { |
67c8c225 | 136 | delete m_o_streambuf; |
3d4c6a21 | 137 | } |
ce4169a4 | 138 | |
c980c992 GL |
139 | size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes) |
140 | { | |
67c8c225 VZ |
141 | size_t oldpos = m_o_streambuf->GetIntPosition(); |
142 | m_o_streambuf->Write(buffer, nbytes); | |
143 | size_t newpos = m_o_streambuf->GetIntPosition(); | |
144 | ||
145 | // FIXME can someone please explain what this does? (VZ) | |
146 | if ( !newpos ) | |
147 | newpos = m_o_streambuf->GetBufferSize(); | |
148 | ||
149 | return newpos - oldpos; | |
c980c992 GL |
150 | } |
151 | ||
4004775e | 152 | wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
c980c992 | 153 | { |
67c8c225 | 154 | return m_o_streambuf->Seek(pos, mode); |
c980c992 GL |
155 | } |
156 | ||
4004775e | 157 | wxFileOffset wxMemoryOutputStream::OnSysTell() const |
c980c992 | 158 | { |
67c8c225 | 159 | return m_o_streambuf->Tell(); |
c980c992 GL |
160 | } |
161 | ||
67c8c225 | 162 | size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const |
a324a7bc | 163 | { |
67c8c225 | 164 | wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") ); |
a324a7bc | 165 | |
67c8c225 VZ |
166 | if ( len > GetSize() ) |
167 | len = GetSize(); | |
a324a7bc | 168 | |
67c8c225 VZ |
169 | memcpy(buffer, m_o_streambuf->GetBufferStart(), len); |
170 | ||
171 | return len; | |
a324a7bc | 172 | } |
c980c992 | 173 | |
67c8c225 | 174 | #endif // wxUSE_STREAMS |