added wxStringStream classes

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@29223 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2004-09-19 21:26:45 +00:00
parent 95561814d9
commit 121fa06ab6
8 changed files with 291 additions and 0 deletions

117
src/common/sstream.cpp Normal file
View File

@@ -0,0 +1,117 @@
///////////////////////////////////////////////////////////////////////////////
// Name: common/sstream.cpp
// Purpose: string-based streams implementation
// Author: Vadim Zeitlin
// Modified by:
// Created: 2004-09-19
// RCS-ID: $Id$
// Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#if wxUSE_STREAMS
#include "wx/sstream.h"
// ============================================================================
// wxStringInputStream implementation
// ============================================================================
// ----------------------------------------------------------------------------
// seek/tell
// ----------------------------------------------------------------------------
off_t wxStringInputStream::OnSysSeek(off_t ofs, wxSeekMode mode)
{
switch ( mode )
{
case wxFromStart:
// nothing to do, ofs already ok
break;
case wxFromEnd:
ofs += m_str.length()*sizeof(wxChar);
break;
case wxFromCurrent:
ofs += m_pos;
break;
default:
wxFAIL_MSG( _T("invalid seek mode") );
return wxInvalidOffset;
}
m_pos = wx_static_cast(size_t, ofs);
return ofs;
}
off_t wxStringInputStream::OnSysTell() const
{
return wx_static_cast(off_t, m_pos);
}
// ----------------------------------------------------------------------------
// actual IO
// ----------------------------------------------------------------------------
size_t wxStringInputStream::OnSysRead(void *buffer, size_t size)
{
const size_t sizeMax = m_str.length()*sizeof(wxChar) - m_pos;
if ( size >= sizeMax )
{
if ( sizeMax == 0 )
{
m_lasterror = wxSTREAM_EOF;
return 0;
}
size = sizeMax;
}
memcpy(buffer, m_str.data() + m_pos, size);
m_pos += size;
return size;
}
// ============================================================================
// wxStringOutputStream implementation
// ============================================================================
// ----------------------------------------------------------------------------
// actual IO
// ----------------------------------------------------------------------------
size_t wxStringOutputStream::OnSysWrite(const void *buffer, size_t size)
{
// in Unicode mode we might not be able to write the last byte
size_t len = size / sizeof(wxChar);
const wxChar *p = wx_static_cast(const wxChar *, buffer);
m_str->Append(wxString(p, p + len + 1));
// return number of bytes actually written
return len*sizeof(wxChar);
}
#endif // wxUSE_STREAMS