1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     io_RomFileStream.cpp
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision:$
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/io/io_RomFileStream.h>
19 
20 namespace nw {
21 namespace io {
22 
23 NW_UT_RUNTIME_TYPEINFO_DEFINITION(RomFileStream,IOStream);
24 
RomFileStream()25 RomFileStream::RomFileStream()
26 {
27     Initialize();
28 }
29 
30 // RomFileStream::RomFileStream( const wchar_t* path )
RomFileStream(const char * path)31 RomFileStream::RomFileStream( const char* path )
32 {
33     Initialize();
34     Open( path );
35 }
36 #ifdef NW_PLATFORM_CTRWIN
RomFileStream(const nn::fs::File * openedFile,bool closeEnable)37 RomFileStream::RomFileStream( const nn::fs::File* openedFile,  bool closeEnable )
38 {
39     Initialize();
40     Open( openedFile, closeEnable );
41 }
42 #else
RomFileStream(nn::fs::FileReader * openedFileReader,bool closeEnable)43 RomFileStream::RomFileStream( nn::fs::FileReader* openedFileReader,  bool closeEnable )
44 {
45     Initialize();
46     Open( openedFileReader, closeEnable );
47 }
48 #endif
49 
~RomFileStream()50 RomFileStream::~RomFileStream()
51 {
52     if ( m_CloseOnDestroyFlag )
53     {
54         Close();
55     }
56 }
57 
58 #ifdef NW_PLATFORM_CTRWIN
59 
Open(const nn::fs::File * openedFile,bool closeEnable)60 bool RomFileStream::Open( const nn::fs::File* openedFile, bool closeEnable )
61 {
62     // 開かれているファイルがありファイルクローズが可能な場合にファイルを閉じます
63     if ( m_CloseOnDestroyFlag )
64     {
65         Close();
66     }
67 
68     m_File = *openedFile;
69     m_FilePosition.SetFileSize( nn::fs::GetLength( &m_File ) );
70     m_FilePosition.Seek( 0, FILE_STREAM_SEEK_BEGIN );
71 
72     // オープン済みのファイルハンドラを渡されているので、
73     // クローズするかどうかはユーザの指定に任せる。
74     m_CloseOnDestroyFlag = false;
75     m_CloseEnableFlag = closeEnable;
76     m_IsAvailable = true;
77     return true;
78 }
Open(const char * path)79 bool RomFileStream::Open( const char* path )
80 {
81     NW_NULL_ASSERT( path );
82 
83     if ( nn::fs::OpenFile( &m_File, path ) )
84     {
85         m_FilePosition.SetFileSize( nn::fs::GetLength( &m_File ) );
86         m_FilePosition.Seek( 0, FILE_STREAM_SEEK_BEGIN );
87         m_CloseOnDestroyFlag = true;
88         m_CloseEnableFlag = true;
89         m_IsAvailable = true;
90         return true;
91     }
92     return false;
93 }
94 
95 #else
96 
Open(nn::fs::FileReader * openedFileReader,bool closeEnable)97 bool RomFileStream::Open( nn::fs::FileReader* openedFileReader, bool closeEnable )
98 {
99     // 開かれているファイルがありファイルクローズが可能な場合にファイルを閉じます
100     if ( m_CloseOnDestroyFlag )
101     {
102         Close();
103     }
104 
105     m_pOpenedFileReader = openedFileReader;
106     m_FilePosition.SetFileSize( m_pOpenedFileReader->GetSize() );
107     m_FilePosition.Seek( 0, FILE_STREAM_SEEK_BEGIN );
108 
109     // オープン済みのファイルハンドラを渡されているので、
110     // クローズするかどうかはユーザの指定に任せる。
111     m_CloseOnDestroyFlag = false;
112     m_CloseEnableFlag = closeEnable;
113     m_IsAvailable = true;
114     return true;
115 }
116 // bool RomFileStream::Open( const wchar_t* path )
Open(const char * path)117 bool RomFileStream::Open( const char* path )
118 {
119     NW_NULL_ASSERT( path );
120 
121     m_FileReader.Initialize( path );
122     m_pOpenedFileReader = &m_FileReader;
123     m_FilePosition.SetFileSize( m_pOpenedFileReader->GetSize() );
124     m_FilePosition.Seek( 0, FILE_STREAM_SEEK_BEGIN );
125     m_CloseOnDestroyFlag = true;
126     m_CloseEnableFlag = true;
127     m_IsAvailable = true;
128     return true;
129 }
130 
131 #endif /* NW_PLATFORM_CTRWIN */
132 
133 
Initialize()134 void RomFileStream::Initialize()
135 {
136     m_IsAvailable = false;
137     m_CloseOnDestroyFlag = false;
138 }
139 
Close()140 void RomFileStream::Close()
141 {
142     if ( m_CloseEnableFlag && m_IsAvailable )
143     {
144 #ifdef NW_PLATFORM_CTRWIN
145         nn::fs::CloseFile( &m_File );
146 #else
147         m_pOpenedFileReader->Finalize();
148 #endif
149         m_IsAvailable = false;
150     }
151 }
152 
Read(void * buf,u32 length)153 s32 RomFileStream::Read( void* buf, u32 length )
154 {
155     // NW_ALIGN32_ASSERT( buf );
156     // 必要? NW_ALLIGN32_ASSERT( length );
157     // 必要? NW_ALIGN4_ASSERT( m_FilePosition.Tell() );
158     NW_ASSERTMSG( m_IsAvailable, "RomFileStream is not opened" );
159 
160 #ifdef NW_PLATFORM_CTRWIN
161     if ( nn::fs::SeekFile( &m_File, m_FilePosition.Tell(), nn::fs::FS_SEEK_SET ) )
162     {
163         s32 readBytes = nn::fs::ReadFile( &m_File, buf, length );
164         if ( readBytes > 0 )
165         {
166             m_FilePosition.Skip( readBytes );
167         }
168         return readBytes;
169     }
170     return -1;
171 #else
172     m_pOpenedFileReader->Seek( m_FilePosition.Tell(), nn::fs::POSITION_BASE_BEGIN );
173     s32 readBytes = m_pOpenedFileReader->Read( buf, length );
174     if ( readBytes > 0 )
175     {
176         m_FilePosition.Skip( readBytes );
177     }
178     return readBytes;
179 #endif
180 }
181 
Seek(s32 offset,u32 origin)182 void RomFileStream::Seek( s32 offset, u32 origin )
183 {
184     NW_ALIGN4_ASSERT( offset );
185     NW_ASSERTMSG( m_IsAvailable, "RomFileStream is not opened" );
186     m_FilePosition.Seek( offset, origin );
187 }
188 
189 } // namespace nw::io
190 } // namespace nw
191 
192