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: mstream.cpp 39001 2006-05-03 21:50:35Z ABX $ 8// Copyright: (c) Guilhem Lavaux 9// Licence: wxWindows licence 10///////////////////////////////////////////////////////////////////////////// 11 12// ============================================================================ 13// declarations 14// ============================================================================ 15 16// ---------------------------------------------------------------------------- 17// headers 18// ---------------------------------------------------------------------------- 19 20// For compilers that support precompilation, includes "wx.h". 21#include "wx/wxprec.h" 22 23#ifdef __BORLANDC__ 24 #pragma hdrstop 25#endif 26 27#if wxUSE_STREAMS 28 29#include "wx/mstream.h" 30 31#ifndef WX_PRECOMP 32 #include "wx/stream.h" 33#endif //WX_PRECOMP 34 35#include <stdlib.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(const wxMemoryOutputStream& stream) 56{ 57 const wxFileOffset lenFile = stream.GetLength(); 58 if ( lenFile == wxInvalidOffset ) 59 { 60 m_i_streambuf = NULL; 61 m_lasterror = wxSTREAM_EOF; 62 return; 63 } 64 65 const size_t len = wx_truncate_cast(size_t, lenFile); 66 wxASSERT_MSG( len == lenFile + size_t(0), _T("huge files not supported") ); 67 68 m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read); 69 m_i_streambuf->SetBufferIO(len); // create buffer 70 stream.CopyTo(m_i_streambuf->GetBufferStart(), len); 71 m_i_streambuf->SetIntPosition(0); // seek to start pos 72 m_i_streambuf->Fixed(true); 73 m_length = len; 74} 75 76wxMemoryInputStream::~wxMemoryInputStream() 77{ 78 delete m_i_streambuf; 79} 80 81char wxMemoryInputStream::Peek() 82{ 83 char *buf = (char *)m_i_streambuf->GetBufferStart(); 84 size_t pos = m_i_streambuf->GetIntPosition(); 85 if ( pos == m_length ) 86 { 87 m_lasterror = wxSTREAM_READ_ERROR; 88 89 return 0; 90 } 91 92 return buf[pos]; 93} 94 95size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes) 96{ 97 size_t pos = m_i_streambuf->GetIntPosition(); 98 if ( pos == m_length ) 99 { 100 m_lasterror = wxSTREAM_EOF; 101 102 return 0; 103 } 104 105 m_i_streambuf->Read(buffer, nbytes); 106 m_lasterror = wxSTREAM_NO_ERROR; 107 108 return m_i_streambuf->GetIntPosition() - pos; 109} 110 111wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) 112{ 113 return m_i_streambuf->Seek(pos, mode); 114} 115 116wxFileOffset wxMemoryInputStream::OnSysTell() const 117{ 118 return m_i_streambuf->Tell(); 119} 120 121// ---------------------------------------------------------------------------- 122// wxMemoryOutputStream 123// ---------------------------------------------------------------------------- 124 125wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len) 126{ 127 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write); 128 if ( data ) 129 m_o_streambuf->SetBufferIO(data, len); 130 m_o_streambuf->Fixed(false); 131 m_o_streambuf->Flushable(false); 132} 133 134wxMemoryOutputStream::~wxMemoryOutputStream() 135{ 136 delete m_o_streambuf; 137} 138 139size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes) 140{ 141 size_t oldpos = m_o_streambuf->GetIntPosition(); 142 m_o_streambuf->Write(buffer, nbytes); 143 size_t newpos = m_o_streambuf->GetIntPosition(); 144 145 // FIXME can someone please explain what this does? (VZ) 146 if ( !newpos ) 147 newpos = m_o_streambuf->GetBufferSize(); 148 149 return newpos - oldpos; 150} 151 152wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) 153{ 154 return m_o_streambuf->Seek(pos, mode); 155} 156 157wxFileOffset wxMemoryOutputStream::OnSysTell() const 158{ 159 return m_o_streambuf->Tell(); 160} 161 162size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const 163{ 164 wxCHECK_MSG( buffer, 0, _T("must have buffer to CopyTo") ); 165 166 if ( len > GetSize() ) 167 len = GetSize(); 168 169 memcpy(buffer, m_o_streambuf->GetBufferStart(), len); 170 171 return len; 172} 173 174#endif // wxUSE_STREAMS 175