mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-02-03 11:32:43 +01:00
get rid of HAS_STD_FILESYSTEM
just use std::filesystem
This commit is contained in:
parent
acafb07707
commit
d51e4e5236
@ -4,17 +4,17 @@
|
|||||||
#include "Common/FileSearch.h"
|
#include "Common/FileSearch.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <filesystem>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <system_error>
|
||||||
|
|
||||||
#include "Common/CommonPaths.h"
|
#include "Common/CommonPaths.h"
|
||||||
|
#include "Common/Logging/Log.h"
|
||||||
#include "Common/StringUtil.h"
|
#include "Common/StringUtil.h"
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include <filesystem>
|
|
||||||
namespace fs = std::filesystem;
|
|
||||||
#define HAS_STD_FILESYSTEM
|
|
||||||
#else
|
#else
|
||||||
#ifdef ANDROID
|
#ifdef ANDROID
|
||||||
#include "jni/AndroidCommon/AndroidCommon.h"
|
#include "jni/AndroidCommon/AndroidCommon.h"
|
||||||
@ -25,52 +25,45 @@ namespace fs = std::filesystem;
|
|||||||
#include "Common/FileUtil.h"
|
#include "Common/FileUtil.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
namespace Common
|
namespace Common
|
||||||
{
|
{
|
||||||
#ifndef HAS_STD_FILESYSTEM
|
|
||||||
|
|
||||||
static void FileSearchWithTest(const std::string& directory, bool recursive,
|
|
||||||
std::vector<std::string>* result_out,
|
|
||||||
std::function<bool(const File::FSTEntry&)> callback)
|
|
||||||
{
|
|
||||||
File::FSTEntry top = File::ScanDirectoryTree(directory, recursive);
|
|
||||||
|
|
||||||
const std::function<void(File::FSTEntry&)> DoEntry = [&](File::FSTEntry& entry) {
|
|
||||||
if (callback(entry))
|
|
||||||
result_out->push_back(entry.physicalName);
|
|
||||||
for (auto& child : entry.children)
|
|
||||||
DoEntry(child);
|
|
||||||
};
|
|
||||||
|
|
||||||
for (auto& child : top.children)
|
|
||||||
DoEntry(child);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::string> DoFileSearch(const std::vector<std::string>& directories,
|
std::vector<std::string> DoFileSearch(const std::vector<std::string>& directories,
|
||||||
const std::vector<std::string>& exts, bool recursive)
|
const std::vector<std::string>& exts, bool recursive)
|
||||||
{
|
{
|
||||||
std::vector<std::string> result;
|
const bool accept_all = exts.empty();
|
||||||
|
|
||||||
bool accept_all = exts.empty();
|
std::vector<fs::path> native_exts;
|
||||||
const auto callback = [&exts, accept_all](const File::FSTEntry& entry) {
|
for (const auto& ext : exts)
|
||||||
if (accept_all)
|
native_exts.push_back(StringToPath(ext));
|
||||||
return true;
|
|
||||||
if (entry.isDirectory)
|
// N.B. This avoids doing any copies
|
||||||
return false;
|
auto ext_matches = [&native_exts](const fs::path& path) {
|
||||||
return std::any_of(exts.begin(), exts.end(), [&](const std::string& ext) {
|
const std::basic_string_view<fs::path::value_type> native_path = path.native();
|
||||||
const std::string& name = entry.virtualName;
|
return std::any_of(native_exts.cbegin(), native_exts.cend(), [&native_path](const auto& ext) {
|
||||||
return name.length() >= ext.length() &&
|
const auto compare_len = ext.native().length();
|
||||||
strcasecmp(name.c_str() + name.length() - ext.length(), ext.c_str()) == 0;
|
if (native_path.length() < compare_len)
|
||||||
|
return false;
|
||||||
|
const auto substr_to_compare = native_path.substr(native_path.length() - compare_len);
|
||||||
|
#ifdef _WIN32
|
||||||
|
return CompareStringOrdinal(substr_to_compare.data(), static_cast<int>(compare_len),
|
||||||
|
ext.c_str(), static_cast<int>(compare_len), TRUE) == CSTR_EQUAL;
|
||||||
|
#else
|
||||||
|
return strncasecmp(substr_to_compare.data(), ext.c_str(), compare_len) == 0;
|
||||||
|
#endif
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const std::string& directory : directories)
|
std::vector<std::string> result;
|
||||||
|
auto add_filtered = [&](const fs::directory_entry& entry) {
|
||||||
|
auto& path = entry.path();
|
||||||
|
if (accept_all || (!entry.is_directory() && ext_matches(path)))
|
||||||
|
result.emplace_back(PathToString(path));
|
||||||
|
};
|
||||||
|
for (const auto& directory : directories)
|
||||||
{
|
{
|
||||||
#ifdef ANDROID
|
#ifdef ANDROID
|
||||||
// While File::ScanDirectoryTree (which is called in FileSearchWithTest) does handle Android
|
|
||||||
// content correctly, having a specialized implementation of DoFileSearch for Android content
|
|
||||||
// provides a much needed performance boost. Also, this specialized implementation will be
|
|
||||||
// required if we in the future replace the use of File::ScanDirectoryTree with std::filesystem.
|
|
||||||
if (IsPathAndroidContent(directory))
|
if (IsPathAndroidContent(directory))
|
||||||
{
|
{
|
||||||
const std::vector<std::string> partial_result =
|
const std::vector<std::string> partial_result =
|
||||||
@ -82,62 +75,22 @@ std::vector<std::string> DoFileSearch(const std::vector<std::string>& directorie
|
|||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
FileSearchWithTest(directory, recursive, &result, callback);
|
fs::path directory_path = StringToPath(directory);
|
||||||
}
|
std::error_code error;
|
||||||
}
|
|
||||||
|
|
||||||
// remove duplicates
|
|
||||||
std::sort(result.begin(), result.end());
|
|
||||||
result.erase(std::unique(result.begin(), result.end()), result.end());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
std::vector<std::string> DoFileSearch(const std::vector<std::string>& directories,
|
|
||||||
const std::vector<std::string>& exts, bool recursive)
|
|
||||||
{
|
|
||||||
bool accept_all = exts.empty();
|
|
||||||
|
|
||||||
std::vector<fs::path> native_exts;
|
|
||||||
for (const auto& ext : exts)
|
|
||||||
native_exts.push_back(StringToPath(ext));
|
|
||||||
|
|
||||||
// N.B. This avoids doing any copies
|
|
||||||
auto ext_matches = [&native_exts](const fs::path& path) {
|
|
||||||
const auto& native_path = path.native();
|
|
||||||
return std::any_of(native_exts.cbegin(), native_exts.cend(), [&native_path](const auto& ext) {
|
|
||||||
// TODO provide cross-platform compat for the comparison function, once more platforms
|
|
||||||
// support std::filesystem
|
|
||||||
int compare_len = static_cast<int>(ext.native().length());
|
|
||||||
return native_path.length() >= compare_len &&
|
|
||||||
CompareStringOrdinal(&native_path.c_str()[native_path.length() - compare_len],
|
|
||||||
compare_len, ext.c_str(), compare_len, TRUE) == CSTR_EQUAL;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
std::vector<std::string> result;
|
|
||||||
auto add_filtered = [&](const fs::directory_entry& entry) {
|
|
||||||
auto& path = entry.path();
|
|
||||||
if (accept_all || (ext_matches(path) && !fs::is_directory(path)))
|
|
||||||
result.emplace_back(PathToString(path));
|
|
||||||
};
|
|
||||||
for (const auto& directory : directories)
|
|
||||||
{
|
|
||||||
fs::path directory_path = StringToPath(directory);
|
|
||||||
if (fs::is_directory(directory_path)) // Can't create iterators for non-existant directories
|
|
||||||
{
|
|
||||||
if (recursive)
|
if (recursive)
|
||||||
{
|
{
|
||||||
// TODO use fs::directory_options::follow_directory_symlink ?
|
for (auto it = fs::recursive_directory_iterator(std::move(directory_path), error);
|
||||||
for (auto& entry : fs::recursive_directory_iterator(std::move(directory_path)))
|
it != fs::recursive_directory_iterator(); it.increment(error))
|
||||||
add_filtered(entry);
|
add_filtered(*it);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (auto& entry : fs::directory_iterator(std::move(directory_path)))
|
for (auto it = fs::directory_iterator(std::move(directory_path), error);
|
||||||
add_filtered(entry);
|
it != fs::directory_iterator(); it.increment(error))
|
||||||
|
add_filtered(*it);
|
||||||
}
|
}
|
||||||
|
if (error)
|
||||||
|
ERROR_LOG_FMT(COMMON, "{} error on {}: {}", __func__, directory, error.message());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,6 +113,4 @@ std::vector<std::string> DoFileSearch(const std::vector<std::string>& directorie
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
} // namespace Common
|
} // namespace Common
|
||||||
|
@ -54,7 +54,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef ANDROID
|
#ifdef ANDROID
|
||||||
#include "Common/StringUtil.h"
|
|
||||||
#include "jni/AndroidCommon/AndroidCommon.h"
|
#include "jni/AndroidCommon/AndroidCommon.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -645,7 +645,6 @@ std::u16string UTF8ToUTF16(std::string_view input)
|
|||||||
return converter.from_bytes(input.data(), input.data() + input.size());
|
return converter.from_bytes(input.data(), input.data() + input.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAS_STD_FILESYSTEM
|
|
||||||
// This is a replacement for path::u8path, which is deprecated starting with C++20.
|
// This is a replacement for path::u8path, which is deprecated starting with C++20.
|
||||||
std::filesystem::path StringToPath(std::string_view path)
|
std::filesystem::path StringToPath(std::string_view path)
|
||||||
{
|
{
|
||||||
@ -666,7 +665,6 @@ std::string PathToString(const std::filesystem::path& path)
|
|||||||
return path.native();
|
return path.native();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::vector<std::string> CommandLineToUtf8Argv(const wchar_t* command_line)
|
std::vector<std::string> CommandLineToUtf8Argv(const wchar_t* command_line)
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <filesystem>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
@ -16,11 +17,6 @@
|
|||||||
|
|
||||||
#include "Common/CommonTypes.h"
|
#include "Common/CommonTypes.h"
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#include <filesystem>
|
|
||||||
#define HAS_STD_FILESYSTEM
|
|
||||||
#endif
|
|
||||||
|
|
||||||
std::string StringFromFormatV(const char* format, va_list args);
|
std::string StringFromFormatV(const char* format, va_list args);
|
||||||
|
|
||||||
std::string StringFromFormat(const char* format, ...)
|
std::string StringFromFormat(const char* format, ...)
|
||||||
@ -214,10 +210,8 @@ inline std::string UTF8ToTStr(std::string_view str)
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAS_STD_FILESYSTEM
|
|
||||||
std::filesystem::path StringToPath(std::string_view path);
|
std::filesystem::path StringToPath(std::string_view path);
|
||||||
std::string PathToString(const std::filesystem::path& path);
|
std::string PathToString(const std::filesystem::path& path);
|
||||||
#endif
|
|
||||||
|
|
||||||
// Thousand separator. Turns 12345678 into 12,345,678
|
// Thousand separator. Turns 12345678 into 12,345,678
|
||||||
template <typename I>
|
template <typename I>
|
||||||
|
@ -3,12 +3,6 @@
|
|||||||
|
|
||||||
#include "Core/Boot/Boot.h"
|
#include "Core/Boot/Boot.h"
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#include <filesystem>
|
|
||||||
namespace fs = std::filesystem;
|
|
||||||
#define HAS_STD_FILESYSTEM
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -67,10 +61,6 @@ namespace fs = std::filesystem;
|
|||||||
static std::vector<std::string> ReadM3UFile(const std::string& m3u_path,
|
static std::vector<std::string> ReadM3UFile(const std::string& m3u_path,
|
||||||
const std::string& folder_path)
|
const std::string& folder_path)
|
||||||
{
|
{
|
||||||
#ifndef HAS_STD_FILESYSTEM
|
|
||||||
ASSERT(folder_path.back() == '/');
|
|
||||||
#endif
|
|
||||||
|
|
||||||
std::vector<std::string> result;
|
std::vector<std::string> result;
|
||||||
std::vector<std::string> nonexistent;
|
std::vector<std::string> nonexistent;
|
||||||
|
|
||||||
@ -91,12 +81,7 @@ static std::vector<std::string> ReadM3UFile(const std::string& m3u_path,
|
|||||||
|
|
||||||
if (!line.empty() && line.front() != '#') // Comments start with #
|
if (!line.empty() && line.front() != '#') // Comments start with #
|
||||||
{
|
{
|
||||||
#ifdef HAS_STD_FILESYSTEM
|
|
||||||
const std::string path_to_add = PathToString(StringToPath(folder_path) / StringToPath(line));
|
const std::string path_to_add = PathToString(StringToPath(folder_path) / StringToPath(line));
|
||||||
#else
|
|
||||||
const std::string path_to_add = line.front() != '/' ? folder_path + line : line;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
(File::Exists(path_to_add) ? result : nonexistent).push_back(path_to_add);
|
(File::Exists(path_to_add) ? result : nonexistent).push_back(path_to_add);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,13 +18,7 @@ namespace DiscIO
|
|||||||
{
|
{
|
||||||
static std::string MakeAbsolute(const std::string& directory, const std::string& path)
|
static std::string MakeAbsolute(const std::string& directory, const std::string& path)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
|
||||||
return PathToString(StringToPath(directory) / StringToPath(path));
|
return PathToString(StringToPath(directory) / StringToPath(path));
|
||||||
#else
|
|
||||||
if (StringBeginsWith(path, "/"))
|
|
||||||
return path;
|
|
||||||
return directory + "/" + path;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<GameModDescriptor> ParseGameModDescriptorFile(const std::string& filename)
|
std::optional<GameModDescriptor> ParseGameModDescriptorFile(const std::string& filename)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user