1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     fslow_IArchive.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: 29386 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_FSLOW_FSLOW_IARCHIVE_H_
17 #define NN_FSLOW_FSLOW_IARCHIVE_H_
18 
19 #include <nn/Result.h>
20 #include <nn/fs/fs_Result.h>
21 #include <nn/util/util_Result.h>
22 #include <nn/fs/fs_Parameters.h>
23 #include <nn/util/util_NonCopyable.h>
24 #include <nn/fslow/fslow_Common.h>
25 
26 #include <cstring>
27 #include <cstdlib>
28 
29 namespace nn { namespace fslow {
30 
31 // アーカイブの基底クラス
32 template <class TArchiveTypeInfo>
33 class IArchive : private nn::util::NonCopyable<IArchive<TArchiveTypeInfo> >
34 {
35 public:
36     typedef TArchiveTypeInfo ArchiveTypeInfo;
37     typedef typename ArchiveTypeInfo::File File;
38     typedef typename ArchiveTypeInfo::Directory Directory;
39     typedef typename ArchiveTypeInfo::Path Path;
40 
41     virtual Result OpenFile(File* pOut, nn::fs::Transaction transaction, Path path, bit32 mode, nn::fs::Attributes attributes) = 0;
OpenFile(File * pOut,nn::fs::Transaction transaction,Path path,nn::fs::PathMark pathMark,bit32 mode,nn::fs::Attributes attributes)42     virtual Result OpenFile(File* pOut, nn::fs::Transaction transaction, Path path, nn::fs::PathMark pathMark, bit32 mode, nn::fs::Attributes attributes)
43     {
44         NN_UNUSED_VAR( pOut);
45         NN_UNUSED_VAR( transaction);
46         NN_UNUSED_VAR( path);
47         NN_UNUSED_VAR( pathMark);
48         NN_UNUSED_VAR( mode);
49         NN_UNUSED_VAR( attributes);
50         return nn::fs::ResultUnsupportedOperation();
51     }
52 
53     virtual Result OpenDirectory(Directory* pOut, Path  path) = 0;
54 
55     virtual Result DeleteFile(nn::fs::Transaction transaction, Path path) = 0;
56     virtual Result DeleteDirectory(nn::fs::Transaction transaction, Path path) = 0;
DeleteDirectoryRecursively(nn::fs::Transaction,Path)57     virtual Result DeleteDirectoryRecursively(nn::fs::Transaction, Path) { return nn::fs::ResultUnsupportedOperation(); }
CreateFile(nn::fs::Transaction transaction,Path path,nn::fs::Attributes attributes,s64 size)58     virtual Result CreateFile(nn::fs::Transaction transaction, Path path, nn::fs::Attributes attributes, s64 size)
59     {
60         File p;
61         NN_UTIL_RETURN_IF_FAILED(OpenFile(&p, transaction, path, nn::fs::OPEN_MODE_CREATE | nn::fs::OPEN_MODE_WRITE, attributes));
62         Result result = p->SetSize(size);
63         p->Close();
64         FreeFileObject(p);
65         return result;
66     }// TORIAEZU
67     virtual Result CreateDirectory(nn::fs::Transaction transaction, Path path, nn::fs::Attributes attributes) = 0;
68 
69     virtual Result RenameFile(nn::fs::Transaction transaction, Path oldPath, Path newPath) = 0;
70     virtual Result RenameDirectory(nn::fs::Transaction transaction, Path oldPath, Path newPath) = 0;
71 
GetPathMark(nn::fs::PathMark * pathMark,Path path)72     virtual Result GetPathMark( nn::fs::PathMark* pathMark, Path path)
73     {
74         NN_UNUSED_VAR( pathMark);
75         NN_UNUSED_VAR( path);
76         return nn::fs::ResultUnsupportedOperation();
77     }
78 
HasFile(bool * pOut,Path path)79     virtual Result HasFile(bool* pOut, Path path)
80     {
81         File p;
82         Result result = OpenFile(&p, nn::fs::Transaction(), path, nn::fs::OPEN_MODE_READ, nn::fs::Attributes());
83         if (result.IsFailure())
84         {
85             if (result <= nn::fs::ResultNotFound())
86             {
87                 *pOut = false;
88                 return ResultSuccess();
89             }
90             else
91             {
92                 return result;
93             }
94         }
95         p->Close();
96         FreeFileObject(p);
97         *pOut = true;
98         return ResultSuccess();
99     }// TORIAEZU
HasDirectory(bool * pOut,Path path)100     virtual Result HasDirectory(bool* pOut, Path path)
101     {
102         Directory p;
103         Result result = OpenDirectory(&p, path);
104         if (result.IsFailure())
105         {
106             if (result <= nn::fs::ResultNotFound())
107             {
108                 *pOut = false;
109                 return ResultSuccess();
110             }
111             else
112             {
113                 return result;
114             }
115         }
116         p->Close();
117         FreeDirectoryObject(p);
118         *pOut = true;
119         return ResultSuccess();
120     }// TORIAEZU
121 
122     virtual void FreeFileObject(File)  = 0;
123     virtual void FreeDirectoryObject(Directory) = 0;
124 
125 protected:
~IArchive()126     virtual ~IArchive() {}
127 };
128 
129 template <class TBase>
130 class ReadOnlyArchiveBase : public TBase
131 {
132 public:
133     typedef typename TBase::ArchiveTypeInfo ArchiveTypeInfo;
134     typedef typename ArchiveTypeInfo::Path Path;
DeleteFile(nn::fs::Transaction,Path)135     virtual Result DeleteFile(nn::fs::Transaction, Path) { return nn::fs::ResultUnsupportedOperation(); }
DeleteDirectory(nn::fs::Transaction,Path)136     virtual Result DeleteDirectory(nn::fs::Transaction, Path) { return nn::fs::ResultUnsupportedOperation(); }
CreateDirectory(nn::fs::Transaction,Path,nn::fs::Attributes)137     virtual Result CreateDirectory(nn::fs::Transaction, Path, nn::fs::Attributes) { return nn::fs::ResultUnsupportedOperation(); }
RenameFile(nn::fs::Transaction,Path,Path)138     virtual Result RenameFile(nn::fs::Transaction, Path, Path) { return nn::fs::ResultUnsupportedOperation(); }
RenameDirectory(nn::fs::Transaction,Path,Path)139     virtual Result RenameDirectory(nn::fs::Transaction, Path, Path) { return nn::fs::ResultUnsupportedOperation(); }
140 };
141 
142 template <class TChar, size_t TMaxLength, TChar TSeparator>
143 class ArchiveName
144 {
145 public:
146 
147     typedef TChar Char;
148     static const size_t MaxLength = TMaxLength;
149     static const TChar Separator = TSeparator;
150 
151 private:
152 
153     static const size_t ByteSize = sizeof(Char) * (MaxLength + 1);
154 
155     union Data
156     {
157         Char c[MaxLength + 1];
158         bit32 w[ByteSize / sizeof(bit32)];
159     };
160 
161     Data m_Data;
162 
Clear()163     void Clear() { std::memset(&m_Data, 0, sizeof(m_Data)); }
164 
165 public:
166 
ArchiveName()167     ArchiveName() { Clear(); }
168 
ArchiveName(const Char * s)169     ArchiveName(const Char* s)
170     {
171         Clear();
172         for (s32 i = 0; i <= MaxLength; ++i)
173         {
174             if (!s[i] || s[i] == Separator)
175             {
176                 return;
177             }
178             m_Data.c[i] = s[i];
179         }
180         Clear();
181     }
182 
GetPathString(const Char * s)183     static const Char* GetPathString(const Char* s)
184     {
185         for (s32 i = 0; i <= MaxLength; ++i)
186         {
187             if (!*s)
188             {
189                 return 0;
190             }
191             if (*s++ == Separator)
192             {
193                 return s;
194             }
195         }
196         return 0;
197     }
198 
199     template <class Path>
GetPathString(Path path)200     static Path GetPathString(Path path)
201     {
202         const typename Path::Char* s = path;
203         for (s32 i = 0; i <= MaxLength; ++i)
204         {
205             if (!*s)
206             {
207                 return Path();
208             }
209             if (*s++ == Separator)
210             {
211                 {
212                     typename Path::Updater u(path);
213                     u->EraseHead(i + 1);
214                 }
215                 return path;
216             }
217         }
218         return Path();
219     }
220 
221     operator const Char*() const { return m_Data.c; }
222 
IsValid()223     bool IsValid() const { return m_Data.c[0] != 0; }
224 
225     bool operator==(const ArchiveName& rhs) const { return std::memcmp(&this->m_Data, &rhs.m_Data, sizeof(Data)) == 0; }
226     bool operator!=(const ArchiveName& rhs) const { return !(*this == rhs); }
227 
228     bool operator>(const ArchiveName& rhs) const { return std::memcmp(&this->m_Data, &rhs.m_Data, sizeof(Data)) > 0; }
229     bool operator<=(const ArchiveName& rhs) const { return !(*this > rhs); }
230     bool operator<(const ArchiveName& rhs) const { return rhs > *this; }
231     bool operator>=(const ArchiveName& rhs) const { return rhs <= *this; }
232 
233 };
234 
235 }}
236 
237 #endif
238