]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/mstream.cpp
added test for writing empty value to the config
[wxWidgets.git] / src / common / mstream.cpp
... / ...
CommitLineData
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 license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
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
45wxMemoryInputStream::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
55wxMemoryInputStream::~wxMemoryInputStream()
56{
57 delete m_i_streambuf;
58}
59
60char wxMemoryInputStream::Peek()
61{
62 char *buf = (char *)m_i_streambuf->GetBufferStart();
63
64 return buf[m_i_streambuf->GetIntPosition()];
65}
66
67bool wxMemoryInputStream::Eof() const
68{
69 return !m_i_streambuf->GetBytesLeft();
70}
71
72size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
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;
86}
87
88off_t wxMemoryInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
89{
90 return m_i_streambuf->Seek(pos, mode);
91}
92
93off_t wxMemoryInputStream::OnSysTell() const
94{
95 return m_i_streambuf->Tell();
96}
97
98// ----------------------------------------------------------------------------
99// wxMemoryOutputStream
100// ----------------------------------------------------------------------------
101
102wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len)
103{
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);
109}
110
111wxMemoryOutputStream::~wxMemoryOutputStream()
112{
113 delete m_o_streambuf;
114}
115
116size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
117{
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;
127}
128
129off_t wxMemoryOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
130{
131 return m_o_streambuf->Seek(pos, mode);
132}
133
134off_t wxMemoryOutputStream::OnSysTell() const
135{
136 return m_o_streambuf->Tell();
137}
138
139size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const
140{
141 wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") );
142
143 if ( len > GetSize() )
144 len = GetSize();
145
146 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
147
148 return len;
149}
150
151#endif // wxUSE_STREAMS