1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: fs_FileBase.h
4
5 Copyright (C)2009-2012 Nintendo Co., Ltd. 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 $Rev: 46347 $
14 *---------------------------------------------------------------------------*/
15
16 #ifndef NN_FS_FS_FILEBASE_H_
17 #define NN_FS_FS_FILEBASE_H_
18
19 #include <cstdlib>
20 #include <cstring>
21 #include <cwchar>
22 #include <nn/err.h>
23 #include <nn/fs/fs_Parameters.h>
24 #include <nn/util/util_Int64.h>
25 #include <nn/fs/fs_FileSystemBase.h>
26
27 #ifdef NN_PLATFORM_CTR
28 #include <nn/fs/CTR/fs_FileBase.h>
29 #endif
30
31 namespace nn { namespace fs { namespace detail {
32
33 class FileBase : public FileBaseImpl
34 {
35 protected:
FileBase()36 FileBase() : m_Position(0), m_Size(0) {}
37 FileBase(const wchar_t* pathName, bit32 mode);
38 FileBase(const char* pathName, bit32 mode);
39 void Initialize(const wchar_t* pathName, bit32 mode);
40 void Initialize(const char* pathName, bit32 mode);
41 nn::Result TryInitialize(const wchar_t* pathName, bit32 mode);
42 nn::Result TryInitialize(const char* pathName, bit32 mode);
43
44 nn::Result TryRead(s32* pOut, void* buffer, size_t size);
45 nn::Result TryWrite(s32* pOut, const void* buffer, size_t size, bool flush=true);
46 nn::Result TrySeek(s64 position, PositionBase base);
47 nn::Result TryGetPosition(s64* pOut) const;
48 nn::Result TrySetPosition(s64 position);
49 nn::Result TryGetSize(s64* pOut) const;
50 nn::Result TrySetSize(s64 size);
51 nn::Result TryFlush();
52 s32 Read(void* buffer, size_t size);
53 s32 Write(const void* buffer, size_t size, bool flush=true);
54 void Seek(s64 position, PositionBase base);
55 s64 GetPosition() const;
56 void SetPosition(s64 position);
57 s64 GetSize() const;
58 void SetSize(s64 size);
59 void Flush();
60 void SetPriority(s32 priority);
61 void GetPriority(s32* pOut) const;
62
63 private:
64
65 typedef nn::util::Int64<s64> S64;
66 S64 m_Position;
67 mutable S64 m_Size;
68
69 #ifndef NN_PROCESSOR_ARM946ES
70 public:
OpenDirect(Handle handle)71 void OpenDirect(Handle handle)
72 {
73 this->m_Position = this->m_Size = 0;
74 FileBaseImpl::OpenDirect(handle);
75 }
76 #endif
77
78 };
79
TryInitialize(const wchar_t * pathName,bit32 mode)80 inline nn::Result FileBase::TryInitialize(const wchar_t* pathName, bit32 mode)
81 {
82 this->m_Position = this->m_Size = 0;
83 return TryOpenImpl(pathName, mode);
84 }
85
TryInitialize(const char * pathName,bit32 mode)86 inline nn::Result FileBase::TryInitialize(const char* pathName, bit32 mode)
87 {
88 static const size_t BUF_SIZE = MAX_FILE_PATH_LENGTH + 1;
89 wchar_t buffer[BUF_SIZE];
90 ConvertMbsToWcs(buffer, MAX_FILE_PATH_LENGTH, pathName);
91 return TryInitialize(buffer, mode);
92 }
93
Initialize(const wchar_t * pathName,bit32 mode)94 inline void FileBase::Initialize(const wchar_t* pathName, bit32 mode)
95 {
96 Result result = TryInitialize(pathName, mode);
97 if (result.IsFailure())
98 {
99 NN_SLOG_("file open failed: ");
100 NN_SLOG_("%lls\n", pathName);
101 NN_ERR_THROW_FATAL_ALL(result);
102 }
103 }
104
Initialize(const char * pathName,bit32 mode)105 inline void FileBase::Initialize(const char* pathName, bit32 mode)
106 {
107 Result result = TryInitialize(pathName, mode);
108 if (result.IsFailure())
109 {
110 NN_SLOG_("file open failed: ");
111 NN_SLOG_("%s\n", pathName);
112 NN_ERR_THROW_FATAL_ALL(result);
113 }
114 }
115
FileBase(const wchar_t * pathName,bit32 mode)116 inline FileBase::FileBase(const wchar_t* pathName, bit32 mode)
117 {
118 Initialize(pathName, mode);
119 }
120
FileBase(const char * pathName,bit32 mode)121 inline FileBase::FileBase(const char* pathName, bit32 mode)
122 {
123 Initialize(pathName, mode);
124 }
125
TryGetPosition(s64 * pOut)126 inline nn::Result FileBase::TryGetPosition(s64* pOut) const
127 {
128 *pOut = m_Position;
129 return ResultSuccess();
130 }
131
Read(void * buffer,size_t size)132 inline s32 FileBase::Read(void* buffer, size_t size)
133 {
134 s32 ret;
135 NN_ERR_THROW_FATAL_ALL(TryRead(&ret, buffer, size));
136 return ret;
137 }
138
Write(const void * buffer,size_t size,bool flush)139 inline s32 FileBase::Write(const void* buffer, size_t size, bool flush)
140 {
141 s32 ret;
142 NN_ERR_THROW_FATAL_ALL(TryWrite(&ret, buffer, size, flush));
143 return ret;
144 }
145
Seek(s64 position,PositionBase base)146 inline void FileBase::Seek(s64 position, PositionBase base)
147 {
148 NN_ERR_THROW_FATAL_ALL(TrySeek(position, base));
149 }
150
GetPosition()151 inline s64 FileBase::GetPosition() const
152 {
153 s64 ret;
154 NN_ERR_THROW_FATAL_ALL(TryGetPosition(&ret));
155 return ret;
156 }
157
SetPosition(s64 position)158 inline void FileBase::SetPosition(s64 position)
159 {
160 NN_ERR_THROW_FATAL_ALL(TrySetPosition(position));
161 }
162
GetSize()163 inline s64 FileBase::GetSize() const
164 {
165 s64 ret;
166 NN_ERR_THROW_FATAL_ALL(TryGetSize(&ret));
167 return ret;
168 }
169
SetSize(s64 size)170 inline void FileBase::SetSize(s64 size)
171 {
172 NN_ERR_THROW_FATAL_ALL(TrySetSize(size));
173 }
174
Flush()175 inline void FileBase::Flush()
176 {
177 NN_ERR_THROW_FATAL_ALL(TryFlush());
178 }
179
SetPriority(s32 priority)180 inline void FileBase::SetPriority(s32 priority)
181 {
182 NN_ERR_THROW_FATAL_ALL(TrySetPriority(priority));
183 }
184
GetPriority(s32 * pOut)185 inline void FileBase::GetPriority(s32* pOut) const
186 {
187 NN_ERR_THROW_FATAL_ALL(TryGetPriority(pOut));
188 }
189
190 }}}
191
192 #endif
193