From 68c929b7cb10798ba41601a03998bf7cdb686b46 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 5 Dec 2021 19:35:45 +0000 Subject: [PATCH] Changed accessible-directory to cancellable TPL --- 2021/11/AccessibleDirectory.cs | 366 +++++++++++++++++++-------------- 1 file changed, 213 insertions(+), 153 deletions(-) diff --git a/2021/11/AccessibleDirectory.cs b/2021/11/AccessibleDirectory.cs index ae18269..76aa427 100644 --- a/2021/11/AccessibleDirectory.cs +++ b/2021/11/AccessibleDirectory.cs @@ -3,75 +3,101 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using System.Threading; +using System.Threading.Tasks; namespace RyzStudio.IO { public class AccessibleDirectory { - public static List GetFiles(string path, string pattern, bool searchTopOnly) + public static List GetFiles(string path, string pattern, bool searchTopOnly) => GetFilesAsync(path, pattern, searchTopOnly).Result; + + public static Task> GetFilesAsync(string path, string pattern, bool searchTopOnly) { - List fileList = new List(); - List directoryList = new List(); + CancellationToken cancellationToken = new CancellationToken(); - if (string.IsNullOrWhiteSpace(pattern)) pattern = "*"; + return GetFilesAsync(path, pattern, searchTopOnly, cancellationToken); + } - directoryList.Add(path); - - while (true) + public static Task> GetFilesAsync(string path, string pattern, bool searchTopOnly, CancellationToken cancellationToken) + { + return Task.Run>(() => { - if (directoryList.Count <= 0) + List fileList = new List(); + List directoryList = new List(); + + if (string.IsNullOrWhiteSpace(pattern)) pattern = "*"; + + if (cancellationToken.IsCancellationRequested) return fileList; + + directoryList.Add(path); + + while (true) { - break; - } - - string directory = directoryList.First(); - directoryList.RemoveAt(0); - - if (IsDirectoryAccessible(directory)) - { - IEnumerable searchDirList = new List(); - - try + if (directoryList.Count <= 0) { - searchDirList = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly); - } - catch (Exception) - { - continue; + break; } - if (!searchTopOnly) + if (cancellationToken.IsCancellationRequested) return fileList; + + string directory = directoryList.First(); + directoryList.RemoveAt(0); + + if (IsDirectoryAccessible(directory)) { - foreach (string item in searchDirList) + IEnumerable searchDirList = new List(); + + if (cancellationToken.IsCancellationRequested) return fileList; + + try { - if (!IsDirectoryAccessible(item)) + searchDirList = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly); + } + catch (Exception) + { + continue; + } + + if (!searchTopOnly) + { + foreach (string item in searchDirList) + { + if (!IsDirectoryAccessible(item)) + { + continue; + } + + directoryList.Add(item); + } + } + + foreach (string item in Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly)) + { + if (cancellationToken.IsCancellationRequested) return fileList; + + if (!MatchFileSearchPattern(pattern, Path.GetFileName(item))) { continue; } - directoryList.Add(item); - } - } + if (!IsFileAccessible(item)) + { + continue; + } - foreach (string item in Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly)) - { - if (!MatchFileSearchPattern(pattern, Path.GetFileName(item))) - { - continue; + fileList.Add(item); + + if (cancellationToken.IsCancellationRequested) return fileList; } - if (!IsFileAccessible(item)) - { - continue; - } - - fileList.Add(item); + if (cancellationToken.IsCancellationRequested) return fileList; } } - } - return fileList; + return fileList; + }); } public static IEnumerable EnumerateFiles(string path, string pattern, bool searchTopOnly) @@ -136,145 +162,179 @@ namespace RyzStudio.IO } } - public static void GetFiles(string path, string pattern, bool searchTopOnly, Func callback) + public static void GetFiles(string path, string pattern, bool searchTopOnly, Func callback) => GetFilesAsync(path, pattern, searchTopOnly, callback); + + public static Task GetFilesAsync(string path, string pattern, bool searchTopOnly, Func callback) { - List directoryList = new List(); - ulong searchCount = 0; + CancellationToken cancellationToken = new CancellationToken(); - if (string.IsNullOrWhiteSpace(pattern)) pattern = "*"; + return GetFilesAsync(path, pattern, searchTopOnly, callback, cancellationToken); + } - directoryList.Add(path); - searchCount++; - - while (true) + public static Task GetFilesAsync(string path, string pattern, bool searchTopOnly, Func callback, CancellationToken cancellationToken) + { + return Task.Run(() => { - if (directoryList.Count <= 0) + List directoryList = new List(); + ulong searchCount = 0; + + if (string.IsNullOrWhiteSpace(pattern)) pattern = "*"; + + if (cancellationToken.IsCancellationRequested) return; + + directoryList.Add(path); + searchCount++; + + while (true) { - break; - } - - string directory = directoryList.First(); - directoryList.RemoveAt(0); - - callback(null, searchCount, directoryList.Count); - - if (IsDirectoryAccessible(directory)) - { - IEnumerable searchDirList = new List(); - - try + if (directoryList.Count <= 0) { - searchDirList = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly); - } - catch (Exception) - { - continue; + break; } - if (!searchTopOnly) + if (cancellationToken.IsCancellationRequested) return; + + string directory = directoryList.First(); + directoryList.RemoveAt(0); + + callback(null, searchCount, directoryList.Count); + + if (IsDirectoryAccessible(directory)) { - foreach (string item in searchDirList) + IEnumerable searchDirList = new List(); + + try { - if (!IsDirectoryAccessible(item)) + searchDirList = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly); + } + catch (Exception) + { + continue; + } + + if (cancellationToken.IsCancellationRequested) return; + + if (!searchTopOnly) + { + foreach (string item in searchDirList) + { + if (!IsDirectoryAccessible(item)) + { + continue; + } + + directoryList.Add(item); + searchCount++; + + callback(null, searchCount, directoryList.Count); + } + } + + foreach (string item in Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly)) + { + if (cancellationToken.IsCancellationRequested) return; + + if (!MatchFileSearchPattern(pattern, Path.GetFileName(item))) { continue; } - directoryList.Add(item); - searchCount++; + if (!IsFileAccessible(item)) + { + continue; + } - callback(null, searchCount, directoryList.Count); + callback(item, searchCount, directoryList.Count); + + if (cancellationToken.IsCancellationRequested) return; } - } - foreach (string item in Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly)) + if (cancellationToken.IsCancellationRequested) return; + } + } + }); + } + + public static string GetNextFile(string filepath, string pattern) => GetNextFileAsync(filepath, pattern).Result; + + public static Task GetNextFileAsync(string filepath, string pattern) + { + return Task.Run(() => + { + if (string.IsNullOrWhiteSpace(filepath)) return null; + + string path = Path.GetDirectoryName(filepath); + if (string.IsNullOrWhiteSpace(path)) return null; + + string filename = Path.GetFileName(filepath); + if (string.IsNullOrWhiteSpace(filename)) return null; + + bool returnNext = false; + foreach (string item in Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)) + { + if (!MatchFileSearchPattern(pattern, Path.GetFileName(item))) { - if (!MatchFileSearchPattern(pattern, Path.GetFileName(item))) - { - continue; - } + continue; + } - if (!IsFileAccessible(item)) - { - continue; - } + if (!IsFileAccessible(item)) + { + continue; + } - callback(item, searchCount, directoryList.Count); + if (returnNext) + { + return item; + } + + if (Path.GetFileName(item).Equals(filename)) + { + returnNext = true; } } - } + + return null; + }); } - public static string GetNextFile(string filepath, string pattern) + public static string GetPreviousFile(string filepath, string pattern) => GetPreviousFileAsync(filepath, pattern).Result; + + public static Task GetPreviousFileAsync(string filepath, string pattern) { - if (string.IsNullOrWhiteSpace(filepath)) return null; - - string path = Path.GetDirectoryName(filepath); - if (string.IsNullOrWhiteSpace(path)) return null; - - string filename = Path.GetFileName(filepath); - if (string.IsNullOrWhiteSpace(filename)) return null; - - bool returnNext = false; - foreach (string item in Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)) + return Task.Run(() => { - if (!MatchFileSearchPattern(pattern, Path.GetFileName(item))) + if (string.IsNullOrWhiteSpace(filepath)) return null; + + string path = Path.GetDirectoryName(filepath); + if (string.IsNullOrWhiteSpace(path)) return null; + + string filename = Path.GetFileName(filepath); + if (string.IsNullOrWhiteSpace(filename)) return null; + + StringBuilder sb = new StringBuilder(); + foreach (string item in Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)) { - continue; + if (!MatchFileSearchPattern(pattern, Path.GetFileName(item))) + { + continue; + } + + if (!IsFileAccessible(item)) + { + continue; + } + + sb.Clear(); + sb.Append(item); + + if (Path.GetFileName(item).Equals(filename)) + { + return (sb.Length <= 0) ? null : sb.ToString(); + } } - if (!IsFileAccessible(item)) - { - continue; - } - - if (returnNext) - { - return item; - } - - if (Path.GetFileName(item).Equals(filename)) - { - returnNext = true; - } - } - - return null; - } - - public static string GetPreviousFile(string filepath, string pattern) - { - if (string.IsNullOrWhiteSpace(filepath)) return null; - - string path = Path.GetDirectoryName(filepath); - if (string.IsNullOrWhiteSpace(path)) return null; - - string filename = Path.GetFileName(filepath); - if (string.IsNullOrWhiteSpace(filename)) return null; - - StringBuilder sb = new StringBuilder(); - foreach (string item in Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)) - { - if (!MatchFileSearchPattern(pattern, Path.GetFileName(item))) - { - continue; - } - - if (!IsFileAccessible(item)) - { - continue; - } - - sb.Clear(); - sb.Append(item); - - if (Path.GetFileName(item).Equals(filename)) - { - return (sb.Length <= 0) ? null : sb.ToString(); - } - } - - return null; + return null; + }); } public static bool IsAccessible(string path)