]> git.saurik.com Git - wxWidgets.git/blame - src/common/mstream.cpp
wx/debug.h is always unconditionally included by wx/defs.h
[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__
530ecef0 24 #pragma hdrstop
79c3e0e1 25#endif
3d4c6a21 26
ce4169a4
RR
27#if wxUSE_STREAMS
28
3096bd2f 29#include "wx/mstream.h"
ce4169a4 30
530ecef0
WS
31#ifndef WX_PRECOMP
32 #include "wx/stream.h"
33#endif //WX_PRECOMP
34
35#include <stdlib.h>
36
67c8c225
VZ
37// ============================================================================
38// implementation
39// ============================================================================
40
79c3e0e1
GL
41// ----------------------------------------------------------------------------
42// wxMemoryInputStream
43// ----------------------------------------------------------------------------
44
67c8c225 45wxMemoryInputStream::wxMemoryInputStream(const void *data, size_t len)
79c3e0e1 46{
67c8c225
VZ
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
4e32eea1 50 m_i_streambuf->Fixed(true);
c980c992 51
67c8c225 52 m_length = len;
3d4c6a21
GL
53}
54
96461cc2
VZ
55wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream)
56{
17a1ebd1
VZ
57 const wxFileOffset lenFile = stream.GetLength();
58 if ( lenFile == wxInvalidOffset )
59 {
96461cc2
VZ
60 m_i_streambuf = NULL;
61 m_lasterror = wxSTREAM_EOF;
62 return;
63 }
17a1ebd1
VZ
64
65 const size_t len = wx_truncate_cast(size_t, lenFile);
4a10ea8b 66 wxASSERT_MSG( len == lenFile + size_t(0), _T("huge files not supported") );
17a1ebd1 67
96461cc2
VZ
68 m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read);
69 m_i_streambuf->SetBufferIO(len); // create buffer
70 stream.CopyTo(m_i_streambuf->GetBufferStart(), len);
71 m_i_streambuf->SetIntPosition(0); // seek to start pos
72 m_i_streambuf->Fixed(true);
73 m_length = len;
74}
75
0cd9bfe8
GL
76wxMemoryInputStream::~wxMemoryInputStream()
77{
67c8c225 78 delete m_i_streambuf;
0cd9bfe8
GL
79}
80
32a4b1d5
GL
81char wxMemoryInputStream::Peek()
82{
67c8c225 83 char *buf = (char *)m_i_streambuf->GetBufferStart();
6c783b03
MB
84 size_t pos = m_i_streambuf->GetIntPosition();
85 if ( pos == m_length )
86 {
87 m_lasterror = wxSTREAM_READ_ERROR;
67c8c225 88
6c783b03
MB
89 return 0;
90 }
91
92 return buf[pos];
c980c992
GL
93}
94
95size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
83141d3a
VZ
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);
2b5f62a0 106 m_lasterror = wxSTREAM_NO_ERROR;
83141d3a
VZ
107
108 return m_i_streambuf->GetIntPosition() - pos;
c980c992
GL
109}
110
4004775e 111wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
c980c992 112{
67c8c225 113 return m_i_streambuf->Seek(pos, mode);
c980c992
GL
114}
115
4004775e 116wxFileOffset wxMemoryInputStream::OnSysTell() const
c980c992 117{
67c8c225 118 return m_i_streambuf->Tell();
3d4c6a21
GL
119}
120
79c3e0e1
GL
121// ----------------------------------------------------------------------------
122// wxMemoryOutputStream
123// ----------------------------------------------------------------------------
124
67c8c225 125wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len)
79c3e0e1 126{
67c8c225
VZ
127 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write);
128 if ( data )
129 m_o_streambuf->SetBufferIO(data, len);
4e32eea1
WS
130 m_o_streambuf->Fixed(false);
131 m_o_streambuf->Flushable(false);
79c3e0e1
GL
132}
133
0cd9bfe8 134wxMemoryOutputStream::~wxMemoryOutputStream()
79c3e0e1 135{
67c8c225 136 delete m_o_streambuf;
3d4c6a21 137}
ce4169a4 138
c980c992
GL
139size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
140{
67c8c225
VZ
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;
c980c992
GL
150}
151
4004775e 152wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
c980c992 153{
67c8c225 154 return m_o_streambuf->Seek(pos, mode);
c980c992
GL
155}
156
4004775e 157wxFileOffset wxMemoryOutputStream::OnSysTell() const
c980c992 158{
67c8c225 159 return m_o_streambuf->Tell();
c980c992
GL
160}
161
67c8c225 162size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const
a324a7bc 163{
67c8c225 164 wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") );
a324a7bc 165
67c8c225
VZ
166 if ( len > GetSize() )
167 len = GetSize();
a324a7bc 168
67c8c225
VZ
169 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
170
171 return len;
a324a7bc 172}
c980c992 173
67c8c225 174#endif // wxUSE_STREAMS