]> git.saurik.com Git - wxWidgets.git/blob - src/common/mstream.cpp
Create non-GUI log target until GUI logging is available, allows stderr messages...
[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()
56 {
57 delete m_i_streambuf;
58 }
59
60 char wxMemoryInputStream::Peek()
61 {
62 char *buf = (char *)m_i_streambuf->GetBufferStart();
63 size_t pos = m_i_streambuf->GetIntPosition();
64 if ( pos == m_length )
65 {
66 m_lasterror = wxSTREAM_READ_ERROR;
67
68 return 0;
69 }
70
71 return buf[pos];
72 }
73
74 bool wxMemoryInputStream::Eof() const
75 {
76 return !m_i_streambuf->GetBytesLeft();
77 }
78
79 size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
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);
90 m_lasterror = wxSTREAM_NO_ERROR;
91
92 return m_i_streambuf->GetIntPosition() - pos;
93 }
94
95 wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
96 {
97 return m_i_streambuf->Seek(pos, mode);
98 }
99
100 wxFileOffset wxMemoryInputStream::OnSysTell() const
101 {
102 return m_i_streambuf->Tell();
103 }
104
105 // ----------------------------------------------------------------------------
106 // wxMemoryOutputStream
107 // ----------------------------------------------------------------------------
108
109 wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len)
110 {
111 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write);
112 if ( data )
113 m_o_streambuf->SetBufferIO(data, len);
114 m_o_streambuf->Fixed(false);
115 m_o_streambuf->Flushable(false);
116 }
117
118 wxMemoryOutputStream::~wxMemoryOutputStream()
119 {
120 delete m_o_streambuf;
121 }
122
123 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
124 {
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;
134 }
135
136 wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
137 {
138 return m_o_streambuf->Seek(pos, mode);
139 }
140
141 wxFileOffset wxMemoryOutputStream::OnSysTell() const
142 {
143 return m_o_streambuf->Tell();
144 }
145
146 size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const
147 {
148 wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") );
149
150 if ( len > GetSize() )
151 len = GetSize();
152
153 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
154
155 return len;
156 }
157
158 #endif // wxUSE_STREAMS