/****************************************************************************** libx42.net - skinned vertex animation library (.NET API) Copyright (C) 2007 HermitWorks Entertainment Corporation This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA. ******************************************************************************/ #include "StdAfx.h" #include "StreamAdapter.h" namespace Libx42 { using namespace System; using namespace System::Runtime::InteropServices; static size_t X42_CALL Stream_imp_read_fn( void *buffer, size_t cb, void *baton ) { try { GCHandle gch = GCHandle::FromIntPtr( IntPtr( baton ) ); StreamAdapter ^stm = (StreamAdapter^)gch.Target; array< Byte > ^buf = stm->GetBuffer( (uint)cb ); int cr = stm->BaseStream->Read( buf, 0, (int)cb ); pin_ptr< Byte > pinBuf= &buf[0]; memcpy( buffer, pinBuf, cr ); return cr; } catch( ... ) { return 0; } } static size_t X42_CALL Stream_imp_write_fn( const void *buffer, size_t cb, void *baton ) { try { GCHandle gch = GCHandle::FromIntPtr( IntPtr( baton ) ); StreamAdapter ^stm = (StreamAdapter^)gch.Target; array< Byte > ^buf = stm->GetBuffer( (uint)cb ); pin_ptr< Byte > pinBuf= &buf[0]; memcpy( pinBuf, buffer, cb ); pinBuf = nullptr; stm->BaseStream->Write( buf, 0, (int)cb ); return cb; } catch( ... ) { return 0; } } StreamAdapter::StreamAdapter( System::IO::Stream ^stream ) : stm( stream ) { if( !stream ) throw gcnew ArgumentNullException( "stream" ); gch = GCHandle::Alloc( this, GCHandleType::Weak ); } StreamAdapter::~StreamAdapter( void ) { StreamAdapter::!StreamAdapter(); //delete stm; stm = nullptr; buf = nullptr; } StreamAdapter::!StreamAdapter( void ) { delete inStm; inStm = 0; delete outStm; outStm = 0; gch.Free(); } x42inStream_t* StreamAdapter::InStream::get( void ) { if( !stm->CanRead ) throw gcnew InvalidOperationException( "Stream is not readable." ); if( !inStm ) { inStm = new x42inStream_t; inStm->read_fn = Stream_imp_read_fn; inStm->baton = GCHandle::ToIntPtr( gch ).ToPointer(); } return inStm; } x42outStream_t* StreamAdapter::OutStream::get( void ) { if( !stm->CanRead ) throw gcnew InvalidOperationException( "Stream is not writable." ); if( !outStm ) { outStm = new x42outStream_t; outStm->write_fn = Stream_imp_write_fn; outStm->baton = GCHandle::ToIntPtr( gch ).ToPointer(); } return outStm; } array< Byte >^ StreamAdapter::GetBuffer( uint cb ) { if( !buf || buf->Length < (int)cb ) buf = gcnew array< Byte >( cb ); return buf; } };