]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/mstream.cpp
wxHTML src code indentation now conforms (more) to wxWin coding style
[wxWidgets.git] / src / common / mstream.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: mstream.cpp
3// Purpose: "Memory stream" classes
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "mstream.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
23#if wxUSE_STREAMS
24
25#include <stdlib.h>
26#include "wx/stream.h"
27#include "wx/mstream.h"
28
29// ----------------------------------------------------------------------------
30// wxMemoryInputStream
31// ----------------------------------------------------------------------------
32
33wxMemoryInputStream::wxMemoryInputStream(const char *data, size_t len)
34 : wxInputStream()
35{
36 m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read);
37 m_i_streambuf->SetBufferIO((char*) data, (char*) (data+len));
38 m_i_streambuf->SetIntPosition(0); // seek to start pos
39 m_i_streambuf->Fixed(TRUE);
40
41 m_length = len;
42}
43
44wxMemoryInputStream::~wxMemoryInputStream()
45{
46 delete m_i_streambuf;
47}
48
49char wxMemoryInputStream::Peek()
50{
51 return m_i_streambuf->GetBufferStart()[m_i_streambuf->GetIntPosition()];
52}
53
54bool wxMemoryInputStream::Eof() const
55{
56 return m_i_streambuf->GetBufferPos() == m_i_streambuf->GetBufferEnd();
57}
58
59size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
60{
61 size_t pos = m_i_streambuf->GetIntPosition();
62 if ( pos == m_length )
63 {
64 m_lasterror = wxSTREAM_EOF;
65
66 return 0;
67 }
68
69 m_i_streambuf->Read(buffer, nbytes);
70 m_lasterror = wxSTREAM_NOERROR;
71
72 return m_i_streambuf->GetIntPosition() - pos;
73}
74
75off_t wxMemoryInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
76{
77 return m_i_streambuf->Seek(pos, mode);
78}
79
80off_t wxMemoryInputStream::OnSysTell() const
81{
82 return m_i_streambuf->Tell();
83}
84
85// ----------------------------------------------------------------------------
86// wxMemoryOutputStream
87// ----------------------------------------------------------------------------
88
89wxMemoryOutputStream::wxMemoryOutputStream(char *data, size_t len)
90 : wxOutputStream()
91{
92 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write);
93 if (data)
94 m_o_streambuf->SetBufferIO(data, data+len);
95 m_o_streambuf->Fixed(FALSE);
96 m_o_streambuf->Flushable(FALSE);
97}
98
99wxMemoryOutputStream::~wxMemoryOutputStream()
100{
101 delete m_o_streambuf;
102}
103
104size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
105{
106 size_t bufsize = m_o_streambuf->GetBufferEnd() - m_o_streambuf->GetBufferStart();
107 size_t oldpos = m_o_streambuf->GetIntPosition();
108 m_o_streambuf->Write(buffer, nbytes);
109 size_t newpos = m_o_streambuf->GetIntPosition();
110 if (newpos == 0) return bufsize - oldpos;
111 else return newpos - oldpos;
112}
113
114off_t wxMemoryOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
115{
116 return m_o_streambuf->Seek(pos, mode);
117}
118
119off_t wxMemoryOutputStream::OnSysTell() const
120{
121 return m_o_streambuf->Tell();
122}
123
124size_t wxMemoryOutputStream::CopyTo(char *buffer, size_t len) const
125{
126 if (!buffer)
127 return 0;
128
129 if (len > GetSize())
130 len = GetSize();
131
132 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
133 return len;
134}
135
136#endif