1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
#pragma once
#include "Runtime/Misc/Allocator.h"
#if UNITY_OSX || UNITY_IPHONE || UNITY_ANDROID || UNITY_LINUX || UNITY_BB10 || UNITY_TIZEN
#include "fcntl.h"
#endif
#include "Runtime/Utilities/NonCopyable.h"
#include "Runtime/Utilities/PathNameUtility.h"
#include <set>
#include "Runtime/Utilities/EnumFlags.h"
#if UNITY_WII
#include <rvlaux/file-io.h>
#endif
#if UNITY_WINRT
// Enable only one
#define ENABLE_CRT_IO 0
#define ENABLE_WIN_IO 1
#endif
class File : public NonCopyable
{
int m_Position;
std::string m_Path;
#if UNITY_WII
bool m_Open;
WiiFileInfo m_File;
#elif UNITY_WINRT
//Microsoft::WRL::ComPtr<IInspectable> m_Stream;
# if ENABLE_CRT_IO
FILE* m_File;
# elif ENABLE_WIN_IO
HANDLE m_FileHandle;
# else
std::vector<unsigned char> m_Data;
# endif
#elif UNITY_WIN || UNITY_XENON
HANDLE m_FileHandle;
#elif UNITY_PS3
int m_FileDescriptor;
#else
FILE* m_File;
#endif
public:
File ();
~File ();
enum Permission { kReadPermission = 0, kWritePermission = 1, kReadWritePermission = 2, kAppendPermission = 3 };
enum AutoBehavior { kNormalBehavior = 0, kSilentReturnOnOpenFail = 1<<0, kRetryOnOpenFail = 1<<1};
bool Open (const std::string& path, Permission perm, AutoBehavior behavior = kNormalBehavior);
bool Close ();
int Read (void* buffer, int size);
int Read (int position, void* buffer, int size);
bool Write (const void* buffer, int size);
bool Write (int pos, const void* buffer, int size);
bool SetFileLength (int size);
int GetFileLength ();
int GetPosition () const { return m_Position; }
static void SetCurrentDirectory (const std::string& path);
static const std::string& GetCurrentDirectory ();
static void CleanupClass();
#if UNITY_OSX || UNITY_IPHONE || UNITY_ANDROID || UNITY_LINUX || UNITY_BB10 || UNITY_TIZEN
enum LockMode {
kNone = LOCK_UN,
kShared = LOCK_SH,
kExclusive = LOCK_EX
};
bool Lock(File::LockMode mode, bool block);
#endif
};
ENUM_FLAGS(File::AutoBehavior);
/// Returns the file length of the file at pathName. If the file doesn't exist returns -1.
int GetFileLength (const std::string& pathName);
size_t GetFileSize (FILE* file);
std::string PathToAbsolutePath (const std::string& path);
// Path is absolute in platform dependent way!
bool IsAbsoluteFilePath( const std::string& path );
/// Reads the contents of the file at pathName into data.
/// byteStart is the first byte read in the file, byteLength is the number of bytes that should be read
bool ReadFromFile (const std::string& pathName, void *data, int fileStart, int byteLength);
bool WriteBytesToFile (const void *data, int byteLength, const std::string& pathName);
#if UNITY_WII
typedef std::basic_string<char, std::char_traits<char>, STL_ALLOCATOR_ALIGNED(kMemIOTemp, char, 32)> InputString;
#else
typedef TEMP_STRING InputString;
#endif
bool ReadStringFromFile (InputString* outData, const std::string& pathName);
bool IsFileCreated (const std::string& path);
bool IsFileOrDirectoryInUse( const std::string& path );
bool CreateDirectory (const std::string& pathName);
/// Returns a set of files and folders that are children of the folder at pathname.
/// Ignores invisible files
bool GetFolderContentsAtPath (const std::string& pathName, std::set<std::string>& paths);
/// Deletes a single file. Returns true if the file could be deleted.
/// Returns false if the file couldnt be deleted or the file was a folder.
bool DeleteFile (const std::string& file);
bool DeleteFileOrDirectory (const std::string& path);
bool SetFileLength (const std::string& path, int size);
#if UNITY_WIN
bool SetFileLength (const HANDLE hFile, const std::string& path, int size);
bool GetFolderContentsAtPathImpl( const std::wstring& pathNameWide, std::set<std::string>& paths, bool deep );
#endif
bool IsDirectoryCreated (const std::string& path);
#if UNITY_EDITOR
bool MoveReplaceFile (const std::string& fromPath, const std::string& toPath);
#if UNITY_OSX || UNITY_LINUX
std::string GetFormattedFileError (int error, const std::string& operation);
#endif
#endif
#if !UNITY_EXTERNAL_TOOL
inline bool CreateDirectoryRecursive (const std::string& pathName)
{
if (IsDirectoryCreated(DeleteLastPathNameComponent(pathName)))
{
if (IsDirectoryCreated (pathName))
return true;
else if (IsFileCreated (pathName))
return false;
else
return CreateDirectory (pathName);
}
std::string::size_type pos = pathName.rfind ("/");
if (pos == std::string::npos)
return false;
std::string superPath;
superPath.assign (pathName.begin (), pathName.begin () + pos);
if (!CreateDirectoryRecursive (superPath))
return false;
return CreateDirectoryRecursive (pathName);
}
#endif
// On Vista, its search indexer can wreak havoc on files that are written then renamed. Full story here:
// http://stackoverflow.com/questions/153257/random-movefileex-failures-on-vista-access-denied-looks-like-caused-by-search-i
// but in short, whenever a file is temporary or should be excluded from search indexing, we have to set those flags.
enum FileFlags {
kFileFlagTemporary = (1<<0), // File is meant to be temporary.
kFileFlagDontIndex = (1<<1), // Exclude file from search indexing (Spotlight, Vista Search, ...)
kFileFlagHidden = (1<<2), // Set file as hidden file.
kAllFileFlags = kFileFlagTemporary | kFileFlagDontIndex
};
#if UNITY_WIN
bool SetFileFlags (const std::string& path, UInt32 attributeMask, UInt32 attributeValue);
bool isHiddenFile (const std::string& path);
#elif UNITY_EDITOR
bool SetFileFlags (const std::string& path, UInt32 attributeMask, UInt32 attributeValue);
bool isHiddenFile (const std::string& path);
#else
inline bool SetFileFlags (const std::string& path, UInt32 attributeMask, UInt32 attributeValue)
{
return true;
}
inline bool isHiddenFile (const std::string& path)
{
return false;
}
#endif
#if UNITY_OSX
void SetupFileLimits ();
#endif
|