summaryrefslogtreecommitdiff
path: root/src/common/file_util.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/common/file_util.h61
1 files changed, 34 insertions, 27 deletions
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 187b93161..681b28137 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -48,19 +48,19 @@ struct FSTEntry {
48}; 48};
49 49
50// Returns true if file filename exists 50// Returns true if file filename exists
51bool Exists(const std::string& filename); 51[[nodiscard]] bool Exists(const std::string& filename);
52 52
53// Returns true if filename is a directory 53// Returns true if filename is a directory
54bool IsDirectory(const std::string& filename); 54[[nodiscard]] bool IsDirectory(const std::string& filename);
55 55
56// Returns the size of filename (64bit) 56// Returns the size of filename (64bit)
57u64 GetSize(const std::string& filename); 57[[nodiscard]] u64 GetSize(const std::string& filename);
58 58
59// Overloaded GetSize, accepts file descriptor 59// Overloaded GetSize, accepts file descriptor
60u64 GetSize(const int fd); 60[[nodiscard]] u64 GetSize(int fd);
61 61
62// Overloaded GetSize, accepts FILE* 62// Overloaded GetSize, accepts FILE*
63u64 GetSize(FILE* f); 63[[nodiscard]] u64 GetSize(FILE* f);
64 64
65// Returns true if successful, or path already exists. 65// Returns true if successful, or path already exists.
66bool CreateDir(const std::string& filename); 66bool CreateDir(const std::string& filename);
@@ -120,7 +120,7 @@ u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry,
120bool DeleteDirRecursively(const std::string& directory, unsigned int recursion = 256); 120bool DeleteDirRecursively(const std::string& directory, unsigned int recursion = 256);
121 121
122// Returns the current directory 122// Returns the current directory
123std::optional<std::string> GetCurrentDir(); 123[[nodiscard]] std::optional<std::string> GetCurrentDir();
124 124
125// Create directory and copy contents (does not overwrite existing files) 125// Create directory and copy contents (does not overwrite existing files)
126void CopyDir(const std::string& source_path, const std::string& dest_path); 126void CopyDir(const std::string& source_path, const std::string& dest_path);
@@ -132,20 +132,20 @@ bool SetCurrentDir(const std::string& directory);
132// directory. To be used in "multi-user" mode (that is, installed). 132// directory. To be used in "multi-user" mode (that is, installed).
133const std::string& GetUserPath(UserPath path, const std::string& new_path = ""); 133const std::string& GetUserPath(UserPath path, const std::string& new_path = "");
134 134
135std::string GetHactoolConfigurationPath(); 135[[nodiscard]] std::string GetHactoolConfigurationPath();
136 136
137std::string GetNANDRegistrationDir(bool system = false); 137[[nodiscard]] std::string GetNANDRegistrationDir(bool system = false);
138 138
139// Returns the path to where the sys file are 139// Returns the path to where the sys file are
140std::string GetSysDirectory(); 140[[nodiscard]] std::string GetSysDirectory();
141 141
142#ifdef __APPLE__ 142#ifdef __APPLE__
143std::string GetBundleDirectory(); 143[[nodiscard]] std::string GetBundleDirectory();
144#endif 144#endif
145 145
146#ifdef _WIN32 146#ifdef _WIN32
147const std::string& GetExeDirectory(); 147[[nodiscard]] const std::string& GetExeDirectory();
148std::string AppDataRoamingDirectory(); 148[[nodiscard]] std::string AppDataRoamingDirectory();
149#endif 149#endif
150 150
151std::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);
@@ -164,38 +164,45 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam
164 164
165// 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
166// 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" }
167std::vector<std::string> SplitPathComponents(std::string_view filename); 167[[nodiscard]] std::vector<std::string> SplitPathComponents(std::string_view filename);
168 168
169// 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.
170std::string_view GetParentPath(std::string_view path); 170[[nodiscard]] std::string_view GetParentPath(std::string_view path);
171 171
172// 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.
173std::string_view GetPathWithoutTop(std::string_view path); 173[[nodiscard]] std::string_view GetPathWithoutTop(std::string_view path);
174 174
175// Gets the filename of the path 175// Gets the filename of the path
176std::string_view GetFilename(std::string_view path); 176[[nodiscard]] std::string_view GetFilename(std::string_view path);
177 177
178// Gets the extension of the filename 178// Gets the extension of the filename
179std::string_view GetExtensionFromFilename(std::string_view name); 179[[nodiscard]] std::string_view GetExtensionFromFilename(std::string_view name);
180 180
181// Removes the final '/' or '\' if one exists 181// Removes the final '/' or '\' if one exists
182std::string_view RemoveTrailingSlash(std::string_view path); 182[[nodiscard]] std::string_view RemoveTrailingSlash(std::string_view path);
183 183
184// Creates a new vector containing indices [first, last) from the original. 184// Creates a new vector containing indices [first, last) from the original.
185template <typename T> 185template <typename T>
186std::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,
187 if (first >= last) 187 std::size_t last) {
188 if (first >= last) {
188 return {}; 189 return {};
190 }
189 last = std::min<std::size_t>(last, vector.size()); 191 last = std::min<std::size_t>(last, vector.size());
190 return std::vector<T>(vector.begin() + first, vector.begin() + first + last); 192 return std::vector<T>(vector.begin() + first, vector.begin() + first + last);
191} 193}
192 194
193enum class DirectorySeparator { ForwardSlash, BackwardSlash, PlatformDefault }; 195enum class DirectorySeparator {
196 ForwardSlash,
197 BackwardSlash,
198 PlatformDefault,
199};
194 200
195// Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\' 201// Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\'
196// 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
197std::string SanitizePath(std::string_view path, 203[[nodiscard]] std::string SanitizePath(
198 DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash); 204 std::string_view path,
205 DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash);
199 206
200// simple wrapper for cstdlib file functions to 207// simple wrapper for cstdlib file functions to
201// hopefully will make error checking easier 208// hopefully will make error checking easier
@@ -215,7 +222,7 @@ public:
215 222
216 void Swap(IOFile& other) noexcept; 223 void Swap(IOFile& other) noexcept;
217 224
218 bool Open(const std::string& filename, const char openmode[], int flags = 0); 225 [[nodiscard]] bool Open(const std::string& filename, const char openmode[], int flags = 0);
219 bool Close(); 226 bool Close();
220 227
221 template <typename T> 228 template <typename T>
@@ -256,13 +263,13 @@ public:
256 return WriteArray(str.data(), str.length()); 263 return WriteArray(str.data(), str.length());
257 } 264 }
258 265
259 bool IsOpen() const { 266 [[nodiscard]] bool IsOpen() const {
260 return nullptr != m_file; 267 return nullptr != m_file;
261 } 268 }
262 269
263 bool Seek(s64 off, int origin) const; 270 bool Seek(s64 off, int origin) const;
264 u64 Tell() const; 271 [[nodiscard]] u64 Tell() const;
265 u64 GetSize() const; 272 [[nodiscard]] u64 GetSize() const;
266 bool Resize(u64 size); 273 bool Resize(u64 size);
267 bool Flush(); 274 bool Flush();
268 275