]> git.saurik.com Git - wxWidgets.git/blob - src/common/mstream.cpp
more synonyms for UCS-2/4
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "mstream.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_STREAMS
32
33 #include <stdlib.h>
34 #include "wx/stream.h"
35 #include "wx/mstream.h"
36
37 // ============================================================================
38 // implementation
39 // ============================================================================
40
41 // ----------------------------------------------------------------------------
42 // wxMemoryInputStream
43 // ----------------------------------------------------------------------------
44
45 wxMemoryInputStream::wxMemoryInputStream(const void *data, size_t len)
46 {
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
50 m_i_streambuf->Fixed(true);
51
52 m_length = len;
53 }
54
55 wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream)
56 {
57 ssize_t len = (ssize_t)stream.GetLength();
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
71 wxMemoryInputStream::~wxMemoryInputStream()
72 {
73 delete m_i_streambuf;
74 }
75
76 char wxMemoryInputStream::Peek()
77 {
78 char *buf = (char *)m_i_streambuf->GetBufferStart();
79 size_t pos = m_i_streambuf->GetIntPosition();
80 if ( pos == m_length )
81 {
82 m_lasterror = wxSTREAM_READ_ERROR;
83
84 return 0;
85 }
86
87 return buf[pos];
88 }
89
90 bool wxMemoryInputStream::Eof() const
91 {
92 return !m_i_streambuf->GetBytesLeft();
93 }
94
95 size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
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);
106 m_lasterror = wxSTREAM_NO_ERROR;
107
108 return m_i_streambuf->GetIntPosition() - pos;
109 }
110
111 wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
112 {
113 return m_i_streambuf->Seek(pos, mode);
114 }
115
116 wxFileOffset wxMemoryInputStream::OnSysTell() const
117 {
118 return m_i_streambuf->Tell();
119 }
120
121 // ----------------------------------------------------------------------------
122 // wxMemoryOutputStream
123 // ----------------------------------------------------------------------------
124
125 wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len)
126 {
127 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write);
128 if ( data )
129 m_o_streambuf->SetBufferIO(data, len);
130 m_o_streambuf->Fixed(false);
131 m_o_streambuf->Flushable(false);
132 }
133
134 wxMemoryOutputStream::~wxMemoryOutputStream()
135 {
136 delete m_o_streambuf;
137 }
138
139 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
140 {
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;
150 }
151
152 wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
153 {
154 return m_o_streambuf->Seek(pos, mode);
155 }
156
157 wxFileOffset wxMemoryOutputStream::OnSysTell() const
158 {
159 return m_o_streambuf->Tell();
160 }
161
162 size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const
163 {
164 wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") );
165
166 if ( len > GetSize() )
167 len = GetSize();
168
169 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
170
171 return len;
172 }
173
174 #endif // wxUSE_STREAMS