diff --git a/src/common/file_system.cpp b/src/common/file_system.cpp index 2ceef316a..377ad45d6 100644 --- a/src/common/file_system.cpp +++ b/src/common/file_system.cpp @@ -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) diff --git a/src/common/file_system.h b/src/common/file_system.h index d51d7b799..56c76269d 100644 --- a/src/common/file_system.h +++ b/src/common/file_system.h @@ -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 diff --git a/src/duckstation-qt/memorycardeditorwindow.cpp b/src/duckstation-qt/memorycardeditorwindow.cpp index 4ef231212..83b04971a 100644 --- a/src/duckstation-qt/memorycardeditorwindow.cpp +++ b/src/duckstation-qt/memorycardeditorwindow.cpp @@ -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)); diff --git a/src/util/imgui_fullscreen.cpp b/src/util/imgui_fullscreen.cpp index beb29c704..503d46351 100644 --- a/src/util/imgui_fullscreen.cpp +++ b/src/util/imgui_fullscreen.cpp @@ -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 " ", 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) { diff --git a/src/util/input_manager.cpp b/src/util/input_manager.cpp index 28fd7f61b..f5d14cf68 100644 --- a/src/util/input_manager.cpp +++ b/src/util/input_manager.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin +// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin // 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 #include #include #include @@ -1438,13 +1439,15 @@ std::vector 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 ret; ret.reserve(results.size()); for (FILESYSTEM_FIND_DATA& fd : results) ret.emplace_back(Path::GetFileTitle(fd.FileName)); + return ret; } diff --git a/src/util/postprocessing.cpp b/src/util/postprocessing.cpp index 009b2b28f..936c32c8c 100644 --- a/src/util/postprocessing.cpp +++ b/src/util/postprocessing.cpp @@ -204,7 +204,7 @@ std::vector> PostProcessing::GetAvailableSha std::sort(names.begin(), names.end(), [](const std::pair& lhs, const std::pair& rhs) { - return (lhs.first < lhs.second); + return (StringUtil::Strcasecmp(lhs.first.c_str(), rhs.first.c_str()) < 0); }); return names;