| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text; |
| | | 6 | | using System.Text.RegularExpressions; |
| | | 7 | | |
| | | 8 | | namespace DirectSight.Common; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Finds files and directories by matching their path names against a pattern. |
| | | 12 | | /// Implementation based on https://github.com/mganss/Glob.cs. |
| | | 13 | | /// </summary> |
| | | 14 | | internal class Glob |
| | | 15 | | { |
| | 1 | 16 | | private static readonly char[] GlobCharacters = "*?[]{}".ToCharArray(); |
| | | 17 | | |
| | 1 | 18 | | private static readonly Dictionary<string, RegexOrString> RegexOrStringCache = []; |
| | | 19 | | |
| | 1 | 20 | | private static readonly HashSet<char> RegexSpecialChars = new HashSet<char>(['[', '\\', '^', '$', '.', '|', '?', '*' |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="Glob"/> class. |
| | | 24 | | /// </summary> |
| | 0 | 25 | | public Glob() |
| | 0 | 26 | | { |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="Glob"/> class. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="pattern">The pattern to be matched. See <see cref="Pattern"/> for syntax.</param> |
| | 18 | 33 | | public Glob(string pattern) |
| | 18 | 34 | | { |
| | 18 | 35 | | this.Pattern = pattern; |
| | 18 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets or sets a value indicating the pattern to match file and directory names against. |
| | | 40 | | /// The pattern can contain the following special characters: |
| | | 41 | | /// <list type="table"> |
| | | 42 | | /// <item> |
| | | 43 | | /// <term>?</term> |
| | | 44 | | /// <description>Matches any single character in a file or directory name.</description> |
| | | 45 | | /// </item> |
| | | 46 | | /// <item> |
| | | 47 | | /// <term>*</term> |
| | | 48 | | /// <description>Matches zero or more characters in a file or directory name.</description> |
| | | 49 | | /// </item> |
| | | 50 | | /// <item> |
| | | 51 | | /// <term>**</term> |
| | | 52 | | /// <description>Matches zero or more recursve directories.</description> |
| | | 53 | | /// </item> |
| | | 54 | | /// <item> |
| | | 55 | | /// <term>[...]</term> |
| | | 56 | | /// <description>Matches a set of characters in a name. Syntax is equivalent to character groups in <see cref="Syste |
| | | 57 | | /// </item> |
| | | 58 | | /// <item> |
| | | 59 | | /// <term>{group1,group2,...}</term> |
| | | 60 | | /// <description>Matches any of the pattern groups. Groups can contain groups and patterns.</description> |
| | | 61 | | /// </item> |
| | | 62 | | /// </list> |
| | | 63 | | /// </summary> |
| | 36 | 64 | | public string Pattern { get; set; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets or sets a value indicating whether case should be ignored in file and directory names. Default is true. |
| | | 68 | | /// </summary> |
| | 62 | 69 | | public bool IgnoreCase { get; set; } = true; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Returns a <see cref="string" /> that represents this instance. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <returns> |
| | | 75 | | /// A <see cref="string" /> that represents this instance. |
| | | 76 | | /// </returns> |
| | | 77 | | public override string ToString() |
| | 0 | 78 | | { |
| | 0 | 79 | | return this.Pattern; |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Returns a hash code for this instance. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <returns> |
| | | 86 | | /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. |
| | | 87 | | /// </returns> |
| | | 88 | | public override int GetHashCode() |
| | 0 | 89 | | { |
| | 0 | 90 | | return this.Pattern.GetHashCode(); |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Determines whether the specified <see cref="object" />, is equal to this instance. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="obj">The <see cref="object" /> to compare with this instance.</param> |
| | | 97 | | /// <returns> |
| | | 98 | | /// <c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>. |
| | | 99 | | /// </returns> |
| | | 100 | | public override bool Equals(object obj) |
| | 0 | 101 | | { |
| | | 102 | | // Check for null and compare run-time types. |
| | 0 | 103 | | if (obj == null || this.GetType() != obj.GetType()) |
| | 0 | 104 | | { |
| | 0 | 105 | | return false; |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | var g = (Glob)obj; |
| | 0 | 109 | | return this.Pattern == g.Pattern; |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Performs a pattern match. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <returns>The matched path names.</returns> |
| | | 116 | | public IEnumerable<string> ExpandNames() |
| | 18 | 117 | | { |
| | 153 | 118 | | return this.Expand(this.Pattern, false).Select(f => f.FullName); |
| | 18 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Performs a pattern match. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <returns>The matched <see cref="FileSystemInfo"/> objects.</returns> |
| | | 125 | | public IEnumerable<FileSystemInfo> Expand() |
| | 0 | 126 | | { |
| | 0 | 127 | | return this.Expand(this.Pattern, false); |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | private RegexOrString CreateRegexOrString(string pattern) |
| | 11 | 131 | | { |
| | 11 | 132 | | if (!RegexOrStringCache.TryGetValue(pattern, out RegexOrString regexOrString)) |
| | 6 | 133 | | { |
| | 6 | 134 | | regexOrString = new RegexOrString(GlobToRegex(pattern), pattern, this.IgnoreCase, compileRegex: true); |
| | 6 | 135 | | RegexOrStringCache[pattern] = regexOrString; |
| | 6 | 136 | | } |
| | | 137 | | |
| | 11 | 138 | | return regexOrString; |
| | 11 | 139 | | } |
| | | 140 | | |
| | | 141 | | private IEnumerable<FileSystemInfo> Expand(string path, bool dirOnly) |
| | 38 | 142 | | { |
| | 38 | 143 | | if (string.IsNullOrEmpty(path)) |
| | 0 | 144 | | { |
| | 0 | 145 | | yield break; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | // stop looking if there are no more glob characters in the path. |
| | | 149 | | // but only if ignoring case because FileSystemInfo.Exists always ignores case. |
| | 38 | 150 | | if (this.IgnoreCase && path.IndexOfAny(GlobCharacters) < 0) |
| | 27 | 151 | | { |
| | 27 | 152 | | FileSystemInfo fsi = null; |
| | 27 | 153 | | bool exists = false; |
| | | 154 | | |
| | 27 | 155 | | fsi = dirOnly ? (FileSystemInfo)new DirectoryInfo(path) : new FileInfo(path); |
| | 27 | 156 | | exists = fsi.Exists; |
| | | 157 | | |
| | 27 | 158 | | if (exists) |
| | 24 | 159 | | { |
| | 24 | 160 | | yield return fsi; |
| | 15 | 161 | | } |
| | | 162 | | |
| | 18 | 163 | | yield break; |
| | | 164 | | } |
| | | 165 | | |
| | 11 | 166 | | string parent = Path.GetDirectoryName(path); |
| | | 167 | | |
| | 11 | 168 | | if (parent == null) |
| | 0 | 169 | | { |
| | 0 | 170 | | DirectoryInfo dir = new DirectoryInfo(path); |
| | | 171 | | |
| | 0 | 172 | | if (dir != null) |
| | 0 | 173 | | { |
| | 0 | 174 | | yield return dir; |
| | 0 | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | yield break; |
| | | 178 | | } |
| | | 179 | | |
| | 11 | 180 | | if (parent == string.Empty) |
| | 1 | 181 | | { |
| | 1 | 182 | | parent = Directory.GetCurrentDirectory(); |
| | 1 | 183 | | } |
| | | 184 | | |
| | 11 | 185 | | var child = Path.GetFileName(path); |
| | | 186 | | |
| | | 187 | | // handle groups that contain folders |
| | | 188 | | // child will contain unmatched closing brace |
| | 95 | 189 | | if (child.Count(c => c == '}') > child.Count(c => c == '{')) |
| | 0 | 190 | | { |
| | 0 | 191 | | foreach (var group in Ungroup(path)) |
| | 0 | 192 | | { |
| | 0 | 193 | | foreach (var item in this.Expand(group, dirOnly)) |
| | 0 | 194 | | { |
| | 0 | 195 | | yield return item; |
| | 0 | 196 | | } |
| | 0 | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | yield break; |
| | | 200 | | } |
| | | 201 | | |
| | 11 | 202 | | if (child == "**") |
| | 0 | 203 | | { |
| | 0 | 204 | | foreach (DirectoryInfo dir in this.Expand(parent, true).DistinctBy(d => d.FullName).Cast<DirectoryInfo>()) |
| | 0 | 205 | | { |
| | 0 | 206 | | yield return dir; |
| | | 207 | | |
| | 0 | 208 | | foreach (var subDir in GetDirectories(dir)) |
| | 0 | 209 | | { |
| | 0 | 210 | | yield return subDir; |
| | 0 | 211 | | } |
| | 0 | 212 | | } |
| | | 213 | | |
| | 0 | 214 | | yield break; |
| | | 215 | | } |
| | | 216 | | |
| | 22 | 217 | | var childRegexes = Ungroup(child).Select(s => this.CreateRegexOrString(s)).ToList(); |
| | | 218 | | |
| | 84 | 219 | | foreach (DirectoryInfo parentDir in this.Expand(parent, true).DistinctBy(d => d.FullName).Cast<DirectoryInfo>()) |
| | 17 | 220 | | { |
| | 17 | 221 | | IEnumerable<FileSystemInfo> fileSystemEntries = dirOnly ? parentDir.EnumerateDirectories() : parentDir.Enume |
| | | 222 | | |
| | 593 | 223 | | foreach (var fileSystemEntry in fileSystemEntries) |
| | 271 | 224 | | { |
| | 542 | 225 | | if (childRegexes.Any(r => r.IsMatch(fileSystemEntry.Name))) |
| | 128 | 226 | | { |
| | 128 | 227 | | yield return fileSystemEntry; |
| | 128 | 228 | | } |
| | 271 | 229 | | } |
| | | 230 | | |
| | 34 | 231 | | if (childRegexes.Any(r => r.Pattern == @"^\.\.$")) |
| | 0 | 232 | | { |
| | 0 | 233 | | yield return parentDir.Parent ?? parentDir; |
| | 0 | 234 | | } |
| | | 235 | | |
| | 34 | 236 | | if (childRegexes.Any(r => r.Pattern == @"^\.$")) |
| | 0 | 237 | | { |
| | 0 | 238 | | yield return parentDir; |
| | 0 | 239 | | } |
| | 17 | 240 | | } |
| | 11 | 241 | | } |
| | | 242 | | |
| | | 243 | | private static string GlobToRegex(string glob) |
| | 6 | 244 | | { |
| | 6 | 245 | | var regex = new StringBuilder(); |
| | 6 | 246 | | var characterClass = false; |
| | | 247 | | |
| | 6 | 248 | | regex.Append("^"); |
| | | 249 | | |
| | 84 | 250 | | foreach (var c in glob) |
| | 33 | 251 | | { |
| | 33 | 252 | | if (characterClass) |
| | 0 | 253 | | { |
| | 0 | 254 | | if (c == ']') |
| | 0 | 255 | | { |
| | 0 | 256 | | characterClass = false; |
| | 0 | 257 | | } |
| | | 258 | | |
| | 0 | 259 | | regex.Append(c); |
| | 0 | 260 | | continue; |
| | | 261 | | } |
| | | 262 | | |
| | 33 | 263 | | switch (c) |
| | | 264 | | { |
| | | 265 | | case '*': |
| | 7 | 266 | | regex.Append(".*"); |
| | 7 | 267 | | break; |
| | | 268 | | case '?': |
| | 1 | 269 | | regex.Append("."); |
| | 1 | 270 | | break; |
| | | 271 | | case '[': |
| | 0 | 272 | | characterClass = true; |
| | 0 | 273 | | regex.Append(c); |
| | 0 | 274 | | break; |
| | | 275 | | default: |
| | 25 | 276 | | if (RegexSpecialChars.Contains(c)) |
| | 3 | 277 | | { |
| | 3 | 278 | | regex.Append('\\'); |
| | 3 | 279 | | } |
| | | 280 | | |
| | 25 | 281 | | regex.Append(c); |
| | 25 | 282 | | break; |
| | | 283 | | } |
| | 33 | 284 | | } |
| | | 285 | | |
| | 6 | 286 | | regex.Append("$"); |
| | | 287 | | |
| | 6 | 288 | | return regex.ToString(); |
| | 6 | 289 | | } |
| | | 290 | | |
| | | 291 | | private static IEnumerable<string> Ungroup(string path) |
| | 11 | 292 | | { |
| | 11 | 293 | | if (!path.Contains('{')) |
| | 11 | 294 | | { |
| | 11 | 295 | | yield return path; |
| | 11 | 296 | | yield break; |
| | | 297 | | } |
| | | 298 | | |
| | 0 | 299 | | var level = 0; |
| | 0 | 300 | | var option = new StringBuilder(); |
| | 0 | 301 | | var prefix = string.Empty; |
| | 0 | 302 | | var postfix = string.Empty; |
| | 0 | 303 | | var options = new List<string>(); |
| | | 304 | | |
| | 0 | 305 | | for (int i = 0; i < path.Length; i++) |
| | 0 | 306 | | { |
| | 0 | 307 | | var c = path[i]; |
| | | 308 | | |
| | 0 | 309 | | switch (c) |
| | | 310 | | { |
| | | 311 | | case '{': |
| | 0 | 312 | | level++; |
| | 0 | 313 | | if (level == 1) |
| | 0 | 314 | | { |
| | 0 | 315 | | prefix = option.ToString(); |
| | 0 | 316 | | option.Clear(); |
| | 0 | 317 | | } |
| | | 318 | | else |
| | 0 | 319 | | { |
| | 0 | 320 | | option.Append(c); |
| | 0 | 321 | | } |
| | | 322 | | |
| | 0 | 323 | | break; |
| | | 324 | | case ',': |
| | 0 | 325 | | if (level == 1) |
| | 0 | 326 | | { |
| | 0 | 327 | | options.Add(option.ToString()); |
| | 0 | 328 | | option.Clear(); |
| | 0 | 329 | | } |
| | | 330 | | else |
| | 0 | 331 | | { |
| | 0 | 332 | | option.Append(c); |
| | 0 | 333 | | } |
| | | 334 | | |
| | 0 | 335 | | break; |
| | | 336 | | case '}': |
| | 0 | 337 | | level--; |
| | 0 | 338 | | if (level == 0) |
| | 0 | 339 | | { |
| | 0 | 340 | | options.Add(option.ToString()); |
| | 0 | 341 | | break; |
| | | 342 | | } |
| | | 343 | | else |
| | 0 | 344 | | { |
| | 0 | 345 | | option.Append(c); |
| | 0 | 346 | | } |
| | | 347 | | |
| | 0 | 348 | | break; |
| | | 349 | | default: |
| | 0 | 350 | | option.Append(c); |
| | 0 | 351 | | break; |
| | | 352 | | } |
| | | 353 | | |
| | 0 | 354 | | if (level == 0 && c == '}' && (i + 1) < path.Length) |
| | 0 | 355 | | { |
| | 0 | 356 | | postfix = path.Substring(i + 1); |
| | 0 | 357 | | break; |
| | | 358 | | } |
| | 0 | 359 | | } |
| | | 360 | | |
| | | 361 | | // invalid grouping |
| | 0 | 362 | | if (level > 0) |
| | 0 | 363 | | { |
| | 0 | 364 | | yield return path; |
| | 0 | 365 | | yield break; |
| | | 366 | | } |
| | | 367 | | |
| | 0 | 368 | | var postGroups = Ungroup(postfix); |
| | | 369 | | |
| | 0 | 370 | | foreach (var opt in options.SelectMany(o => Ungroup(o))) |
| | 0 | 371 | | { |
| | 0 | 372 | | foreach (var postGroup in postGroups) |
| | 0 | 373 | | { |
| | 0 | 374 | | var s = prefix + opt + postGroup; |
| | 0 | 375 | | yield return s; |
| | 0 | 376 | | } |
| | 0 | 377 | | } |
| | 0 | 378 | | } |
| | | 379 | | |
| | | 380 | | private static IEnumerable<DirectoryInfo> GetDirectories(DirectoryInfo root) |
| | 0 | 381 | | { |
| | | 382 | | IEnumerable<DirectoryInfo> subDirs; |
| | | 383 | | |
| | | 384 | | try |
| | 0 | 385 | | { |
| | 0 | 386 | | subDirs = root.EnumerateDirectories(); |
| | 0 | 387 | | } |
| | 0 | 388 | | catch (Exception) |
| | 0 | 389 | | { |
| | 0 | 390 | | yield break; |
| | | 391 | | } |
| | | 392 | | |
| | 0 | 393 | | foreach (DirectoryInfo dirInfo in subDirs) |
| | 0 | 394 | | { |
| | 0 | 395 | | yield return dirInfo; |
| | | 396 | | |
| | 0 | 397 | | foreach (var recursiveDir in GetDirectories(dirInfo)) |
| | 0 | 398 | | { |
| | 0 | 399 | | yield return recursiveDir; |
| | 0 | 400 | | } |
| | 0 | 401 | | } |
| | 0 | 402 | | } |
| | | 403 | | |
| | | 404 | | private class RegexOrString |
| | | 405 | | { |
| | 6 | 406 | | public RegexOrString(string pattern, string rawString, bool ignoreCase, bool compileRegex) |
| | 6 | 407 | | { |
| | 6 | 408 | | this.IgnoreCase = ignoreCase; |
| | | 409 | | |
| | | 410 | | try |
| | 6 | 411 | | { |
| | 6 | 412 | | this.Regex = new Regex( |
| | 6 | 413 | | pattern, |
| | 6 | 414 | | RegexOptions.CultureInvariant | (ignoreCase ? RegexOptions.IgnoreCase : 0) | (compileRegex ? RegexOp |
| | 6 | 415 | | this.Pattern = pattern; |
| | 6 | 416 | | } |
| | 0 | 417 | | catch |
| | 0 | 418 | | { |
| | 0 | 419 | | this.Pattern = rawString; |
| | 0 | 420 | | } |
| | 6 | 421 | | } |
| | | 422 | | |
| | 548 | 423 | | public Regex Regex { get; set; } |
| | | 424 | | |
| | 40 | 425 | | public string Pattern { get; set; } |
| | | 426 | | |
| | 6 | 427 | | public bool IgnoreCase { get; set; } |
| | | 428 | | |
| | | 429 | | public bool IsMatch(string input) |
| | 271 | 430 | | { |
| | 271 | 431 | | if (this.Regex != null) |
| | 271 | 432 | | { |
| | 271 | 433 | | return this.Regex.IsMatch(input); |
| | | 434 | | } |
| | | 435 | | |
| | 0 | 436 | | return this.Pattern.Equals(input, this.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Or |
| | 271 | 437 | | } |
| | | 438 | | } |
| | | 439 | | } |