-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileSearcher2.cs
More file actions
155 lines (136 loc) · 5.89 KB
/
FileSearcher2.cs
File metadata and controls
155 lines (136 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SimpleFileSearch
{
public class FileSearcher2
{
private BlockingCollection<string> _fileQueue = new BlockingCollection<string>(new ConcurrentQueue<string>());
private readonly List<FileInfo> _matchingFiles = new List<FileInfo>();
private int _totalFiles;
private int _processedCount;
private const int WorkerCount = 8; // Adjust number of consumers
public async Task<List<FileInfo>> SearchFilesAsync(
string directoryPath, string filenamePatterns, string searchText, bool isCaseSensitive, bool ignoreAccents, int maxFileSize, int parallelSearches, Action<string> statusCallback, CancellationToken cancellationToken = default)
{
bool searchInsideFiles = !string.IsNullOrEmpty(searchText);
_processedCount = 0;
_matchingFiles.Clear();
_fileQueue = new BlockingCollection<string>(new ConcurrentQueue<string>());
try
{
string[] patterns = filenamePatterns.Split(';', StringSplitOptions.RemoveEmptyEntries);
HashSet<string> allFiles = new HashSet<string>();
foreach (string pattern in patterns)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
foreach (string file in Directory.GetFiles(directoryPath, pattern, SearchOption.AllDirectories))
{
cancellationToken.ThrowIfCancellationRequested();
allFiles.Add(file);
}
}
catch (UnauthorizedAccessException) { }
catch (DirectoryNotFoundException) { }
}
_totalFiles = allFiles.Count;
statusCallback?.Invoke($"Found {_totalFiles} files to check...");
foreach (var file in allFiles)
{
cancellationToken.ThrowIfCancellationRequested();
_fileQueue.Add(file);
}
_fileQueue.CompleteAdding();
var consumers = Enumerable.Range(0, WorkerCount)
.Select(_ => Task.Run(() => ProcessFiles(searchInsideFiles, searchText, isCaseSensitive, ignoreAccents, maxFileSize, statusCallback, cancellationToken)))
.ToArray();
await Task.WhenAll(consumers);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
return _matchingFiles;
}
private void ProcessFiles(bool searchInsideFiles, string searchText, bool isCaseSensitive, bool ignoreAccents, int maxFileSize, Action<string> statusCallback, CancellationToken cancellationToken)
{
foreach (var filePath in _fileQueue.GetConsumingEnumerable(cancellationToken))
{
if (cancellationToken.IsCancellationRequested) break;
if (searchInsideFiles)
{
if (ContainsTextInFile(filePath, searchText, isCaseSensitive, ignoreAccents, maxFileSize))
{
lock (_matchingFiles)
{
_matchingFiles.Add(new FileInfo(filePath));
}
}
}
else
{
lock (_matchingFiles)
{
_matchingFiles.Add(new FileInfo(filePath));
}
}
Interlocked.Increment(ref _processedCount);
statusCallback?.Invoke($"Processed {_processedCount} of {_totalFiles} files...");
}
}
private bool ContainsTextInFile(string filePath, string searchText, bool isCaseSensitive, bool ignoreAccents, int maxFileSize)
{
try
{
if (maxFileSize > 0)
{
var fileInfo = new FileInfo(filePath);
if (fileInfo.Length > maxFileSize * 1024) // maxFileSize is in KB
return false;
}
StringComparison comparisonType = isCaseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase;
using StreamReader reader = new StreamReader(filePath);
string searchTextToCompare = ignoreAccents ? RemoveDiacritics(searchText) : searchText;
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (line != null)
{
string lineToCompare = ignoreAccents ? RemoveDiacritics(line) : line;
if (lineToCompare.Contains(searchTextToCompare, comparisonType))
{
return true;
}
}
}
}
catch (IOException) { }
catch (UnauthorizedAccessException) { }
return false;
}
private string RemoveDiacritics(string text)
{
if (string.IsNullOrEmpty(text))
return text;
try
{
return string.Concat(text
.Normalize(NormalizationForm.FormD)
.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark))
.Normalize(NormalizationForm.FormC);
}
catch (Exception)
{
return text;
}
}
}
}