mirror of
https://git.suyu.dev/suyu/suyu
synced 2024-11-01 04:47:53 +00:00
file_util: Migrate Exists() and IsDirectory() over to std::filesystem
Greatly simplifies our file-handling code for these functions.
This commit is contained in:
parent
aeb100cffe
commit
0e54aa17e6
2 changed files with 13 additions and 57 deletions
|
@ -3,6 +3,7 @@
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <filesystem>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
@ -75,62 +76,16 @@
|
||||||
// The code still needs a ton of cleanup.
|
// The code still needs a ton of cleanup.
|
||||||
// REMEMBER: strdup considered harmful!
|
// REMEMBER: strdup considered harmful!
|
||||||
namespace Common::FS {
|
namespace Common::FS {
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
// Remove any ending forward slashes from directory paths
|
bool Exists(const fs::path& path) {
|
||||||
// Modifies argument.
|
std::error_code ec;
|
||||||
static void StripTailDirSlashes(std::string& fname) {
|
return fs::exists(path, ec);
|
||||||
if (fname.length() <= 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t i = fname.length();
|
|
||||||
while (i > 0 && fname[i - 1] == DIR_SEP_CHR) {
|
|
||||||
--i;
|
|
||||||
}
|
|
||||||
fname.resize(i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Exists(const std::string& filename) {
|
bool IsDirectory(const fs::path& path) {
|
||||||
struct stat file_info;
|
std::error_code ec;
|
||||||
|
return fs::is_directory(path, ec);
|
||||||
std::string copy(filename);
|
|
||||||
StripTailDirSlashes(copy);
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
// Windows needs a slash to identify a driver root
|
|
||||||
if (copy.size() != 0 && copy.back() == ':')
|
|
||||||
copy += DIR_SEP_CHR;
|
|
||||||
|
|
||||||
int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info);
|
|
||||||
#else
|
|
||||||
int result = stat(copy.c_str(), &file_info);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return (result == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsDirectory(const std::string& filename) {
|
|
||||||
struct stat file_info;
|
|
||||||
|
|
||||||
std::string copy(filename);
|
|
||||||
StripTailDirSlashes(copy);
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
// Windows needs a slash to identify a driver root
|
|
||||||
if (copy.size() != 0 && copy.back() == ':')
|
|
||||||
copy += DIR_SEP_CHR;
|
|
||||||
|
|
||||||
int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info);
|
|
||||||
#else
|
|
||||||
int result = stat(copy.c_str(), &file_info);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (result < 0) {
|
|
||||||
LOG_DEBUG(Common_Filesystem, "stat failed on {}: {}", filename, GetLastErrorMsg());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return S_ISDIR(file_info.st_mode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Delete(const std::string& filename) {
|
bool Delete(const std::string& filename) {
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
@ -47,11 +48,11 @@ struct FSTEntry {
|
||||||
std::vector<FSTEntry> children;
|
std::vector<FSTEntry> children;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Returns true if file filename exists
|
// Returns true if the exists
|
||||||
[[nodiscard]] bool Exists(const std::string& filename);
|
[[nodiscard]] bool Exists(const std::filesystem::path& path);
|
||||||
|
|
||||||
// Returns true if filename is a directory
|
// Returns true if path is a directory
|
||||||
[[nodiscard]] bool IsDirectory(const std::string& filename);
|
[[nodiscard]] bool IsDirectory(const std::filesystem::path& path);
|
||||||
|
|
||||||
// Returns the size of filename (64bit)
|
// Returns the size of filename (64bit)
|
||||||
[[nodiscard]] u64 GetSize(const std::string& filename);
|
[[nodiscard]] u64 GetSize(const std::string& filename);
|
||||||
|
|
Loading…
Reference in a new issue