]>
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 | ||
0cd9bfe8 GL |
55 | wxMemoryInputStream::~wxMemoryInputStream() |
56 | { | |
67c8c225 | 57 | delete m_i_streambuf; |
0cd9bfe8 GL |
58 | } |
59 | ||
32a4b1d5 GL |
60 | char wxMemoryInputStream::Peek() |
61 | { | |
67c8c225 | 62 | char *buf = (char *)m_i_streambuf->GetBufferStart(); |
6c783b03 MB |
63 | size_t pos = m_i_streambuf->GetIntPosition(); |
64 | if ( pos == m_length ) | |
65 | { | |
66 | m_lasterror = wxSTREAM_READ_ERROR; | |
67c8c225 | 67 | |
6c783b03 MB |
68 | return 0; |
69 | } | |
70 | ||
71 | return buf[pos]; | |
c980c992 GL |
72 | } |
73 | ||
83141d3a VZ |
74 | bool wxMemoryInputStream::Eof() const |
75 | { | |
67c8c225 | 76 | return !m_i_streambuf->GetBytesLeft(); |
83141d3a VZ |
77 | } |
78 | ||
c980c992 | 79 | size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes) |
83141d3a VZ |
80 | { |
81 | size_t pos = m_i_streambuf->GetIntPosition(); | |
82 | if ( pos == m_length ) | |
83 | { | |
84 | m_lasterror = wxSTREAM_EOF; | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
89 | m_i_streambuf->Read(buffer, nbytes); | |
2b5f62a0 | 90 | m_lasterror = wxSTREAM_NO_ERROR; |
83141d3a VZ |
91 | |
92 | return m_i_streambuf->GetIntPosition() - pos; | |
c980c992 GL |
93 | } |
94 | ||
4004775e | 95 | wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
c980c992 | 96 | { |
67c8c225 | 97 | return m_i_streambuf->Seek(pos, mode); |
c980c992 GL |
98 | } |
99 | ||
4004775e | 100 | wxFileOffset wxMemoryInputStream::OnSysTell() const |
c980c992 | 101 | { |
67c8c225 | 102 | return m_i_streambuf->Tell(); |
3d4c6a21 GL |
103 | } |
104 | ||
79c3e0e1 GL |
105 | // ---------------------------------------------------------------------------- |
106 | // wxMemoryOutputStream | |
107 | // ---------------------------------------------------------------------------- | |
108 | ||
67c8c225 | 109 | wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len) |
79c3e0e1 | 110 | { |
67c8c225 VZ |
111 | m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write); |
112 | if ( data ) | |
113 | m_o_streambuf->SetBufferIO(data, len); | |
4e32eea1 WS |
114 | m_o_streambuf->Fixed(false); |
115 | m_o_streambuf->Flushable(false); | |
79c3e0e1 GL |
116 | } |
117 | ||
0cd9bfe8 | 118 | wxMemoryOutputStream::~wxMemoryOutputStream() |
79c3e0e1 | 119 | { |
67c8c225 | 120 | delete m_o_streambuf; |
3d4c6a21 | 121 | } |
ce4169a4 | 122 | |
c980c992 GL |
123 | size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes) |
124 | { | |
67c8c225 VZ |
125 | size_t oldpos = m_o_streambuf->GetIntPosition(); |
126 | m_o_streambuf->Write(buffer, nbytes); | |
127 | size_t newpos = m_o_streambuf->GetIntPosition(); | |
128 | ||
129 | // FIXME can someone please explain what this does? (VZ) | |
130 | if ( !newpos ) | |
131 | newpos = m_o_streambuf->GetBufferSize(); | |
132 | ||
133 | return newpos - oldpos; | |
c980c992 GL |
134 | } |
135 | ||
4004775e | 136 | wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
c980c992 | 137 | { |
67c8c225 | 138 | return m_o_streambuf->Seek(pos, mode); |
c980c992 GL |
139 | } |
140 | ||
4004775e | 141 | wxFileOffset wxMemoryOutputStream::OnSysTell() const |
c980c992 | 142 | { |
67c8c225 | 143 | return m_o_streambuf->Tell(); |
c980c992 GL |
144 | } |
145 | ||
67c8c225 | 146 | size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const |
a324a7bc | 147 | { |
67c8c225 | 148 | wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") ); |
a324a7bc | 149 | |
67c8c225 VZ |
150 | if ( len > GetSize() ) |
151 | len = GetSize(); | |
a324a7bc | 152 | |
67c8c225 VZ |
153 | memcpy(buffer, m_o_streambuf->GetBufferStart(), len); |
154 | ||
155 | return len; | |
a324a7bc | 156 | } |
c980c992 | 157 | |
67c8c225 | 158 | #endif // wxUSE_STREAMS |