Fixed wxMemoryOutputStream (it wasn't working at all)
[wxWidgets.git] / src / common / mstream.cpp
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
33 wxMemoryInputStream::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
44 wxMemoryInputStream::~wxMemoryInputStream()
45 {
46 delete m_i_streambuf;
47 }
48
49 char wxMemoryInputStream::Peek()
50 {
51 return m_i_streambuf->GetBufferStart()[m_i_streambuf->GetIntPosition()];
52 }
53
54 size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
55 {
56 m_lastcount = 0;
57 return m_i_streambuf->Read(buffer, nbytes);
58 }
59
60 off_t wxMemoryInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
61 {
62 return m_i_streambuf->Seek(pos, mode);
63 }
64
65 off_t wxMemoryInputStream::OnSysTell() const
66 {
67 return m_i_streambuf->Tell();
68 }
69
70 // ----------------------------------------------------------------------------
71 // wxMemoryOutputStream
72 // ----------------------------------------------------------------------------
73
74 wxMemoryOutputStream::wxMemoryOutputStream(char *data, size_t len)
75 : wxOutputStream()
76 {
77 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write);
78 if (data)
79 m_o_streambuf->SetBufferIO(data, data+len);
80 m_o_streambuf->Fixed(FALSE);
81 m_o_streambuf->Flushable(FALSE);
82 }
83
84 wxMemoryOutputStream::~wxMemoryOutputStream()
85 {
86 delete m_o_streambuf;
87 }
88
89 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
90 {
91 m_lastcount = 0;
92 return m_o_streambuf->Write(buffer, nbytes);
93 }
94
95 off_t wxMemoryOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
96 {
97 return m_o_streambuf->Seek(pos, mode);
98 }
99
100 off_t wxMemoryOutputStream::OnSysTell() const
101 {
102 return m_o_streambuf->Tell();
103 }
104
105 size_t wxMemoryOutputStream::CopyTo(char *buffer, size_t len) const
106 {
107 if (!buffer)
108 return 0;
109
110 if (len > GetSize())
111 len = GetSize();
112
113 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
114 return len;
115 }
116
117 #endif