]>
git.saurik.com Git - wxWidgets.git/blob - src/common/mstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: "Memory stream" classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "mstream.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/stream.h"
27 #include "wx/mstream.h"
29 // ----------------------------------------------------------------------------
30 // wxMemoryInputStream
31 // ----------------------------------------------------------------------------
33 wxMemoryInputStream::wxMemoryInputStream(const char *data
, size_t len
)
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
);
44 wxMemoryInputStream::~wxMemoryInputStream()
49 char wxMemoryInputStream::Peek()
51 return m_i_streambuf
->GetBufferStart()[m_i_streambuf
->GetIntPosition()];
54 bool wxMemoryInputStream::Eof() const
56 return m_i_streambuf
->GetBufferPos() == m_i_streambuf
->GetBufferEnd();
59 size_t wxMemoryInputStream::OnSysRead(void *buffer
, size_t nbytes
)
61 size_t pos
= m_i_streambuf
->GetIntPosition();
62 if ( pos
== m_length
)
64 m_lasterror
= wxSTREAM_EOF
;
69 m_i_streambuf
->Read(buffer
, nbytes
);
70 m_lasterror
= wxSTREAM_NOERROR
;
72 return m_i_streambuf
->GetIntPosition() - pos
;
75 off_t
wxMemoryInputStream::OnSysSeek(off_t pos
, wxSeekMode mode
)
77 return m_i_streambuf
->Seek(pos
, mode
);
80 off_t
wxMemoryInputStream::OnSysTell() const
82 return m_i_streambuf
->Tell();
85 // ----------------------------------------------------------------------------
86 // wxMemoryOutputStream
87 // ----------------------------------------------------------------------------
89 wxMemoryOutputStream::wxMemoryOutputStream(char *data
, size_t len
)
92 m_o_streambuf
= new wxStreamBuffer(wxStreamBuffer::write
);
94 m_o_streambuf
->SetBufferIO(data
, data
+len
);
95 m_o_streambuf
->Fixed(FALSE
);
96 m_o_streambuf
->Flushable(FALSE
);
99 wxMemoryOutputStream::~wxMemoryOutputStream()
101 delete m_o_streambuf
;
104 size_t wxMemoryOutputStream::OnSysWrite(const void *buffer
, size_t nbytes
)
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
;
114 off_t
wxMemoryOutputStream::OnSysSeek(off_t pos
, wxSeekMode mode
)
116 return m_o_streambuf
->Seek(pos
, mode
);
119 off_t
wxMemoryOutputStream::OnSysTell() const
121 return m_o_streambuf
->Tell();
124 size_t wxMemoryOutputStream::CopyTo(char *buffer
, size_t len
) const
132 memcpy(buffer
, m_o_streambuf
->GetBufferStart(), len
);