Search any tree-like data structure with wildcard and globbing patterns.
Simple, manageable code helps to understand the solution to the problem step by step. In this case, it is to understand globbing. Incidentally, it allows a fairly quick search in the file system. The code is deliberately kept simple so as not to distract from the actual goal.
Either clone the repo and add the project to your code-base or reference the nuget package.
The project uses the target framework netstandard2.0. Make sure you have a corresponding SDK installed.
git clone https://github.com/atzedent/globkit.git
dotnet add package Globkit --version 1.0.2
First you need an instance of your search tree. As a simple example, you can use the one shown below for the file system. With this create a new instance of the search. Now you are ready to search your tree with wildcard and globbing pattens.
The pathseparator is a constant character of the search tree's implementation. It is up to you to define yours for your implementation of your search tree. See the TestSearchTree in Globkit.Tests for a malleable version of such an implementation.
If you use the below FileSystemSearchTree then /**/*.log
on UNIX like systems searches recursively in all directories found under /
.
/var/log/*.log
searches flat in only /var/log/
for files with the extension .log
.
**\*.png
scans all Windows drives and searches for PNG files.
That's all it takes to search the file system.
void Main() {
var searchTree = new FilesystemSearchTree();
var search = new Search(searchTree);
var result = search.FindLeaves(Console.ReadLine());
foreach (var file in result)
Console.WriteLine(file);
}
class FilesystemSearchTree : ISearchTree {
public char PathSeparator => Path.DirectorySeparatorChar;
public bool BranchExists(string path) {
try { return Directory.Exists(path); }
catch { return false; }
}
public string CombinePaths(string first, string second) {
return Path.Combine(first, second);
}
public IEnumerable<string> GetBranches(string path){
try { return Directory.EnumerateDirectories(path); }
catch { return Enumerable.Empty<string>(); }
}
public IEnumerable<string> GetBranches(string path, string filter) {
try { return Directory.EnumerateDirectories(path, filter); }
catch { return Enumerable.Empty<string>(); }
}
public IEnumerable<string> GetLeaves(string path) {
try { return Directory.EnumerateFiles(path); }
catch { return Enumerable.Empty<string>(); }
}
public IEnumerable<string> GetLeaves(string path, string filter) {
try { return Directory.EnumerateFiles(path, filter); }
catch { return Enumerable.Empty<string>(); }
}
public string GetPathRoot(string path) {
return Path.GetPathRoot(path);
}
public IEnumerable<string> GetTreeRoots() {
return Environment.GetLogicalDrives();
}
public bool IsPathRooted(string path) {
return Path.IsPathRooted(path);
}
}
Try it here for yourself
Contributions are what make the open source community such an amazing place to be, learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Matthias Hurrle - @SOFTWARETOGO - cry4hurrle(at)gmail.com
Project Link: https://github.com/atzedent/globkit