]> git.saurik.com Git - wxWidgets.git/blob - src/common/mstream.cpp
Increase the number of visible OS X combo items.
[wxWidgets.git] / src / common / mstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/mstream.cpp
3 // Purpose: "Memory stream" classes
4 // Author: Guilhem Lavaux
5 // Modified by: VZ (23.11.00): general code review
6 // Created: 04/01/98
7 // Copyright: (c) Guilhem Lavaux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_STREAMS
27
28 #include "wx/mstream.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/stream.h"
32 #endif //WX_PRECOMP
33
34 #include <stdlib.h>
35
36 // ============================================================================
37 // implementation
38 // ============================================================================
39
40 // ----------------------------------------------------------------------------
41 // wxMemoryInputStream
42 // ----------------------------------------------------------------------------
43
44 IMPLEMENT_ABSTRACT_CLASS(wxMemoryInputStream, wxInputStream)
45
46 wxMemoryInputStream::wxMemoryInputStream(const void *data, size_t len)
47 {
48 m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read);
49 m_i_streambuf->SetBufferIO(const_cast<void *>(data), len);
50 m_i_streambuf->SetIntPosition(0); // seek to start pos
51 m_i_streambuf->Fixed(true);
52
53 m_length = len;
54 }
55
56 wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream)
57 {
58 const wxFileOffset lenFile = stream.GetLength();
59 if ( lenFile == wxInvalidOffset )
60 {
61 m_i_streambuf = NULL;
62 m_lasterror = wxSTREAM_EOF;
63 return;
64 }
65
66 const size_t len = wx_truncate_cast(size_t, lenFile);
67 wxASSERT_MSG( len == lenFile + size_t(0), wxT("huge files not supported") );
68
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
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);
91 wxASSERT_MSG( (wxFileOffset)len == lenFile, wxT("huge files not supported") );
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
101 bool wxMemoryInputStream::CanRead() const
102 {
103 return m_i_streambuf->GetIntPosition() != m_length;
104 }
105
106 wxMemoryInputStream::~wxMemoryInputStream()
107 {
108 delete m_i_streambuf;
109 }
110
111 char wxMemoryInputStream::Peek()
112 {
113 char *buf = (char *)m_i_streambuf->GetBufferStart();
114 size_t pos = m_i_streambuf->GetIntPosition();
115 if ( pos == m_length )
116 {
117 m_lasterror = wxSTREAM_READ_ERROR;
118
119 return 0;
120 }
121
122 return buf[pos];
123 }
124
125 size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
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);
136 m_lasterror = wxSTREAM_NO_ERROR;
137
138 return m_i_streambuf->GetIntPosition() - pos;
139 }
140
141 wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
142 {
143 return m_i_streambuf->Seek(pos, mode);
144 }
145
146 wxFileOffset wxMemoryInputStream::OnSysTell() const
147 {
148 return m_i_streambuf->Tell();
149 }
150
151 // ----------------------------------------------------------------------------
152 // wxMemoryOutputStream
153 // ----------------------------------------------------------------------------
154
155 IMPLEMENT_DYNAMIC_CLASS(wxMemoryOutputStream, wxOutputStream)
156
157 wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len)
158 {
159 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write);
160 if ( data )
161 m_o_streambuf->SetBufferIO(data, len);
162 m_o_streambuf->Fixed(false);
163 m_o_streambuf->Flushable(false);
164 }
165
166 wxMemoryOutputStream::~wxMemoryOutputStream()
167 {
168 delete m_o_streambuf;
169 }
170
171 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
172 {
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;
182 }
183
184 wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
185 {
186 return m_o_streambuf->Seek(pos, mode);
187 }
188
189 wxFileOffset wxMemoryOutputStream::OnSysTell() const
190 {
191 return m_o_streambuf->Tell();
192 }
193
194 size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const
195 {
196 wxCHECK_MSG( buffer, 0, wxT("must have buffer to CopyTo") );
197
198 if ( len > GetSize() )
199 len = GetSize();
200
201 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
202
203 return len;
204 }
205
206 #endif // wxUSE_STREAMS