summaryrefslogtreecommitdiff
path: root/src/common/file_util.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/file_util.h')
-rw-r--r--src/common/file_util.h86
1 files changed, 47 insertions, 39 deletions
diff --git a/src/common/file_util.h b/src/common/file_util.h
index f7a0c33fa..8b587320f 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -19,7 +19,7 @@
19#include "common/string_util.h" 19#include "common/string_util.h"
20#endif 20#endif
21 21
22namespace FileUtil { 22namespace Common::FS {
23 23
24// User paths for GetUserPath 24// User paths for GetUserPath
25enum class UserPath { 25enum class UserPath {
@@ -32,6 +32,7 @@ enum class UserPath {
32 SDMCDir, 32 SDMCDir,
33 LoadDir, 33 LoadDir,
34 DumpDir, 34 DumpDir,
35 ScreenshotsDir,
35 ShaderDir, 36 ShaderDir,
36 SysDataDir, 37 SysDataDir,
37 UserDir, 38 UserDir,
@@ -47,19 +48,19 @@ struct FSTEntry {
47}; 48};
48 49
49// Returns true if file filename exists 50// Returns true if file filename exists
50bool Exists(const std::string& filename); 51[[nodiscard]] bool Exists(const std::string& filename);
51 52
52// Returns true if filename is a directory 53// Returns true if filename is a directory
53bool IsDirectory(const std::string& filename); 54[[nodiscard]] bool IsDirectory(const std::string& filename);
54 55
55// Returns the size of filename (64bit) 56// Returns the size of filename (64bit)
56u64 GetSize(const std::string& filename); 57[[nodiscard]] u64 GetSize(const std::string& filename);
57 58
58// Overloaded GetSize, accepts file descriptor 59// Overloaded GetSize, accepts file descriptor
59u64 GetSize(const int fd); 60[[nodiscard]] u64 GetSize(int fd);
60 61
61// Overloaded GetSize, accepts FILE* 62// Overloaded GetSize, accepts FILE*
62u64 GetSize(FILE* f); 63[[nodiscard]] u64 GetSize(FILE* f);
63 64
64// Returns true if successful, or path already exists. 65// Returns true if successful, or path already exists.
65bool CreateDir(const std::string& filename); 66bool CreateDir(const std::string& filename);
@@ -119,7 +120,7 @@ u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry,
119bool DeleteDirRecursively(const std::string& directory, unsigned int recursion = 256); 120bool DeleteDirRecursively(const std::string& directory, unsigned int recursion = 256);
120 121
121// Returns the current directory 122// Returns the current directory
122std::optional<std::string> GetCurrentDir(); 123[[nodiscard]] std::optional<std::string> GetCurrentDir();
123 124
124// Create directory and copy contents (does not overwrite existing files) 125// Create directory and copy contents (does not overwrite existing files)
125void CopyDir(const std::string& source_path, const std::string& dest_path); 126void CopyDir(const std::string& source_path, const std::string& dest_path);
@@ -131,20 +132,20 @@ bool SetCurrentDir(const std::string& directory);
131// directory. To be used in "multi-user" mode (that is, installed). 132// directory. To be used in "multi-user" mode (that is, installed).
132const std::string& GetUserPath(UserPath path, const std::string& new_path = ""); 133const std::string& GetUserPath(UserPath path, const std::string& new_path = "");
133 134
134std::string GetHactoolConfigurationPath(); 135[[nodiscard]] std::string GetHactoolConfigurationPath();
135 136
136std::string GetNANDRegistrationDir(bool system = false); 137[[nodiscard]] std::string GetNANDRegistrationDir(bool system = false);
137 138
138// Returns the path to where the sys file are 139// Returns the path to where the sys file are
139std::string GetSysDirectory(); 140[[nodiscard]] std::string GetSysDirectory();
140 141
141#ifdef __APPLE__ 142#ifdef __APPLE__
142std::string GetBundleDirectory(); 143[[nodiscard]] std::string GetBundleDirectory();
143#endif 144#endif
144 145
145#ifdef _WIN32 146#ifdef _WIN32
146const std::string& GetExeDirectory(); 147[[nodiscard]] const std::string& GetExeDirectory();
147std::string AppDataRoamingDirectory(); 148[[nodiscard]] std::string AppDataRoamingDirectory();
148#endif 149#endif
149 150
150std::size_t WriteStringToFile(bool text_file, const std::string& filename, std::string_view str); 151std::size_t WriteStringToFile(bool text_file, const std::string& filename, std::string_view str);
@@ -163,38 +164,55 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam
163 164
164// Splits the path on '/' or '\' and put the components into a vector 165// Splits the path on '/' or '\' and put the components into a vector
165// i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } 166// i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" }
166std::vector<std::string> SplitPathComponents(std::string_view filename); 167[[nodiscard]] std::vector<std::string> SplitPathComponents(std::string_view filename);
167 168
168// Gets all of the text up to the last '/' or '\' in the path. 169// Gets all of the text up to the last '/' or '\' in the path.
169std::string_view GetParentPath(std::string_view path); 170[[nodiscard]] std::string_view GetParentPath(std::string_view path);
170 171
171// Gets all of the text after the first '/' or '\' in the path. 172// Gets all of the text after the first '/' or '\' in the path.
172std::string_view GetPathWithoutTop(std::string_view path); 173[[nodiscard]] std::string_view GetPathWithoutTop(std::string_view path);
173 174
174// Gets the filename of the path 175// Gets the filename of the path
175std::string_view GetFilename(std::string_view path); 176[[nodiscard]] std::string_view GetFilename(std::string_view path);
176 177
177// Gets the extension of the filename 178// Gets the extension of the filename
178std::string_view GetExtensionFromFilename(std::string_view name); 179[[nodiscard]] std::string_view GetExtensionFromFilename(std::string_view name);
179 180
180// Removes the final '/' or '\' if one exists 181// Removes the final '/' or '\' if one exists
181std::string_view RemoveTrailingSlash(std::string_view path); 182[[nodiscard]] std::string_view RemoveTrailingSlash(std::string_view path);
182 183
183// Creates a new vector containing indices [first, last) from the original. 184// Creates a new vector containing indices [first, last) from the original.
184template <typename T> 185template <typename T>
185std::vector<T> SliceVector(const std::vector<T>& vector, std::size_t first, std::size_t last) { 186[[nodiscard]] std::vector<T> SliceVector(const std::vector<T>& vector, std::size_t first,
186 if (first >= last) 187 std::size_t last) {
188 if (first >= last) {
187 return {}; 189 return {};
190 }
188 last = std::min<std::size_t>(last, vector.size()); 191 last = std::min<std::size_t>(last, vector.size());
189 return std::vector<T>(vector.begin() + first, vector.begin() + first + last); 192 return std::vector<T>(vector.begin() + first, vector.begin() + first + last);
190} 193}
191 194
192enum class DirectorySeparator { ForwardSlash, BackwardSlash, PlatformDefault }; 195enum class DirectorySeparator {
196 ForwardSlash,
197 BackwardSlash,
198 PlatformDefault,
199};
193 200
194// Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\' 201// Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\'
195// depending if directory_separator is BackwardSlash or PlatformDefault and running on windows 202// depending if directory_separator is BackwardSlash or PlatformDefault and running on windows
196std::string SanitizePath(std::string_view path, 203[[nodiscard]] std::string SanitizePath(
197 DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash); 204 std::string_view path,
205 DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash);
206
207// To deal with Windows being dumb at Unicode
208template <typename T>
209void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode) {
210#ifdef _MSC_VER
211 fstream.open(Common::UTF8ToUTF16W(filename), openmode);
212#else
213 fstream.open(filename, openmode);
214#endif
215}
198 216
199// simple wrapper for cstdlib file functions to 217// simple wrapper for cstdlib file functions to
200// hopefully will make error checking easier 218// hopefully will make error checking easier
@@ -214,7 +232,7 @@ public:
214 232
215 void Swap(IOFile& other) noexcept; 233 void Swap(IOFile& other) noexcept;
216 234
217 bool Open(const std::string& filename, const char openmode[], int flags = 0); 235 [[nodiscard]] bool Open(const std::string& filename, const char openmode[], int flags = 0);
218 bool Close(); 236 bool Close();
219 237
220 template <typename T> 238 template <typename T>
@@ -255,13 +273,13 @@ public:
255 return WriteArray(str.data(), str.length()); 273 return WriteArray(str.data(), str.length());
256 } 274 }
257 275
258 bool IsOpen() const { 276 [[nodiscard]] bool IsOpen() const {
259 return nullptr != m_file; 277 return nullptr != m_file;
260 } 278 }
261 279
262 bool Seek(s64 off, int origin) const; 280 bool Seek(s64 off, int origin) const;
263 u64 Tell() const; 281 [[nodiscard]] u64 Tell() const;
264 u64 GetSize() const; 282 [[nodiscard]] u64 GetSize() const;
265 bool Resize(u64 size); 283 bool Resize(u64 size);
266 bool Flush(); 284 bool Flush();
267 285
@@ -277,14 +295,4 @@ private:
277 std::FILE* m_file = nullptr; 295 std::FILE* m_file = nullptr;
278}; 296};
279 297
280} // namespace FileUtil 298} // namespace Common::FS
281
282// To deal with Windows being dumb at unicode:
283template <typename T>
284void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode) {
285#ifdef _MSC_VER
286 fstream.open(Common::UTF8ToUTF16W(filename), openmode);
287#else
288 fstream.open(filename, openmode);
289#endif
290}