]> git.saurik.com Git - wxWidgets.git/blame - src/common/mstream.cpp
fixed Fit() to call SetClientSize() and not SetSize()
[wxWidgets.git] / src / common / mstream.cpp
CommitLineData
3d4c6a21 1/////////////////////////////////////////////////////////////////////////////
67c8c225 2// Name: src/common/mstream.cpp
3d4c6a21
GL
3// Purpose: "Memory stream" classes
4// Author: Guilhem Lavaux
67c8c225 5// Modified by: VZ (23.11.00): general code review
3d4c6a21
GL
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
65571936 9// Licence: wxWindows licence
3d4c6a21
GL
10/////////////////////////////////////////////////////////////////////////////
11
67c8c225
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
79c3e0e1
GL
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
79c3e0e1
GL
22
23#ifdef __BORLANDC__
ce4169a4 24 #pragma hdrstop
79c3e0e1 25#endif
3d4c6a21 26
ce4169a4
RR
27#if wxUSE_STREAMS
28
29#include <stdlib.h>
3096bd2f
VZ
30#include "wx/stream.h"
31#include "wx/mstream.h"
ce4169a4 32
67c8c225
VZ
33// ============================================================================
34// implementation
35// ============================================================================
36
79c3e0e1
GL
37// ----------------------------------------------------------------------------
38// wxMemoryInputStream
39// ----------------------------------------------------------------------------
40
67c8c225 41wxMemoryInputStream::wxMemoryInputStream(const void *data, size_t len)
79c3e0e1 42{
67c8c225
VZ
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
4e32eea1 46 m_i_streambuf->Fixed(true);
c980c992 47
67c8c225 48 m_length = len;
3d4c6a21
GL
49}
50
96461cc2
VZ
51wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream)
52{
17a1ebd1
VZ
53 const wxFileOffset lenFile = stream.GetLength();
54 if ( lenFile == wxInvalidOffset )
55 {
96461cc2
VZ
56 m_i_streambuf = NULL;
57 m_lasterror = wxSTREAM_EOF;
58 return;
59 }
17a1ebd1
VZ
60
61 const size_t len = wx_truncate_cast(size_t, lenFile);
4a10ea8b 62 wxASSERT_MSG( len == lenFile + size_t(0), _T("huge files not supported") );
17a1ebd1 63
96461cc2
VZ
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
0cd9bfe8
GL
72wxMemoryInputStream::~wxMemoryInputStream()
73{
67c8c225 74 delete m_i_streambuf;
0cd9bfe8
GL
75}
76
32a4b1d5
GL
77char wxMemoryInputStream::Peek()
78{
67c8c225 79 char *buf = (char *)m_i_streambuf->GetBufferStart();
6c783b03
MB
80 size_t pos = m_i_streambuf->GetIntPosition();
81 if ( pos == m_length )
82 {
83 m_lasterror = wxSTREAM_READ_ERROR;
67c8c225 84
6c783b03
MB
85 return 0;
86 }
87
88 return buf[pos];
c980c992
GL
89}
90
83141d3a
VZ
91bool wxMemoryInputStream::Eof() const
92{
67c8c225 93 return !m_i_streambuf->GetBytesLeft();
83141d3a
VZ
94}
95
c980c992 96size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
83141d3a
VZ
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);
2b5f62a0 107 m_lasterror = wxSTREAM_NO_ERROR;
83141d3a
VZ
108
109 return m_i_streambuf->GetIntPosition() - pos;
c980c992
GL
110}
111
4004775e 112wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
c980c992 113{
67c8c225 114 return m_i_streambuf->Seek(pos, mode);
c980c992
GL
115}
116
4004775e 117wxFileOffset wxMemoryInputStream::OnSysTell() const
c980c992 118{
67c8c225 119 return m_i_streambuf->Tell();
3d4c6a21
GL
120}
121
79c3e0e1
GL
122// ----------------------------------------------------------------------------
123// wxMemoryOutputStream
124// ----------------------------------------------------------------------------
125
67c8c225 126wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len)
79c3e0e1 127{
67c8c225
VZ
128 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write);
129 if ( data )
130 m_o_streambuf->SetBufferIO(data, len);
4e32eea1
WS
131 m_o_streambuf->Fixed(false);
132 m_o_streambuf->Flushable(false);
79c3e0e1
GL
133}
134
0cd9bfe8 135wxMemoryOutputStream::~wxMemoryOutputStream()
79c3e0e1 136{
67c8c225 137 delete m_o_streambuf;
3d4c6a21 138}
ce4169a4 139
c980c992
GL
140size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
141{
67c8c225
VZ
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;
c980c992
GL
151}
152
4004775e 153wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
c980c992 154{
67c8c225 155 return m_o_streambuf->Seek(pos, mode);
c980c992
GL
156}
157
4004775e 158wxFileOffset wxMemoryOutputStream::OnSysTell() const
c980c992 159{
67c8c225 160 return m_o_streambuf->Tell();
c980c992
GL
161}
162
67c8c225 163size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const
a324a7bc 164{
67c8c225 165 wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") );
a324a7bc 166
67c8c225
VZ
167 if ( len > GetSize() )
168 len = GetSize();
a324a7bc 169
67c8c225
VZ
170 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
171
172 return len;
a324a7bc 173}
c980c992 174
67c8c225 175#endif // wxUSE_STREAMS