summaryrefslogtreecommitdiff
path: root/src/common/file_util.h
diff options
context:
space:
mode:
authorGravatar Emmanuel Gil Peyrot2016-09-18 09:38:01 +0900
committerGravatar Emmanuel Gil Peyrot2016-09-18 09:38:01 +0900
commitdc8479928c5aee4c6ad6fe4f59006fb604cee701 (patch)
tree569a7f13128450bbab973236615587ff00bced5f /src/common/file_util.h
parentTravis: Import Dolphin’s clang-format hook. (diff)
downloadyuzu-dc8479928c5aee4c6ad6fe4f59006fb604cee701.tar.gz
yuzu-dc8479928c5aee4c6ad6fe4f59006fb604cee701.tar.xz
yuzu-dc8479928c5aee4c6ad6fe4f59006fb604cee701.zip
Sources: Run clang-format on everything.
Diffstat (limited to 'src/common/file_util.h')
-rw-r--r--src/common/file_util.h118
1 files changed, 63 insertions, 55 deletions
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 7ad7ee829..b15021a63 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -5,9 +5,9 @@
5#pragma once 5#pragma once
6 6
7#include <array> 7#include <array>
8#include <cstdio>
8#include <fstream> 9#include <fstream>
9#include <functional> 10#include <functional>
10#include <cstdio>
11#include <string> 11#include <string>
12#include <type_traits> 12#include <type_traits>
13#include <vector> 13#include <vector>
@@ -51,75 +51,75 @@ enum {
51 NUM_PATH_INDICES 51 NUM_PATH_INDICES
52}; 52};
53 53
54namespace FileUtil 54namespace FileUtil {
55{
56 55
57// FileSystem tree node/ 56// FileSystem tree node/
58struct FSTEntry 57struct FSTEntry {
59{
60 bool isDirectory; 58 bool isDirectory;
61 u64 size; // file length or number of entries from children 59 u64 size; // file length or number of entries from children
62 std::string physicalName; // name on disk 60 std::string physicalName; // name on disk
63 std::string virtualName; // name in FST names table 61 std::string virtualName; // name in FST names table
64 std::vector<FSTEntry> children; 62 std::vector<FSTEntry> children;
65}; 63};
66 64
67// Returns true if file filename exists 65// Returns true if file filename exists
68bool Exists(const std::string &filename); 66bool Exists(const std::string& filename);
69 67
70// Returns true if filename is a directory 68// Returns true if filename is a directory
71bool IsDirectory(const std::string &filename); 69bool IsDirectory(const std::string& filename);
72 70
73// Returns the size of filename (64bit) 71// Returns the size of filename (64bit)
74u64 GetSize(const std::string &filename); 72u64 GetSize(const std::string& filename);
75 73
76// Overloaded GetSize, accepts file descriptor 74// Overloaded GetSize, accepts file descriptor
77u64 GetSize(const int fd); 75u64 GetSize(const int fd);
78 76
79// Overloaded GetSize, accepts FILE* 77// Overloaded GetSize, accepts FILE*
80u64 GetSize(FILE *f); 78u64 GetSize(FILE* f);
81 79
82// Returns true if successful, or path already exists. 80// Returns true if successful, or path already exists.
83bool CreateDir(const std::string &filename); 81bool CreateDir(const std::string& filename);
84 82
85// Creates the full path of fullPath returns true on success 83// Creates the full path of fullPath returns true on success
86bool CreateFullPath(const std::string &fullPath); 84bool CreateFullPath(const std::string& fullPath);
87 85
88// Deletes a given filename, return true on success 86// Deletes a given filename, return true on success
89// Doesn't supports deleting a directory 87// Doesn't supports deleting a directory
90bool Delete(const std::string &filename); 88bool Delete(const std::string& filename);
91 89
92// Deletes a directory filename, returns true on success 90// Deletes a directory filename, returns true on success
93bool DeleteDir(const std::string &filename); 91bool DeleteDir(const std::string& filename);
94 92
95// renames file srcFilename to destFilename, returns true on success 93// renames file srcFilename to destFilename, returns true on success
96bool Rename(const std::string &srcFilename, const std::string &destFilename); 94bool Rename(const std::string& srcFilename, const std::string& destFilename);
97 95
98// copies file srcFilename to destFilename, returns true on success 96// copies file srcFilename to destFilename, returns true on success
99bool Copy(const std::string &srcFilename, const std::string &destFilename); 97bool Copy(const std::string& srcFilename, const std::string& destFilename);
100 98
101// creates an empty file filename, returns true on success 99// creates an empty file filename, returns true on success
102bool CreateEmptyFile(const std::string &filename); 100bool CreateEmptyFile(const std::string& filename);
103 101
104/** 102/**
105 * @param num_entries_out to be assigned by the callable with the number of iterated directory entries, never null 103 * @param num_entries_out to be assigned by the callable with the number of iterated directory
104 * entries, never null
106 * @param directory the path to the enclosing directory 105 * @param directory the path to the enclosing directory
107 * @param virtual_name the entry name, without any preceding directory info 106 * @param virtual_name the entry name, without any preceding directory info
108 * @return whether handling the entry succeeded 107 * @return whether handling the entry succeeded
109 */ 108 */
110using DirectoryEntryCallable = std::function<bool(unsigned* num_entries_out, 109using DirectoryEntryCallable = std::function<bool(
111 const std::string& directory, 110 unsigned* num_entries_out, const std::string& directory, const std::string& virtual_name)>;
112 const std::string& virtual_name)>;
113 111
114/** 112/**
115 * Scans a directory, calling the callback for each file/directory contained within. 113 * Scans a directory, calling the callback for each file/directory contained within.
116 * If the callback returns failure, scanning halts and this function returns failure as well 114 * If the callback returns failure, scanning halts and this function returns failure as well
117 * @param num_entries_out assigned by the function with the number of iterated directory entries, can be null 115 * @param num_entries_out assigned by the function with the number of iterated directory entries,
116 * can be null
118 * @param directory the directory to scan 117 * @param directory the directory to scan
119 * @param callback The callback which will be called for each entry 118 * @param callback The callback which will be called for each entry
120 * @return whether scanning the directory succeeded 119 * @return whether scanning the directory succeeded
121 */ 120 */
122bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directory, DirectoryEntryCallable callback); 121bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string& directory,
122 DirectoryEntryCallable callback);
123 123
124/** 124/**
125 * Scans the directory tree, storing the results. 125 * Scans the directory tree, storing the results.
@@ -128,23 +128,24 @@ bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directo
128 * @param recursion Number of children directories to read before giving up. 128 * @param recursion Number of children directories to read before giving up.
129 * @return the total number of files/directories found 129 * @return the total number of files/directories found
130 */ 130 */
131unsigned ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry, unsigned int recursion = 0); 131unsigned ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry,
132 unsigned int recursion = 0);
132 133
133// deletes the given directory and anything under it. Returns true on success. 134// deletes the given directory and anything under it. Returns true on success.
134bool DeleteDirRecursively(const std::string &directory, unsigned int recursion = 256); 135bool DeleteDirRecursively(const std::string& directory, unsigned int recursion = 256);
135 136
136// Returns the current directory 137// Returns the current directory
137std::string GetCurrentDir(); 138std::string GetCurrentDir();
138 139
139// Create directory and copy contents (does not overwrite existing files) 140// Create directory and copy contents (does not overwrite existing files)
140void CopyDir(const std::string &source_path, const std::string &dest_path); 141void CopyDir(const std::string& source_path, const std::string& dest_path);
141 142
142// Set the current directory to given directory 143// Set the current directory to given directory
143bool SetCurrentDir(const std::string &directory); 144bool SetCurrentDir(const std::string& directory);
144 145
145// Returns a pointer to a string with a Citra data dir in the user's home 146// Returns a pointer to a string with a Citra data dir in the user's home
146// directory. To be used in "multi-user" mode (that is, installed). 147// directory. To be used in "multi-user" mode (that is, installed).
147const std::string& GetUserPath(const unsigned int DirIDX, const std::string &newPath=""); 148const std::string& GetUserPath(const unsigned int DirIDX, const std::string& newPath = "");
148 149
149// Returns the path to where the sys file are 150// Returns the path to where the sys file are
150std::string GetSysDirectory(); 151std::string GetSysDirectory();
@@ -154,11 +155,11 @@ std::string GetBundleDirectory();
154#endif 155#endif
155 156
156#ifdef _WIN32 157#ifdef _WIN32
157std::string &GetExeDirectory(); 158std::string& GetExeDirectory();
158#endif 159#endif
159 160
160size_t WriteStringToFile(bool text_file, const std::string &str, const char *filename); 161size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename);
161size_t ReadFileToString(bool text_file, const char *filename, std::string &str); 162size_t ReadFileToString(bool text_file, const char* filename, std::string& str);
162 163
163/** 164/**
164 * Splits the filename into 8.3 format 165 * Splits the filename into 8.3 format
@@ -173,8 +174,7 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam
173// simple wrapper for cstdlib file functions to 174// simple wrapper for cstdlib file functions to
174// hopefully will make error checking easier 175// hopefully will make error checking easier
175// and make forgetting an fclose() harder 176// and make forgetting an fclose() harder
176class IOFile : public NonCopyable 177class IOFile : public NonCopyable {
177{
178public: 178public:
179 IOFile(); 179 IOFile();
180 IOFile(const std::string& filename, const char openmode[]); 180 IOFile(const std::string& filename, const char openmode[]);
@@ -190,11 +190,12 @@ public:
190 bool Close(); 190 bool Close();
191 191
192 template <typename T> 192 template <typename T>
193 size_t ReadArray(T* data, size_t length) 193 size_t ReadArray(T* data, size_t length) {
194 { 194 static_assert(std::is_standard_layout<T>(),
195 static_assert(std::is_standard_layout<T>(), "Given array does not consist of standard layout objects"); 195 "Given array does not consist of standard layout objects");
196#if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER) 196#if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER)
197 static_assert(std::is_trivially_copyable<T>(), "Given array does not consist of trivially copyable objects"); 197 static_assert(std::is_trivially_copyable<T>(),
198 "Given array does not consist of trivially copyable objects");
198#endif 199#endif
199 200
200 if (!IsOpen()) { 201 if (!IsOpen()) {
@@ -210,11 +211,12 @@ public:
210 } 211 }
211 212
212 template <typename T> 213 template <typename T>
213 size_t WriteArray(const T* data, size_t length) 214 size_t WriteArray(const T* data, size_t length) {
214 { 215 static_assert(std::is_standard_layout<T>(),
215 static_assert(std::is_standard_layout<T>(), "Given array does not consist of standard layout objects"); 216 "Given array does not consist of standard layout objects");
216#if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER) 217#if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER)
217 static_assert(std::is_trivially_copyable<T>(), "Given array does not consist of trivially copyable objects"); 218 static_assert(std::is_trivially_copyable<T>(),
219 "Given array does not consist of trivially copyable objects");
218#endif 220#endif
219 221
220 if (!IsOpen()) { 222 if (!IsOpen()) {
@@ -229,27 +231,31 @@ public:
229 return items_written; 231 return items_written;
230 } 232 }
231 233
232 size_t ReadBytes(void* data, size_t length) 234 size_t ReadBytes(void* data, size_t length) {
233 {
234 return ReadArray(reinterpret_cast<char*>(data), length); 235 return ReadArray(reinterpret_cast<char*>(data), length);
235 } 236 }
236 237
237 size_t WriteBytes(const void* data, size_t length) 238 size_t WriteBytes(const void* data, size_t length) {
238 {
239 return WriteArray(reinterpret_cast<const char*>(data), length); 239 return WriteArray(reinterpret_cast<const char*>(data), length);
240 } 240 }
241 241
242 template<typename T> 242 template <typename T>
243 size_t WriteObject(const T& object) { 243 size_t WriteObject(const T& object) {
244 static_assert(!std::is_pointer<T>::value, "Given object is a pointer"); 244 static_assert(!std::is_pointer<T>::value, "Given object is a pointer");
245 return WriteArray(&object, 1); 245 return WriteArray(&object, 1);
246 } 246 }
247 247
248 bool IsOpen() const { return nullptr != m_file; } 248 bool IsOpen() const {
249 return nullptr != m_file;
250 }
249 251
250 // m_good is set to false when a read, write or other function fails 252 // m_good is set to false when a read, write or other function fails
251 bool IsGood() const { return m_good; } 253 bool IsGood() const {
252 explicit operator bool() const { return IsGood(); } 254 return m_good;
255 }
256 explicit operator bool() const {
257 return IsGood();
258 }
253 259
254 bool Seek(s64 off, int origin); 260 bool Seek(s64 off, int origin);
255 u64 Tell() const; 261 u64 Tell() const;
@@ -258,19 +264,21 @@ public:
258 bool Flush(); 264 bool Flush();
259 265
260 // clear error state 266 // clear error state
261 void Clear() { m_good = true; std::clearerr(m_file); } 267 void Clear() {
268 m_good = true;
269 std::clearerr(m_file);
270 }
262 271
263private: 272private:
264 std::FILE* m_file = nullptr; 273 std::FILE* m_file = nullptr;
265 bool m_good = true; 274 bool m_good = true;
266}; 275};
267 276
268} // namespace 277} // namespace
269 278
270// To deal with Windows being dumb at unicode: 279// To deal with Windows being dumb at unicode:
271template <typename T> 280template <typename T>
272void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode) 281void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode) {
273{
274#ifdef _MSC_VER 282#ifdef _MSC_VER
275 fstream.open(Common::UTF8ToTStr(filename).c_str(), openmode); 283 fstream.open(Common::UTF8ToTStr(filename).c_str(), openmode);
276#else 284#else