/*---------------------------------------------------------------------------* Project: NintendoWare File: io_RomFileStream.cpp Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Revision:$ *---------------------------------------------------------------------------*/ #include "precompiled.h" #include namespace nw { namespace io { NW_UT_RUNTIME_TYPEINFO_DEFINITION(RomFileStream,IOStream); RomFileStream::RomFileStream() { Initialize(); } // RomFileStream::RomFileStream( const wchar_t* path ) RomFileStream::RomFileStream( const char* path ) { Initialize(); Open( path ); } #ifdef NW_PLATFORM_CTRWIN RomFileStream::RomFileStream( const nn::fs::File* openedFile, bool closeEnable ) { Initialize(); Open( openedFile, closeEnable ); } #else RomFileStream::RomFileStream( nn::fs::FileReader* openedFileReader, bool closeEnable ) { Initialize(); Open( openedFileReader, closeEnable ); } #endif RomFileStream::~RomFileStream() { if ( m_CloseOnDestroyFlag ) { Close(); } } #ifdef NW_PLATFORM_CTRWIN bool RomFileStream::Open( const nn::fs::File* openedFile, bool closeEnable ) { // 開かれているファイルがありファイルクローズが可能な場合にファイルを閉じます if ( m_CloseOnDestroyFlag ) { Close(); } m_File = *openedFile; m_FilePosition.SetFileSize( nn::fs::GetLength( &m_File ) ); m_FilePosition.Seek( 0, FILE_STREAM_SEEK_BEGIN ); // オープン済みのファイルハンドラを渡されているので、 // クローズするかどうかはユーザの指定に任せる。 m_CloseOnDestroyFlag = false; m_CloseEnableFlag = closeEnable; m_IsAvailable = true; return true; } bool RomFileStream::Open( const char* path ) { NW_NULL_ASSERT( path ); if ( nn::fs::OpenFile( &m_File, path ) ) { m_FilePosition.SetFileSize( nn::fs::GetLength( &m_File ) ); m_FilePosition.Seek( 0, FILE_STREAM_SEEK_BEGIN ); m_CloseOnDestroyFlag = true; m_CloseEnableFlag = true; m_IsAvailable = true; return true; } return false; } #else bool RomFileStream::Open( nn::fs::FileReader* openedFileReader, bool closeEnable ) { // 開かれているファイルがありファイルクローズが可能な場合にファイルを閉じます if ( m_CloseOnDestroyFlag ) { Close(); } m_pOpenedFileReader = openedFileReader; m_FilePosition.SetFileSize( m_pOpenedFileReader->GetSize() ); m_FilePosition.Seek( 0, FILE_STREAM_SEEK_BEGIN ); // オープン済みのファイルハンドラを渡されているので、 // クローズするかどうかはユーザの指定に任せる。 m_CloseOnDestroyFlag = false; m_CloseEnableFlag = closeEnable; m_IsAvailable = true; return true; } // bool RomFileStream::Open( const wchar_t* path ) bool RomFileStream::Open( const char* path ) { NW_NULL_ASSERT( path ); m_FileReader.Initialize( path ); m_pOpenedFileReader = &m_FileReader; m_FilePosition.SetFileSize( m_pOpenedFileReader->GetSize() ); m_FilePosition.Seek( 0, FILE_STREAM_SEEK_BEGIN ); m_CloseOnDestroyFlag = true; m_CloseEnableFlag = true; m_IsAvailable = true; return true; } #endif /* NW_PLATFORM_CTRWIN */ void RomFileStream::Initialize() { m_IsAvailable = false; m_CloseOnDestroyFlag = false; } void RomFileStream::Close() { if ( m_CloseEnableFlag && m_IsAvailable ) { #ifdef NW_PLATFORM_CTRWIN nn::fs::CloseFile( &m_File ); #else m_pOpenedFileReader->Finalize(); #endif m_IsAvailable = false; } } s32 RomFileStream::Read( void* buf, u32 length ) { // NW_ALIGN32_ASSERT( buf ); // 必要? NW_ALLIGN32_ASSERT( length ); // 必要? NW_ALIGN4_ASSERT( m_FilePosition.Tell() ); NW_ASSERTMSG( m_IsAvailable, "RomFileStream is not opened" ); #ifdef NW_PLATFORM_CTRWIN if ( nn::fs::SeekFile( &m_File, m_FilePosition.Tell(), nn::fs::FS_SEEK_SET ) ) { s32 readBytes = nn::fs::ReadFile( &m_File, buf, length ); if ( readBytes > 0 ) { m_FilePosition.Skip( readBytes ); } return readBytes; } return -1; #else m_pOpenedFileReader->Seek( m_FilePosition.Tell(), nn::fs::POSITION_BASE_BEGIN ); s32 readBytes = m_pOpenedFileReader->Read( buf, length ); if ( readBytes > 0 ) { m_FilePosition.Skip( readBytes ); } return readBytes; #endif } void RomFileStream::Seek( s32 offset, u32 origin ) { NW_ALIGN4_ASSERT( offset ); NW_ASSERTMSG( m_IsAvailable, "RomFileStream is not opened" ); m_FilePosition.Seek( offset, origin ); } } // namespace nw::io } // namespace nw