diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/file_util.cpp | 61 | ||||
| -rw-r--r-- | src/common/file_util.h | 9 |
2 files changed, 13 insertions, 57 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 18fbfa25b..67fcef3e4 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <array> | 5 | #include <array> |
| 6 | #include <filesystem> | ||
| 6 | #include <limits> | 7 | #include <limits> |
| 7 | #include <memory> | 8 | #include <memory> |
| 8 | #include <sstream> | 9 | #include <sstream> |
| @@ -75,62 +76,16 @@ | |||
| 75 | // The code still needs a ton of cleanup. | 76 | // The code still needs a ton of cleanup. |
| 76 | // REMEMBER: strdup considered harmful! | 77 | // REMEMBER: strdup considered harmful! |
| 77 | namespace Common::FS { | 78 | namespace Common::FS { |
| 79 | namespace fs = std::filesystem; | ||
| 78 | 80 | ||
| 79 | // Remove any ending forward slashes from directory paths | 81 | bool Exists(const fs::path& path) { |
| 80 | // Modifies argument. | 82 | std::error_code ec; |
| 81 | static void StripTailDirSlashes(std::string& fname) { | 83 | return fs::exists(path, ec); |
| 82 | if (fname.length() <= 1) { | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | std::size_t i = fname.length(); | ||
| 87 | while (i > 0 && fname[i - 1] == DIR_SEP_CHR) { | ||
| 88 | --i; | ||
| 89 | } | ||
| 90 | fname.resize(i); | ||
| 91 | } | 84 | } |
| 92 | 85 | ||
| 93 | bool Exists(const std::string& filename) { | 86 | bool IsDirectory(const fs::path& path) { |
| 94 | struct stat file_info; | 87 | std::error_code ec; |
| 95 | 88 | return fs::is_directory(path, ec); | |
| 96 | std::string copy(filename); | ||
| 97 | StripTailDirSlashes(copy); | ||
| 98 | |||
| 99 | #ifdef _WIN32 | ||
| 100 | // Windows needs a slash to identify a driver root | ||
| 101 | if (copy.size() != 0 && copy.back() == ':') | ||
| 102 | copy += DIR_SEP_CHR; | ||
| 103 | |||
| 104 | int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); | ||
| 105 | #else | ||
| 106 | int result = stat(copy.c_str(), &file_info); | ||
| 107 | #endif | ||
| 108 | |||
| 109 | return (result == 0); | ||
| 110 | } | ||
| 111 | |||
| 112 | bool IsDirectory(const std::string& filename) { | ||
| 113 | struct stat file_info; | ||
| 114 | |||
| 115 | std::string copy(filename); | ||
| 116 | StripTailDirSlashes(copy); | ||
| 117 | |||
| 118 | #ifdef _WIN32 | ||
| 119 | // Windows needs a slash to identify a driver root | ||
| 120 | if (copy.size() != 0 && copy.back() == ':') | ||
| 121 | copy += DIR_SEP_CHR; | ||
| 122 | |||
| 123 | int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); | ||
| 124 | #else | ||
| 125 | int result = stat(copy.c_str(), &file_info); | ||
| 126 | #endif | ||
| 127 | |||
| 128 | if (result < 0) { | ||
| 129 | LOG_DEBUG(Common_Filesystem, "stat failed on {}: {}", filename, GetLastErrorMsg()); | ||
| 130 | return false; | ||
| 131 | } | ||
| 132 | |||
| 133 | return S_ISDIR(file_info.st_mode); | ||
| 134 | } | 89 | } |
| 135 | 90 | ||
| 136 | bool Delete(const std::string& filename) { | 91 | bool Delete(const std::string& filename) { |
diff --git a/src/common/file_util.h b/src/common/file_util.h index 840cde2a6..be0906434 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <cstdio> | 8 | #include <cstdio> |
| 9 | #include <filesystem> | ||
| 9 | #include <fstream> | 10 | #include <fstream> |
| 10 | #include <functional> | 11 | #include <functional> |
| 11 | #include <limits> | 12 | #include <limits> |
| @@ -47,11 +48,11 @@ struct FSTEntry { | |||
| 47 | std::vector<FSTEntry> children; | 48 | std::vector<FSTEntry> children; |
| 48 | }; | 49 | }; |
| 49 | 50 | ||
| 50 | // Returns true if file filename exists | 51 | // Returns true if the exists |
| 51 | [[nodiscard]] bool Exists(const std::string& filename); | 52 | [[nodiscard]] bool Exists(const std::filesystem::path& path); |
| 52 | 53 | ||
| 53 | // Returns true if filename is a directory | 54 | // Returns true if path is a directory |
| 54 | [[nodiscard]] bool IsDirectory(const std::string& filename); | 55 | [[nodiscard]] bool IsDirectory(const std::filesystem::path& path); |
| 55 | 56 | ||
| 56 | // Returns the size of filename (64bit) | 57 | // Returns the size of filename (64bit) |
| 57 | [[nodiscard]] u64 GetSize(const std::string& filename); | 58 | [[nodiscard]] u64 GetSize(const std::string& filename); |