]> git.saurik.com Git - wxWidgets.git/blob - src/common/mstream.cpp
set error to GSOCK_TIMEOUT if the socket timed out (modified and extended patch 1303554)
[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 ssize_t len = (ssize_t)stream.GetLength();
54 if (len == wxInvalidOffset) {
55 m_i_streambuf = NULL;
56 m_lasterror = wxSTREAM_EOF;
57 return;
58 }
59 m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read);
60 m_i_streambuf->SetBufferIO(len); // create buffer
61 stream.CopyTo(m_i_streambuf->GetBufferStart(), len);
62 m_i_streambuf->SetIntPosition(0); // seek to start pos
63 m_i_streambuf->Fixed(true);
64 m_length = len;
65 }
66
67 wxMemoryInputStream::~wxMemoryInputStream()
68 {
69 delete m_i_streambuf;
70 }
71
72 char wxMemoryInputStream::Peek()
73 {
74 char *buf = (char *)m_i_streambuf->GetBufferStart();
75 size_t pos = m_i_streambuf->GetIntPosition();
76 if ( pos == m_length )
77 {
78 m_lasterror = wxSTREAM_READ_ERROR;
79
80 return 0;
81 }
82
83 return buf[pos];
84 }
85
86 bool wxMemoryInputStream::Eof() const
87 {
88 return !m_i_streambuf->GetBytesLeft();
89 }
90
91 size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
92 {
93 size_t pos = m_i_streambuf->GetIntPosition();
94 if ( pos == m_length )
95 {
96 m_lasterror = wxSTREAM_EOF;
97
98 return 0;
99 }
100
101 m_i_streambuf->Read(buffer, nbytes);
102 m_lasterror = wxSTREAM_NO_ERROR;
103
104 return m_i_streambuf->GetIntPosition() - pos;
105 }
106
107 wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
108 {
109 return m_i_streambuf->Seek(pos, mode);
110 }
111
112 wxFileOffset wxMemoryInputStream::OnSysTell() const
113 {
114 return m_i_streambuf->Tell();
115 }
116
117 // ----------------------------------------------------------------------------
118 // wxMemoryOutputStream
119 // ----------------------------------------------------------------------------
120
121 wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len)
122 {
123 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write);
124 if ( data )
125 m_o_streambuf->SetBufferIO(data, len);
126 m_o_streambuf->Fixed(false);
127 m_o_streambuf->Flushable(false);
128 }
129
130 wxMemoryOutputStream::~wxMemoryOutputStream()
131 {
132 delete m_o_streambuf;
133 }
134
135 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
136 {
137 size_t oldpos = m_o_streambuf->GetIntPosition();
138 m_o_streambuf->Write(buffer, nbytes);
139 size_t newpos = m_o_streambuf->GetIntPosition();
140
141 // FIXME can someone please explain what this does? (VZ)
142 if ( !newpos )
143 newpos = m_o_streambuf->GetBufferSize();
144
145 return newpos - oldpos;
146 }
147
148 wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
149 {
150 return m_o_streambuf->Seek(pos, mode);
151 }
152
153 wxFileOffset wxMemoryOutputStream::OnSysTell() const
154 {
155 return m_o_streambuf->Tell();
156 }
157
158 size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const
159 {
160 wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") );
161
162 if ( len > GetSize() )
163 len = GetSize();
164
165 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
166
167 return len;
168 }
169
170 #endif // wxUSE_STREAMS