private GlobPattern FixPattern(GlobPattern pattern) { //If the pattern ends with "**" we add a additional "*" so we have somthing to search for in all child directories. if (pattern.Parts.Length > 0 && pattern.Parts[pattern.Parts.Length-1] == "**") return new GlobPattern(pattern.Parts.Concat(new [] {"*"}).ToArray(), pattern.CurrentIndex); return pattern; }
public void Search(GlobPattern pattern, GlobPath curPath, List<GlobPath> result, ResultType resultType) { /* if (resultType == ResultType.Directory && pattern.IsOnLastPart) { //If the current directory matches the pattern, add this path to the result list if (pattern.MatchesCompletely(curPath)) result.Add(curPath.ToString()); }*/ if (resultType == ResultType.File && pattern.IsOnLastPart) { //Add all matching files to the result list foreach (var file in GetFiles(curPath)) { if (pattern.MatchesCurrentPart(file)) result.Add(curPath.AppendPart(file)); } } if (pattern.Parts[pattern.CurrentIndex] == ".." || pattern.Parts[pattern.CurrentIndex] == ".") { Search(pattern.SwitchToNextPart(), curPath.AppendPart(pattern.Parts[pattern.CurrentIndex]), result, resultType); return; } //If the next pattern part is recursive wildcard //we get list of all sub folders and continue the normal search there. if (pattern.Parts[pattern.CurrentIndex] == "**") { foreach (var subDir in GetRecursiveSubDirectories(curPath)) Search(pattern.SwitchToNextPart(), subDir, result, resultType); return; } //Traverse all sub directories which should be traversed foreach (var directory in GetDirectories(curPath)) { if (pattern.MatchesCurrentPart(directory)) if (pattern.IsOnLastPart && resultType == ResultType.Directory) { var newPath = curPath.AppendPart(directory); //Only add this path to result list if there is no longer version of this path result.RemoveAll(x => { if (x.Parts.Length <= newPath.Parts.Length) if (x.Parts.SequenceEqual(newPath.Parts.Take(x.Parts.Length))) return true; return false; }); result.Add(newPath); } else if (!pattern.IsOnLastPart) Search(pattern.SwitchToNextPart(), curPath.AppendPart(directory), result, resultType); } }
public static string ConvertFull(GlobPattern pattern) { StringBuilder sb = new StringBuilder(); sb.Append("^"); for (int i = 0; i < pattern.Parts.Length; i++) { if (pattern.Parts[i] == "**") sb.Append(@"(\\.*|[^\\]*$|$|)"); ConvertPart(pattern.Parts[i], sb); if (i != pattern.Parts.Length - 1) sb.Append(@"\\"); } sb.Append("$"); return sb.ToString(); }
private NormalizerResult NormalizeAbsolutePath(string pattern) { var globPattern = new GlobPattern(pattern); int firstWildecardIndex = globPattern.Parts.Length; for (int i = 0; i < globPattern.Parts.Length; i++) { if (ContainsWildecards(globPattern.Parts[i])) { firstWildecardIndex = i; break; } } return new NormalizerResult { Start = new GlobPath(string.Join(Path.DirectorySeparatorChar.ToString(), globPattern.Parts.Take(firstWildecardIndex).ToArray())), Pattern = FixPattern(new GlobPattern(globPattern.Parts, firstWildecardIndex)) }; }