Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/File/FileUtil.h
5659 views
1
// Copyright (C) 2003 Dolphin Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official SVN repository and contact information can be found at
16
// http://code.google.com/p/dolphin-emu/
17
18
#pragma once
19
20
#include <cstdio>
21
#include <string>
22
#include <string_view>
23
#include <time.h>
24
#include <cstdint>
25
26
#include "Common/File/Path.h"
27
28
// Some functions here support Android content URIs. These are marked as such.
29
30
#ifdef _WIN32
31
inline struct tm* localtime_r(const time_t *clock, struct tm *result) {
32
if (localtime_s(result, clock) == 0)
33
return result;
34
return NULL;
35
}
36
#endif
37
38
namespace File {
39
40
// Mostly to handle UTF-8 filenames better on Windows.
41
FILE *OpenCFile(const Path &filename, const char *mode);
42
43
// Reminiscent of PSP's FileAccess enum, due to its use in emulating it.
44
enum OpenFlag {
45
OPEN_NONE = 0,
46
OPEN_READ = 1,
47
OPEN_WRITE = 2,
48
OPEN_APPEND = 4,
49
OPEN_CREATE = 8,
50
OPEN_TRUNCATE = 16,
51
// EXCL?
52
};
53
54
// TODO: This currently only handles Android Content URIs, but might port the rest
55
// of DirectoryFileSystem::Open here eventually for symmetry.
56
int OpenFD(const Path &filename, OpenFlag flags);
57
58
// Cross-platform way to close FDs, corresponsing in platform support with OpenFD above.
59
void CloseFD(int fd);
60
61
// Resolves symlinks and similar.
62
std::string ResolvePath(std::string_view path);
63
64
// Returns true if file filename exists
65
bool Exists(const Path &path);
66
67
// Returns true if file filename exists in directory path.
68
bool ExistsInDir(const Path &path, std::string_view filename);
69
70
// Returns true if filename exists, and is a directory
71
// Supports Android content URIs.
72
bool IsDirectory(const Path &path);
73
74
// Returns struct with modification date of file
75
bool GetModifTime(const Path &filename, tm &return_time);
76
// Same but with unix timestamp
77
bool GetModifTimeT(const Path &filename, time_t *return_time); // the time_t, of course, matches time_now_unix_utc().
78
79
// Returns the size of filename (64bit)
80
uint64_t GetFileSize(const Path &filename);
81
82
// Overloaded GetSize, accepts FILE*
83
uint64_t GetFileSize(FILE *f);
84
85
// Computes the recursive size of a directory. Warning: Might be slow!
86
uint64_t ComputeRecursiveDirectorySize(const Path &path);
87
88
// Returns true if successful, or path already exists.
89
bool CreateDir(const Path &filename);
90
91
void ChangeMTime(const Path &path, time_t mtime);
92
93
// Creates the full path of fullPath returns true on success
94
bool CreateFullPath(const Path &fullPath);
95
96
// Deletes a given file by name, return true on success
97
// Doesn't support deleting a directory (although it will work on some platforms - ideally shouldn't)
98
bool Delete(const Path &filename, bool quiet = false);
99
100
// Deletes a directory by name, returns true on success
101
// Directory must be empty.
102
bool DeleteDir(const Path &filename);
103
104
// Deletes the given directory and anything under it. Returns true on success.
105
bool DeleteDirRecursively(const Path &directory);
106
107
// Renames/moves file srcFilename to destFilename, returns true on success
108
// Will usually only work with in the same partition or other unit of storage,
109
// so you might have to fall back to copy/delete.
110
bool Rename(const Path &srcFilename, const Path &destFilename);
111
112
// copies file srcFilename to destFilename, returns true on success
113
bool Copy(const Path &srcFilename, const Path &destFilename);
114
115
// Tries to rename srcFilename to destFilename, if that fails,
116
// it tries to copy and delete the src if succeeded. If that fails too,
117
// returns false, otherwise returns true.
118
bool Move(const Path &srcFilename, const Path &destFilename);
119
120
// Move file, but only if it can be done quickly (rename or similar).
121
bool MoveIfFast(const Path &srcFilename, const Path &destFilename);
122
123
// creates an empty file filename, returns true on success
124
bool CreateEmptyFile(const Path &filename);
125
126
// Opens ini file (cheats, texture replacements etc.)
127
// TODO: Belongs in System or something.
128
bool OpenFileInEditor(const Path &fileName);
129
130
// Uses some heuristics to determine if this is a folder that we would want to
131
// write to.
132
bool IsProbablyInDownloadsFolder(const Path &folder);
133
134
// TODO: Belongs in System or something.
135
const Path &GetExeDirectory();
136
137
const Path GetCurDirectory();
138
139
// Portable version of fseek() that works with 64-bit offsets if supported by
140
// the operating system.
141
int Fseek(FILE *file, int64_t offset, int whence);
142
143
// Portable version of fseek() that works with 64-bit offsets if supported by
144
// the operating system, and returns the new offset from the start of the file
145
// or -1 if it failed.
146
int64_t Fseektell(FILE *file, int64_t offset, int whence);
147
148
// Portable version of ftell() that works with 64-bit offsets if supported by
149
// the operating system.
150
int64_t Ftell(FILE *file);
151
152
// simple wrapper for cstdlib file functions to
153
// hopefully will make error checking easier
154
// and make forgetting an fclose() harder
155
class IOFile {
156
public:
157
IOFile() {}
158
IOFile(const Path &filename, const char openmode[]);
159
~IOFile();
160
161
// Prevent copies.
162
IOFile(const IOFile &) = delete;
163
void operator=(const IOFile &) = delete;
164
165
bool Open(const Path &filename, const char openmode[]);
166
bool Close();
167
168
template <typename T>
169
bool ReadArray(T* data, size_t length)
170
{
171
if (!IsOpen() || length != fread(data, sizeof(T), length, m_file))
172
m_good = false;
173
174
return m_good;
175
}
176
177
template <typename T>
178
bool WriteArray(const T* data, size_t length)
179
{
180
if (!IsOpen() || length != fwrite(data, sizeof(T), length, m_file))
181
m_good = false;
182
183
return m_good;
184
}
185
186
bool ReadBytes(void* data, size_t length)
187
{
188
return ReadArray(reinterpret_cast<char*>(data), length);
189
}
190
191
bool WriteBytes(const void* data, size_t length)
192
{
193
return WriteArray(reinterpret_cast<const char*>(data), length);
194
}
195
196
bool IsOpen() const { return nullptr != m_file; }
197
198
// m_good is set to false when a read, write or other function fails
199
bool IsGood() const { return m_good; }
200
operator bool() const { return IsGood() && IsOpen(); }
201
202
FILE* ReleaseHandle();
203
204
FILE* GetHandle() { return m_file; }
205
206
void SetHandle(FILE* file);
207
208
bool Seek(int64_t off, int origin);
209
uint64_t Tell();
210
uint64_t GetSize();
211
bool Resize(uint64_t size);
212
bool Flush();
213
214
// clear error state
215
void Clear() {
216
m_good = true;
217
#ifndef HAVE_LIBRETRO_VFS
218
#undef clearerr
219
std::clearerr(m_file);
220
#endif
221
}
222
223
private:
224
FILE *m_file = nullptr;
225
bool m_good = true;
226
};
227
228
#ifdef HAVE_LIBRETRO_VFS
229
// Call this on libretro core initialization with the libretro virtual file
230
// system interface.
231
void InitLibretroVFS(const struct retro_vfs_interface_info *vfs) noexcept;
232
#endif
233
234
// TODO: Refactor, this was moved from the old file_util.cpp.
235
236
// Whole-file reading/writing
237
bool WriteStringToFile(bool textFile, std::string_view str, const Path &filename);
238
bool WriteDataToFile(bool textFile, const void* data, size_t size, const Path &filename);
239
240
bool ReadFileToStringOptions(bool textFile, bool allowShort, const Path &path, std::string *str);
241
242
// Wrappers that clarify the intentions.
243
inline bool ReadBinaryFileToString(const Path &path, std::string *str) {
244
return ReadFileToStringOptions(false, false, path, str);
245
}
246
inline bool ReadSysTextFileToString(const Path &path, std::string *str) {
247
return ReadFileToStringOptions(true, true, path, str);
248
}
249
inline bool ReadTextFileToString(const Path &path, std::string *str) {
250
return ReadFileToStringOptions(true, false, path, str);
251
}
252
253
// Return value must be delete[]-d.
254
uint8_t *ReadLocalFile(const Path &path, size_t *size);
255
256
} // namespace
257
258