CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/File/FileUtil.h
Views: 1401
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 <fstream>
21
#include <cstdio>
22
#include <string>
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 _MSC_VER
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
// Resolves symlinks and similar.
59
std::string ResolvePath(const std::string &path);
60
61
// Returns true if file filename exists
62
bool Exists(const Path &path);
63
64
// Returns true if file filename exists in directory path.
65
bool ExistsInDir(const Path &path, const std::string &filename);
66
67
// Returns true if filename exists, and is a directory
68
// Supports Android content URIs.
69
bool IsDirectory(const Path &filename);
70
71
// Returns struct with modification date of file
72
bool GetModifTime(const Path &filename, tm &return_time);
73
74
// Returns the size of filename (64bit)
75
uint64_t GetFileSize(const Path &filename);
76
77
// Overloaded GetSize, accepts FILE*
78
uint64_t GetFileSize(FILE *f);
79
80
// Computes the recursive size of a directory. Warning: Might be slow!
81
uint64_t ComputeRecursiveDirectorySize(const Path &path);
82
83
// Returns true if successful, or path already exists.
84
bool CreateDir(const Path &filename);
85
86
void ChangeMTime(const Path &path, time_t mtime);
87
88
// Creates the full path of fullPath returns true on success
89
bool CreateFullPath(const Path &fullPath);
90
91
// Deletes a given file by name, return true on success
92
// Doesn't support deleting a directory (although it will work on some platforms - ideally shouldn't)
93
bool Delete(const Path &filename);
94
95
// Deletes a directory by name, returns true on success
96
// Directory must be empty.
97
bool DeleteDir(const Path &filename);
98
99
// Deletes the given directory and anything under it. Returns true on success.
100
bool DeleteDirRecursively(const Path &directory);
101
102
// Renames/moves file srcFilename to destFilename, returns true on success
103
// Will usually only work with in the same partition or other unit of storage,
104
// so you might have to fall back to copy/delete.
105
bool Rename(const Path &srcFilename, const Path &destFilename);
106
107
// copies file srcFilename to destFilename, returns true on success
108
bool Copy(const Path &srcFilename, const Path &destFilename);
109
110
// Tries to rename srcFilename to destFilename, if that fails,
111
// it tries to copy and delete the src if succeeded. If that fails too,
112
// returns false, otherwise returns true.
113
bool Move(const Path &srcFilename, const Path &destFilename);
114
115
// Move file, but only if it can be done quickly (rename or similar).
116
bool MoveIfFast(const Path &srcFilename, const Path &destFilename);
117
118
// creates an empty file filename, returns true on success
119
bool CreateEmptyFile(const Path &filename);
120
121
// Opens ini file (cheats, texture replacements etc.)
122
// TODO: Belongs in System or something.
123
bool OpenFileInEditor(const Path &fileName);
124
125
// Uses some heuristics to determine if this is a folder that we would want to
126
// write to.
127
bool IsProbablyInDownloadsFolder(const Path &folder);
128
129
// TODO: Belongs in System or something.
130
const Path &GetExeDirectory();
131
132
const Path GetCurDirectory();
133
134
// simple wrapper for cstdlib file functions to
135
// hopefully will make error checking easier
136
// and make forgetting an fclose() harder
137
class IOFile {
138
public:
139
IOFile() {}
140
IOFile(const Path &filename, const char openmode[]);
141
~IOFile();
142
143
// Prevent copies.
144
IOFile(const IOFile &) = delete;
145
void operator=(const IOFile &) = delete;
146
147
bool Open(const Path &filename, const char openmode[]);
148
bool Close();
149
150
template <typename T>
151
bool ReadArray(T* data, size_t length)
152
{
153
if (!IsOpen() || length != std::fread(data, sizeof(T), length, m_file))
154
m_good = false;
155
156
return m_good;
157
}
158
159
template <typename T>
160
bool WriteArray(const T* data, size_t length)
161
{
162
if (!IsOpen() || length != std::fwrite(data, sizeof(T), length, m_file))
163
m_good = false;
164
165
return m_good;
166
}
167
168
bool ReadBytes(void* data, size_t length)
169
{
170
return ReadArray(reinterpret_cast<char*>(data), length);
171
}
172
173
bool WriteBytes(const void* data, size_t length)
174
{
175
return WriteArray(reinterpret_cast<const char*>(data), length);
176
}
177
178
bool IsOpen() const { return nullptr != m_file; }
179
180
// m_good is set to false when a read, write or other function fails
181
bool IsGood() const { return m_good; }
182
operator bool() const { return IsGood() && IsOpen(); }
183
184
std::FILE* ReleaseHandle();
185
186
std::FILE* GetHandle() { return m_file; }
187
188
void SetHandle(std::FILE* file);
189
190
bool Seek(int64_t off, int origin);
191
uint64_t Tell();
192
uint64_t GetSize();
193
bool Resize(uint64_t size);
194
bool Flush();
195
196
// clear error state
197
void Clear() {
198
m_good = true;
199
#undef clearerr
200
std::clearerr(m_file);
201
}
202
203
private:
204
std::FILE *m_file = nullptr;
205
bool m_good = true;
206
};
207
208
// TODO: Refactor, this was moved from the old file_util.cpp.
209
210
// Whole-file reading/writing
211
bool WriteStringToFile(bool textFile, const std::string &str, const Path &filename);
212
bool WriteDataToFile(bool textFile, const void* data, size_t size, const Path &filename);
213
214
bool ReadFileToStringOptions(bool textFile, bool allowShort, const Path &path, std::string *str);
215
216
// Wrappers that clarify the intentions.
217
inline bool ReadBinaryFileToString(const Path &path, std::string *str) {
218
return ReadFileToStringOptions(false, false, path, str);
219
}
220
inline bool ReadSysTextFileToString(const Path &path, std::string *str) {
221
return ReadFileToStringOptions(true, true, path, str);
222
}
223
inline bool ReadTextFileToString(const Path &path, std::string *str) {
224
return ReadFileToStringOptions(true, false, path, str);
225
}
226
227
// Return value must be delete[]-d.
228
uint8_t *ReadLocalFile(const Path &path, size_t *size);
229
230
} // namespace
231
232