first round of Intel compiler warning fixes: down from a few thousands just to slight...
[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 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_STREAMS
28
29 #include <stdlib.h>
30 #include "wx/stream.h"
31 #include "wx/mstream.h"
32
33 // ============================================================================
34 // implementation
35 // ============================================================================
36
37 // ----------------------------------------------------------------------------
38 // wxMemoryInputStream
39 // ----------------------------------------------------------------------------
40
41 wxMemoryInputStream::wxMemoryInputStream(const void *data, size_t len)
42 {
43 m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read);
44 m_i_streambuf->SetBufferIO((void *)data, len); // const_cast
45 m_i_streambuf->SetIntPosition(0); // seek to start pos
46 m_i_streambuf->Fixed(true);
47
48 m_length = len;
49 }
50
51 wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream)
52 {
53 const wxFileOffset lenFile = stream.GetLength();
54 if ( lenFile == wxInvalidOffset )
55 {
56 m_i_streambuf = NULL;
57 m_lasterror = wxSTREAM_EOF;
58 return;
59 }
60
61 const size_t len = wx_truncate_cast(size_t, lenFile);
62 wxASSERT_MSG( len == lenFile, _T("huge files not supported") );
63
64 m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read);
65 m_i_streambuf->SetBufferIO(len); // create buffer
66 stream.CopyTo(m_i_streambuf->GetBufferStart(), len);
67 m_i_streambuf->SetIntPosition(0); // seek to start pos
68 m_i_streambuf->Fixed(true);
69 m_length = len;
70 }
71
72 wxMemoryInputStream::~wxMemoryInputStream()
73 {
74 delete m_i_streambuf;
75 }
76
77 char wxMemoryInputStream::Peek()
78 {
79 char *buf = (char *)m_i_streambuf->GetBufferStart();
80 size_t pos = m_i_streambuf->GetIntPosition();
81 if ( pos == m_length )
82 {
83 m_lasterror = wxSTREAM_READ_ERROR;
84
85 return 0;
86 }
87
88 return buf[pos];
89 }
90
91 bool wxMemoryInputStream::Eof() const
92 {
93 return !m_i_streambuf->GetBytesLeft();
94 }
95
96 size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
97 {
98 size_t pos = m_i_streambuf->GetIntPosition();
99 if ( pos == m_length )
100 {
101 m_lasterror = wxSTREAM_EOF;
102
103 return 0;
104 }
105
106 m_i_streambuf->Read(buffer, nbytes);
107 m_lasterror = wxSTREAM_NO_ERROR;
108
109 return m_i_streambuf->GetIntPosition() - pos;
110 }
111
112 wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
113 {
114 return m_i_streambuf->Seek(pos, mode);
115 }
116
117 wxFileOffset wxMemoryInputStream::OnSysTell() const
118 {
119 return m_i_streambuf->Tell();
120 }
121
122 // ----------------------------------------------------------------------------
123 // wxMemoryOutputStream
124 // ----------------------------------------------------------------------------
125
126 wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len)
127 {
128 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write);
129 if ( data )
130 m_o_streambuf->SetBufferIO(data, len);
131 m_o_streambuf->Fixed(false);
132 m_o_streambuf->Flushable(false);
133 }
134
135 wxMemoryOutputStream::~wxMemoryOutputStream()
136 {
137 delete m_o_streambuf;
138 }
139
140 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
141 {
142 size_t oldpos = m_o_streambuf->GetIntPosition();
143 m_o_streambuf->Write(buffer, nbytes);
144 size_t newpos = m_o_streambuf->GetIntPosition();
145
146 // FIXME can someone please explain what this does? (VZ)
147 if ( !newpos )
148 newpos = m_o_streambuf->GetBufferSize();
149
150 return newpos - oldpos;
151 }
152
153 wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
154 {
155 return m_o_streambuf->Seek(pos, mode);
156 }
157
158 wxFileOffset wxMemoryOutputStream::OnSysTell() const
159 {
160 return m_o_streambuf->Tell();
161 }
162
163 size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const
164 {
165 wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") );
166
167 if ( len > GetSize() )
168 len = GetSize();
169
170 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
171
172 return len;
173 }
174
175 #endif // wxUSE_STREAMS