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