1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     io_FileStream.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: 13145 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/io/io_FileStream.h>
19 
20 #include <nw/types.h>
21 #include <nw/assert.h>
22 #include <nw/ut/ut_Inlines.h>
23 #include <nw/ut/ut_RuntimeTypeInfo.h>
24 
25 
26 namespace nw {
27 namespace io   {
28 
29 NW_UT_RUNTIME_TYPEINFO_DEFINITION(FileStream,IOStream);
30 
31 
32 /*!--------------------------------------------------------------------------*
33   Name:        FileStream::Seek
34 
35   @brief       ファイルポインタの移動
36 
37   @param[in]   offset  ファイルポインタ移動値を指定します。
38   @param[in]   origin  ファイルポインタ移動の基準点を指定します。
39                       FILE_STREAM_SEEK_BEGIN      - ファイルの先頭を基準
40                       FILE_STREAM_SEEK_CURRENT    - 現在の読み込み位置を基準
41                       FILE_STREAM_SEEK_END        - ファイルの終端を基準(offset <= 0で指定されるはずです)
42 
43   @return      None.
44  *---------------------------------------------------------------------------*/
45 void
Seek(s32,u32)46 FileStream::Seek( s32 /*offset*/, u32 /*origin*/ )
47 {
48     NW_ASSERTMSG( CanSeek(), "Stream don't support SEEK function\n" );
49 }
50 
51 
52 /*!--------------------------------------------------------------------------*
53   Name:        FileStream::Cancel
54 
55   @brief       非同期処理をキャンセルします。(同期関数)
56 
57   @return      None.
58  *---------------------------------------------------------------------------*/
59 void
Cancel(void)60 FileStream::Cancel( void )
61 {
62     NW_ASSERTMSG( CanCancel(), "Stream don't support CANCEL function\n" );
63 }
64 
65 
66 /*!--------------------------------------------------------------------------*
67   Name:        FileStream::CancelAsync
68 
69   @brief       非同期処理をキャンセルします。(非同期関数)
70 
71   @param[in]   callback
72   @param[in]   arg
73 
74   @return      コマンドが正常に発行された場合には真を返します。
75                発行されなかった場合には偽を返します。
76  *---------------------------------------------------------------------------*/
77 bool
CancelAsync(IOStreamCallback,void *)78 FileStream::CancelAsync(
79         IOStreamCallback /*callback*/, void* /*arg*/ )
80 {
81     NW_ASSERTMSG( CanCancel(), "Stream don't support CANCEL function\n" );
82     NW_ASSERTMSG( CanAsync(),  "Stream don't support ASYNC function\n" );
83     return true;
84 }
85 
86 
87 
88 
89 /*!--------------------------------------------------------------------------*
90   Name:        FileStream::FilePosition::Skip
91 
92   @brief       ファイルポインタの移動。
93                ファイルの終端を越えた場合には、ファイルポインタは終端位置を示します。
94 
95   @param[in]   offset  ファイルポインタ移動オフセット
96 
97   @return      移動後のファイル位置を返します。
98  *---------------------------------------------------------------------------*/
99 u32
Skip(s32 offset)100 FileStream::FilePosition::Skip( s32 offset )
101 {
102     if ( offset != 0 )
103     {
104         s64 position = mPosition + offset;
105 
106         position = ut::Clamp( position, static_cast<s64>(0), static_cast<s64>(mFileSize) );
107 
108         mPosition = static_cast<u32>(position);
109     }
110     return mPosition;
111 }
112 
113 
114 /*!--------------------------------------------------------------------------*
115   Name:        FileStream::FilePosition::Append
116 
117   @brief       データ追加によるファイルポインタの移動。
118                ファイルの終端を越えた場合には、その分ファイルサイズを増加します。
119 
120   @param[in]   offset  ファイルポインタ移動オフセット
121 
122   @return      移動後のファイル位置を返します。
123  *---------------------------------------------------------------------------*/
124 u32
Append(s32 offset)125 FileStream::FilePosition::Append( s32 offset )
126 {
127     s64 position = mPosition + offset;
128 
129     if ( position < 0 )
130     {
131         mPosition = 0;
132     }
133     else
134     {
135         mPosition = static_cast<u32>(position);
136         mFileSize = ut::Max( mPosition, mFileSize );
137     }
138     return mPosition;
139 }
140 
141 
142 /*!--------------------------------------------------------------------------*
143   Name:        FileStream::FilePosition::Seek
144 
145   @brief       ファイルポインタの移動
146 
147   @param[in]   offset  ファイルポインタ移動値を指定します。
148   @param[in]   origin  ファイルポインタ移動の基準点を指定します。
149                        FILE_STREAM_SEEK_BEGIN      - ファイルの先頭を基準
150                        FILE_STREAM_SEEK_CURRENT    - 現在の読み込み位置を基準
151                        FILE_STREAM_SEEK_END        - ファイルの終端を基準(offset <= 0で指定されるはずです)
152 
153   @return      None.
154  *---------------------------------------------------------------------------*/
155 void
Seek(s32 offset,u32 origin)156 FileStream::FilePosition::Seek( s32 offset, u32 origin )
157 {
158     switch ( origin )
159     {
160     case FILE_STREAM_SEEK_BEGIN:
161         mPosition = 0;
162         break;
163     case FILE_STREAM_SEEK_CURRENT:
164         break;
165     case FILE_STREAM_SEEK_END:
166         mPosition = mFileSize;
167         break;
168     }
169     (void)Skip( offset );
170 }
171 
172 
173 
174 
175 }   /* namespace io */
176 }   /* namespace nw */
177 
178