Qt: Fix a couple of instances of inconsistent sorting

Backport from 7927ec647f
This commit is contained in:
KamFretoZ 2024-08-23 00:52:39 +07:00 committed by lightningterror
parent 518728ca36
commit 64e17fce3f
4 changed files with 43 additions and 5 deletions

View File

@ -1196,7 +1196,7 @@ size_t FileSystem::ReadFileWithProgress(std::FILE* fp, void* dst, size_t length,
break;
const size_t read_size = std::min(length - done, chunk_size);
if (std::fread(static_cast<u8*>(dst)+ done, read_size, 1, fp) != 1)
if (std::fread(static_cast<u8*>(dst) + done, read_size, 1, fp) != 1)
{
Error::SetErrno(error, "fread() failed: ", errno);
break;
@ -1460,7 +1460,24 @@ bool FileSystem::FindFiles(const char* path, const char* pattern, u32 flags, Fin
}
// enter the recursive function
return (RecursiveFindFiles(path, nullptr, nullptr, pattern, flags, results, visited) > 0);
if (RecursiveFindFiles(path, nullptr, nullptr, pattern, flags, results, visited) == 0)
return false;
if (flags & FILESYSTEM_FIND_SORT_BY_NAME)
{
std::sort(results->begin(), results->end(), [](const FILESYSTEM_FIND_DATA& lhs, const FILESYSTEM_FIND_DATA& rhs) {
// directories first
if ((lhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY) !=
(rhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY))
{
return ((lhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY) != 0);
}
return (StringUtil::Strcasecmp(lhs.FileName.c_str(), rhs.FileName.c_str()) < 0);
});
}
return true;
}
static void TranslateStat64(struct stat* st, const struct _stat64& st64)
@ -2042,7 +2059,24 @@ bool FileSystem::FindFiles(const char* path, const char* pattern, u32 flags, Fin
}
// enter the recursive function
return (RecursiveFindFiles(path, nullptr, nullptr, pattern, flags, results, visited) > 0);
if (RecursiveFindFiles(path, nullptr, nullptr, pattern, flags, results, visited) == 0)
return false;
if (flags & FILESYSTEM_FIND_SORT_BY_NAME)
{
std::sort(results->begin(), results->end(), [](const FILESYSTEM_FIND_DATA& lhs, const FILESYSTEM_FIND_DATA& rhs) {
// directories first
if ((lhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY) !=
(rhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY))
{
return ((lhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY) != 0);
}
return (StringUtil::Strcasecmp(lhs.FileName.c_str(), rhs.FileName.c_str()) < 0);
});
}
return true;
}
bool FileSystem::StatFile(const char* path, struct stat* st)

View File

@ -39,6 +39,7 @@ enum FILESYSTEM_FIND_FLAGS
FILESYSTEM_FIND_FOLDERS = (1 << 3),
FILESYSTEM_FIND_FILES = (1 << 4),
FILESYSTEM_FIND_KEEP_ARRAY = (1 << 5),
FILESYSTEM_FIND_SORT_BY_NAME = (1 << 6),
};
struct FILESYSTEM_STAT_DATA

View File

@ -1863,7 +1863,9 @@ void ImGuiFullscreen::PopulateFileSelectorItems()
{
FileSystem::FindResultsArray results;
FileSystem::FindFiles(s_file_selector_current_directory.c_str(), "*",
FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_FOLDERS | FILESYSTEM_FIND_HIDDEN_FILES | FILESYSTEM_FIND_RELATIVE_PATHS, &results);
FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_FOLDERS | FILESYSTEM_FIND_HIDDEN_FILES |
FILESYSTEM_FIND_RELATIVE_PATHS | FILESYSTEM_FIND_SORT_BY_NAME,
&results);
std::string parent_path;
std::string::size_type sep_pos = s_file_selector_current_directory.rfind(FS_OSPATH_SEPARATOR_CHARACTER);

View File

@ -473,7 +473,8 @@ std::vector<std::string> Pad::GetInputProfileNames()
{
FileSystem::FindResultsArray results;
FileSystem::FindFiles(EmuFolders::InputProfiles.c_str(), "*.ini",
FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_HIDDEN_FILES | FILESYSTEM_FIND_RELATIVE_PATHS,
FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_HIDDEN_FILES | FILESYSTEM_FIND_RELATIVE_PATHS |
FILESYSTEM_FIND_SORT_BY_NAME,
&results);
std::vector<std::string> ret;