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