Qt: Fix a couple of instances of inconsistent sorting

This commit is contained in:
Stenzek 2024-07-03 17:36:08 +10:00
parent 63b30e2925
commit 7927ec647f
No known key found for this signature in database
6 changed files with 46 additions and 16 deletions

View File

@ -1458,7 +1458,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)
@ -2022,7 +2039,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

@ -38,6 +38,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

@ -162,7 +162,8 @@ void MemoryCardEditorWindow::populateComboBox(QComboBox* cb)
FileSystem::FindResultsArray results;
FileSystem::FindFiles(EmuFolders::MemoryCards.c_str(), "*.mcd",
FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_RELATIVE_PATHS, &results);
FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_RELATIVE_PATHS | FILESYSTEM_FIND_SORT_BY_NAME,
&results);
for (FILESYSTEM_FIND_DATA& fd : results)
{
std::string real_filename(Path::Combine(EmuFolders::MemoryCards, fd.FileName));

View File

@ -2039,7 +2039,7 @@ 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,
FILESYSTEM_FIND_RELATIVE_PATHS | FILESYSTEM_FIND_SORT_BY_NAME,
&results);
std::string parent_path;
@ -2048,15 +2048,6 @@ void ImGuiFullscreen::PopulateFileSelectorItems()
parent_path = Path::Canonicalize(s_file_selector_current_directory.substr(0, sep_pos));
s_file_selector_items.emplace_back(ICON_FA_FOLDER_OPEN " <Parent Directory>", std::move(parent_path), false);
std::sort(results.begin(), results.end(), [](const FILESYSTEM_FIND_DATA& lhs, const FILESYSTEM_FIND_DATA& rhs) {
if ((lhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY) !=
(rhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY))
return (lhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY) != 0;
// return std::lexicographical_compare(lhs.FileName.begin(), lhs.FileName.end(), rhs.FileName.begin(),
// rhs.FileName.end());
return (StringUtil::Strcasecmp(lhs.FileName.c_str(), rhs.FileName.c_str()) < 0);
});
for (const FILESYSTEM_FIND_DATA& fd : results)
{

View File

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "input_manager.h"
@ -18,6 +18,7 @@
#include "fmt/core.h"
#include <algorithm>
#include <array>
#include <atomic>
#include <memory>
@ -1438,13 +1439,15 @@ std::vector<std::string> InputManager::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;
ret.reserve(results.size());
for (FILESYSTEM_FIND_DATA& fd : results)
ret.emplace_back(Path::GetFileTitle(fd.FileName));
return ret;
}

View File

@ -204,7 +204,7 @@ std::vector<std::pair<std::string, std::string>> PostProcessing::GetAvailableSha
std::sort(names.begin(), names.end(),
[](const std::pair<std::string, std::string>& lhs, const std::pair<std::string, std::string>& rhs) {
return (lhs.first < lhs.second);
return (StringUtil::Strcasecmp(lhs.first.c_str(), rhs.first.c_str()) < 0);
});
return names;