FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Cocoar.FileSystem/src/Cocoar.FileSystem/SearchBuilder.cs at develop · cocoar-dev/Cocoar.FileSystem · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
cocoar-dev
/
Cocoar.FileSystem
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Cocoar.FileSystem
/
src
/
Cocoar.FileSystem
/
SearchBuilder.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
206 lines (180 loc) · 7.13 KB
Breadcrumbs
Cocoar.FileSystem
/
src
/
Cocoar.FileSystem
/
SearchBuilder.cs
Copy path
File metadata and controls
206 lines (180 loc) · 7.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using
System
.
Collections
;
namespace
Cocoar
.
FileSystem
;
/// <summary>
/// Fluent builder for configuring and executing file system searches.
/// </summary>
public
sealed
class
SearchBuilder
:
IEnumerable
<
string
>
{
private
readonly
string
_searchPath
;
private
string
_searchPattern
=
"*"
;
private
List
<
string
>
?
_filters
;
private
HashSet
<
string
>
?
_excludedFolders
;
private
int
?
_maxDepth
=
0
;
internal
SearchBuilder
(
string
searchPath
)
{
if
(
string
.
IsNullOrWhiteSpace
(
searchPath
)
)
throw
new
ArgumentException
(
"Search path cannot be null or whitespace."
,
nameof
(
searchPath
)
)
;
_searchPath
=
searchPath
;
}
/// <summary>
/// Adds file patterns to search for. Can be called multiple times to add more patterns.
/// Patterns are matched against filenames only (not full paths).
/// Supports DOS-style wildcards: * (any characters) and ? (single character).
/// </summary>
/// <param name="patterns">One or more file patterns (e.g., "*.txt", "test-*.log")</param>
/// <example>
/// .WithFilter("*.cs", "*.csproj")
/// .WithFilter("*.json") // Adds to existing patterns
/// </example>
public
SearchBuilder
WithFilter
(
params
string
[
]
patterns
)
{
ArgumentNullException
.
ThrowIfNull
(
patterns
)
;
if
(
patterns
.
Length
==
0
)
throw
new
ArgumentException
(
"At least one pattern must be specified."
,
nameof
(
patterns
)
)
;
if
(
patterns
.
Any
(
string
.
IsNullOrWhiteSpace
)
)
throw
new
ArgumentException
(
"Patterns cannot be null or whitespace."
,
nameof
(
patterns
)
)
;
_filters
??=
new
List
<
string
>
(
)
;
_filters
.
AddRange
(
patterns
)
;
_searchPattern
=
"*"
;
// Will filter in enumeration logic
return
this
;
}
/// <summary>
/// Sets a single file pattern to search for.
/// For multiple patterns, use WithFilter(params string[]) instead.
/// </summary>
/// <param name="pattern">File pattern (e.g., "*.txt")</param>
public
SearchBuilder
WithPattern
(
string
pattern
)
{
ArgumentException
.
ThrowIfNullOrWhiteSpace
(
pattern
)
;
_searchPattern
=
pattern
;
_filters
=
null
;
// Clear any multiple filters
return
this
;
}
/// <summary>
/// Clears all previously configured file patterns.
/// </summary>
public
SearchBuilder
ClearFilters
(
)
{
_filters
?
.
Clear
(
)
;
_searchPattern
=
"*"
;
return
this
;
}
public
SearchBuilder
Excluding
(
params
string
[
]
folderNames
)
{
ArgumentNullException
.
ThrowIfNull
(
folderNames
)
;
_excludedFolders
??=
new
HashSet
<
string
>
(
StringComparer
.
OrdinalIgnoreCase
)
;
foreach
(
var
folder
in
folderNames
)
{
if
(
!
string
.
IsNullOrWhiteSpace
(
folder
)
)
_excludedFolders
.
Add
(
folder
)
;
}
return
this
;
}
/// <summary>
/// Controls subdirectory recursion depth.
/// 0 = current directory only, 1 = direct children, 2+ = specific depth, -1 = unlimited.
/// </summary>
/// <param name="maxDepth">Maximum depth to recurse (-1 for unlimited)</param>
public
SearchBuilder
IncludeSubdirectories
(
int
maxDepth
)
{
if
(
maxDepth
<
-
1
)
throw
new
ArgumentException
(
"Max depth must be -1 (unlimited) or greater than or equal to 0."
,
nameof
(
maxDepth
)
)
;
_maxDepth
=
maxDepth
==
-
1
?
null
:
maxDepth
;
return
this
;
}
/// <summary>
/// Enables or disables subdirectory recursion.
/// True = unlimited depth, False = current directory only.
/// </summary>
/// <param name="include">Whether to include subdirectories</param>
public
SearchBuilder
IncludeSubdirectories
(
bool
include
=
true
)
{
_maxDepth
=
include
?
null
:
0
;
return
this
;
}
/// <summary>
/// 0 = current directory only, 1 = current + one level, etc. Use null for unlimited depth.
/// </summary>
public
SearchBuilder
WithMaxDepth
(
int
?
depth
)
{
if
(
depth
<
0
)
throw
new
ArgumentException
(
"Max depth cannot be negative."
,
nameof
(
depth
)
)
;
_maxDepth
=
depth
;
return
this
;
}
/// <summary>
/// Recursively search all subdirectories (unlimited depth).
/// </summary>
public
SearchBuilder
Recursively
(
)
{
_maxDepth
=
null
;
return
this
;
}
/// <summary>
/// Lazy operation that yields results as they are found.
/// </summary>
public
IEnumerable
<
string
>
Enumerate
(
)
{
// If multiple filters configured, search for each pattern
if
(
_filters
!=
null
&&
_filters
.
Count
>
0
)
{
var
allFiles
=
new
HashSet
<
string
>
(
StringComparer
.
OrdinalIgnoreCase
)
;
foreach
(
var
pattern
in
_filters
)
{
foreach
(
var
file
in
FileSearcher
.
EnumerateFiles
(
new
DirectoryInfo
(
_searchPath
)
,
pattern
,
_excludedFolders
,
_maxDepth
)
)
{
if
(
allFiles
.
Add
(
file
)
)
yield
return
file
;
}
}
}
else
{
// Single pattern
foreach
(
var
file
in
FileSearcher
.
EnumerateFiles
(
new
DirectoryInfo
(
_searchPath
)
,
_searchPattern
,
_excludedFolders
,
_maxDepth
)
)
{
yield
return
file
;
}
}
}
public
List
<
string
>
ToList
(
)
=>
Enumerate
(
)
.
ToList
(
)
;
public
string
[
]
ToArray
(
)
=>
Enumerate
(
)
.
ToArray
(
)
;
/// <summary>
/// Counts the number of files matching the configured criteria.
/// </summary>
/// <returns>The number of matching files.</returns>
public
int
Count
(
)
=>
Enumerate
(
)
.
Count
(
)
;
/// <summary>
/// Checks if any files match the configured criteria.
/// </summary>
/// <returns>True if at least one file matches; otherwise, false.</returns>
public
bool
Any
(
)
=>
Enumerate
(
)
.
Any
(
)
;
/// <summary>
/// Gets the first file matching the configured criteria.
/// </summary>
/// <returns>The full path of the first matching file.</returns>
/// <exception cref="InvalidOperationException">If no files match the criteria.</exception>
public
string
First
(
)
=>
Enumerate
(
)
.
First
(
)
;
/// <summary>
/// Gets the first file matching the configured criteria, or null if no files match.
/// </summary>
/// <returns>The full path of the first matching file, or null if none found.</returns>
public
string
?
FirstOrDefault
(
)
=>
Enumerate
(
)
.
FirstOrDefault
(
)
;
/// <summary>
/// Returns an enumerator that iterates through the matching files.
/// This enables foreach and LINQ support directly on the builder.
/// </summary>
public
IEnumerator
<
string
>
GetEnumerator
(
)
=>
Enumerate
(
)
.
GetEnumerator
(
)
;
/// <summary>
/// Returns an enumerator that iterates through the matching files.
/// </summary>
IEnumerator
IEnumerable
.
GetEnumerator
(
)
=>
GetEnumerator
(
)
;
}
Back
|
FazBrowse Home
|
New Git URL