From 1f74222bf4cfadead768b095c6b3f8d422ebf84c Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 11 Feb 2022 00:42:21 +0100 Subject: Add local-manga plugin to read local manga --- src/Storage.cpp | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'src/Storage.cpp') diff --git a/src/Storage.cpp b/src/Storage.cpp index 2754bc8..a0f20e4 100644 --- a/src/Storage.cpp +++ b/src/Storage.cpp @@ -256,7 +256,7 @@ namespace QuickMedia { } } - void for_files_in_dir_sort_last_modified(const Path &path, FileIteratorCallback callback) { + void for_files_in_dir_sort_last_modified(const Path &path, FileIteratorCallback callback, FileSortDirection sort_dir) { std::vector paths; try { for(auto &p : std::filesystem::directory_iterator(path.data)) { @@ -267,9 +267,42 @@ namespace QuickMedia { return; } - std::sort(paths.begin(), paths.end(), [](const std::filesystem::directory_entry &path1, std::filesystem::directory_entry &path2) { - return file_get_filetime_or(path1, std::filesystem::file_time_type::min()) > file_get_filetime_or(path2, std::filesystem::file_time_type::min()); - }); + if(sort_dir == FileSortDirection::ASC) { + std::sort(paths.begin(), paths.end(), [](const std::filesystem::directory_entry &path1, std::filesystem::directory_entry &path2) { + return file_get_filetime_or(path1, std::filesystem::file_time_type::min()) > file_get_filetime_or(path2, std::filesystem::file_time_type::min()); + }); + } else { + std::sort(paths.begin(), paths.end(), [](const std::filesystem::directory_entry &path1, std::filesystem::directory_entry &path2) { + return file_get_filetime_or(path1, std::filesystem::file_time_type::min()) < file_get_filetime_or(path2, std::filesystem::file_time_type::min()); + }); + } + + for(auto &p : paths) { + if(!callback(p.path().string())) + break; + } + } + + void for_files_in_dir_sort_name(const Path &path, FileIteratorCallback callback, FileSortDirection sort_dir) { + std::vector paths; + try { + for(auto &p : std::filesystem::directory_iterator(path.data)) { + paths.push_back(p); + } + } catch(const std::filesystem::filesystem_error &err) { + fprintf(stderr, "Failed to list files in directory %s, error: %s\n", path.data.c_str(), err.what()); + return; + } + + if(sort_dir == FileSortDirection::ASC) { + std::sort(paths.begin(), paths.end(), [](const std::filesystem::directory_entry &path1, std::filesystem::directory_entry &path2) { + return path1.path().filename() < path2.path().filename(); + }); + } else { + std::sort(paths.begin(), paths.end(), [](const std::filesystem::directory_entry &path1, std::filesystem::directory_entry &path2) { + return path1.path().filename() > path2.path().filename(); + }); + } for(auto &p : paths) { if(!callback(p.path().string())) -- cgit v1.2.3