diff options
Diffstat (limited to 'src/common/file_util.h')
| -rw-r--r-- | src/common/file_util.h | 85 |
1 files changed, 46 insertions, 39 deletions
diff --git a/src/common/file_util.h b/src/common/file_util.h index 187b93161..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 | ||
| 22 | namespace FileUtil { | 22 | namespace Common::FS { |
| 23 | 23 | ||
| 24 | // User paths for GetUserPath | 24 | // User paths for GetUserPath |
| 25 | enum class UserPath { | 25 | enum class UserPath { |
| @@ -48,19 +48,19 @@ struct FSTEntry { | |||
| 48 | }; | 48 | }; |
| 49 | 49 | ||
| 50 | // Returns true if file filename exists | 50 | // Returns true if file filename exists |
| 51 | bool 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 |
| 54 | bool 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) |
| 57 | u64 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 |
| 60 | u64 GetSize(const int fd); | 60 | [[nodiscard]] u64 GetSize(int fd); |
| 61 | 61 | ||
| 62 | // Overloaded GetSize, accepts FILE* | 62 | // Overloaded GetSize, accepts FILE* |
| 63 | u64 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. |
| 66 | bool CreateDir(const std::string& filename); | 66 | bool CreateDir(const std::string& filename); |
| @@ -120,7 +120,7 @@ u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry, | |||
| 120 | bool DeleteDirRecursively(const std::string& directory, unsigned int recursion = 256); | 120 | bool DeleteDirRecursively(const std::string& directory, unsigned int recursion = 256); |
| 121 | 121 | ||
| 122 | // Returns the current directory | 122 | // Returns the current directory |
| 123 | std::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) |
| 126 | void CopyDir(const std::string& source_path, const std::string& dest_path); | 126 | void 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). |
| 133 | const std::string& GetUserPath(UserPath path, const std::string& new_path = ""); | 133 | const std::string& GetUserPath(UserPath path, const std::string& new_path = ""); |
| 134 | 134 | ||
| 135 | std::string GetHactoolConfigurationPath(); | 135 | [[nodiscard]] std::string GetHactoolConfigurationPath(); |
| 136 | 136 | ||
| 137 | std::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 |
| 140 | std::string GetSysDirectory(); | 140 | [[nodiscard]] std::string GetSysDirectory(); |
| 141 | 141 | ||
| 142 | #ifdef __APPLE__ | 142 | #ifdef __APPLE__ |
| 143 | std::string GetBundleDirectory(); | 143 | [[nodiscard]] std::string GetBundleDirectory(); |
| 144 | #endif | 144 | #endif |
| 145 | 145 | ||
| 146 | #ifdef _WIN32 | 146 | #ifdef _WIN32 |
| 147 | const std::string& GetExeDirectory(); | 147 | [[nodiscard]] const std::string& GetExeDirectory(); |
| 148 | std::string AppDataRoamingDirectory(); | 148 | [[nodiscard]] std::string AppDataRoamingDirectory(); |
| 149 | #endif | 149 | #endif |
| 150 | 150 | ||
| 151 | std::size_t WriteStringToFile(bool text_file, const std::string& filename, std::string_view str); | 151 | std::size_t WriteStringToFile(bool text_file, const std::string& filename, std::string_view str); |
| @@ -164,38 +164,55 @@ 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" } |
| 167 | std::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. |
| 170 | std::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. |
| 173 | std::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 |
| 176 | std::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 |
| 179 | std::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 |
| 182 | std::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. |
| 185 | template <typename T> | 185 | template <typename T> |
| 186 | std::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 | ||
| 193 | enum class DirectorySeparator { ForwardSlash, BackwardSlash, PlatformDefault }; | 195 | enum 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 |
| 197 | std::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); | ||
| 206 | |||
| 207 | // To deal with Windows being dumb at Unicode | ||
| 208 | template <typename T> | ||
| 209 | void 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 | } | ||
| 199 | 216 | ||
| 200 | // simple wrapper for cstdlib file functions to | 217 | // simple wrapper for cstdlib file functions to |
| 201 | // hopefully will make error checking easier | 218 | // hopefully will make error checking easier |
| @@ -215,7 +232,7 @@ public: | |||
| 215 | 232 | ||
| 216 | void Swap(IOFile& other) noexcept; | 233 | void Swap(IOFile& other) noexcept; |
| 217 | 234 | ||
| 218 | 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); |
| 219 | bool Close(); | 236 | bool Close(); |
| 220 | 237 | ||
| 221 | template <typename T> | 238 | template <typename T> |
| @@ -256,13 +273,13 @@ public: | |||
| 256 | return WriteArray(str.data(), str.length()); | 273 | return WriteArray(str.data(), str.length()); |
| 257 | } | 274 | } |
| 258 | 275 | ||
| 259 | bool IsOpen() const { | 276 | [[nodiscard]] bool IsOpen() const { |
| 260 | return nullptr != m_file; | 277 | return nullptr != m_file; |
| 261 | } | 278 | } |
| 262 | 279 | ||
| 263 | bool Seek(s64 off, int origin) const; | 280 | bool Seek(s64 off, int origin) const; |
| 264 | u64 Tell() const; | 281 | [[nodiscard]] u64 Tell() const; |
| 265 | u64 GetSize() const; | 282 | [[nodiscard]] u64 GetSize() const; |
| 266 | bool Resize(u64 size); | 283 | bool Resize(u64 size); |
| 267 | bool Flush(); | 284 | bool Flush(); |
| 268 | 285 | ||
| @@ -278,14 +295,4 @@ private: | |||
| 278 | std::FILE* m_file = nullptr; | 295 | std::FILE* m_file = nullptr; |
| 279 | }; | 296 | }; |
| 280 | 297 | ||
| 281 | } // namespace FileUtil | 298 | } // namespace Common::FS |
| 282 | |||
| 283 | // To deal with Windows being dumb at unicode: | ||
| 284 | template <typename T> | ||
| 285 | void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode) { | ||
| 286 | #ifdef _MSC_VER | ||
| 287 | fstream.open(Common::UTF8ToUTF16W(filename), openmode); | ||
| 288 | #else | ||
| 289 | fstream.open(filename, openmode); | ||
| 290 | #endif | ||
| 291 | } | ||