Skip to content

Commit

Permalink
Qt: Fix a couple of instances of inconsistent sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
kamfretoz authored and lightningterror committed Aug 24, 2024
1 parent 518728c commit 64e17fc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
40 changes: 37 additions & 3 deletions common/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions common/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion pcsx2/ImGui/ImGuiFullscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion pcsx2/SIO/Pad/Pad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 64e17fc

Please sign in to comment.