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