]> git.saurik.com Git - wxWidgets.git/blame - src/common/mstream.cpp
fixed bug in parsing formats in which fields don't have any non-numeric delimiters
[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
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
67c8c225
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
3d4c6a21 20#ifdef __GNUG__
32fc4afb 21#pragma implementation "mstream.h"
3d4c6a21
GL
22#endif
23
79c3e0e1
GL
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
79c3e0e1
GL
26
27#ifdef __BORLANDC__
ce4169a4 28 #pragma hdrstop
79c3e0e1 29#endif
3d4c6a21 30
ce4169a4
RR
31#if wxUSE_STREAMS
32
33#include <stdlib.h>
3096bd2f
VZ
34#include "wx/stream.h"
35#include "wx/mstream.h"
ce4169a4 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
50 m_i_streambuf->Fixed(TRUE);
c980c992 51
67c8c225 52 m_length = len;
3d4c6a21
GL
53}
54
0cd9bfe8
GL
55wxMemoryInputStream::~wxMemoryInputStream()
56{
67c8c225 57 delete m_i_streambuf;
0cd9bfe8
GL
58}
59
32a4b1d5
GL
60char wxMemoryInputStream::Peek()
61{
67c8c225
VZ
62 char *buf = (char *)m_i_streambuf->GetBufferStart();
63
64 return buf[m_i_streambuf->GetIntPosition()];
c980c992
GL
65}
66
83141d3a
VZ
67bool wxMemoryInputStream::Eof() const
68{
67c8c225 69 return !m_i_streambuf->GetBytesLeft();
83141d3a
VZ
70}
71
c980c992 72size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
83141d3a
VZ
73{
74 size_t pos = m_i_streambuf->GetIntPosition();
75 if ( pos == m_length )
76 {
77 m_lasterror = wxSTREAM_EOF;
78
79 return 0;
80 }
81
82 m_i_streambuf->Read(buffer, nbytes);
83 m_lasterror = wxSTREAM_NOERROR;
84
85 return m_i_streambuf->GetIntPosition() - pos;
c980c992
GL
86}
87
88off_t wxMemoryInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
89{
67c8c225 90 return m_i_streambuf->Seek(pos, mode);
c980c992
GL
91}
92
93off_t wxMemoryInputStream::OnSysTell() const
94{
67c8c225 95 return m_i_streambuf->Tell();
3d4c6a21
GL
96}
97
79c3e0e1
GL
98// ----------------------------------------------------------------------------
99// wxMemoryOutputStream
100// ----------------------------------------------------------------------------
101
67c8c225 102wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len)
79c3e0e1 103{
67c8c225
VZ
104 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write);
105 if ( data )
106 m_o_streambuf->SetBufferIO(data, len);
107 m_o_streambuf->Fixed(FALSE);
108 m_o_streambuf->Flushable(FALSE);
79c3e0e1
GL
109}
110
0cd9bfe8 111wxMemoryOutputStream::~wxMemoryOutputStream()
79c3e0e1 112{
67c8c225 113 delete m_o_streambuf;
3d4c6a21 114}
ce4169a4 115
c980c992
GL
116size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
117{
67c8c225
VZ
118 size_t oldpos = m_o_streambuf->GetIntPosition();
119 m_o_streambuf->Write(buffer, nbytes);
120 size_t newpos = m_o_streambuf->GetIntPosition();
121
122 // FIXME can someone please explain what this does? (VZ)
123 if ( !newpos )
124 newpos = m_o_streambuf->GetBufferSize();
125
126 return newpos - oldpos;
c980c992
GL
127}
128
129off_t wxMemoryOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
130{
67c8c225 131 return m_o_streambuf->Seek(pos, mode);
c980c992
GL
132}
133
134off_t wxMemoryOutputStream::OnSysTell() const
135{
67c8c225 136 return m_o_streambuf->Tell();
c980c992
GL
137}
138
67c8c225 139size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const
a324a7bc 140{
67c8c225 141 wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") );
a324a7bc 142
67c8c225
VZ
143 if ( len > GetSize() )
144 len = GetSize();
a324a7bc 145
67c8c225
VZ
146 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
147
148 return len;
a324a7bc 149}
c980c992 150
67c8c225 151#endif // wxUSE_STREAMS