-
Notifications
You must be signed in to change notification settings - Fork 0
/
Globbing.cs
130 lines (108 loc) · 4.57 KB
/
Globbing.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using Minimatch;
namespace Jannesen.Tools.SourceCleaner
{
internal sealed class Globbing
{
private readonly string _cwd;
private readonly SortedDictionary<string, string> _files;
public IReadOnlyCollection<string> Files
{
get {
return _files.Values;
}
}
public Globbing(string cwd)
{
cwd = cwd.Replace('\\', '/');
if (cwd.Length < 3 || cwd[1] != ':' || cwd[2] != '/')
throw new FormatException("Invalid root.");
if (!cwd.EndsWith("/", StringComparison.Ordinal))
cwd += "/";
if (!Directory.Exists(cwd))
throw new DirectoryNotFoundException("Unknown directory '" + cwd + "'.");
_cwd = cwd;
_files = new SortedDictionary<string, string>();
}
public void Pattern(string pattern)
{
bool not = pattern.StartsWith("!", StringComparison.Ordinal);
if (not)
pattern = pattern.Substring(1);
pattern = _absolutePattern(_cwd, pattern);
var f = pattern.IndexOfAny(new char[] { '*', '?', '[', '(', '{', '+' } );
if (f < 0) {
if (!not) {
if (File.Exists(pattern.Replace('/', '\\'))) {
var key = pattern.ToLowerInvariant();
if (!_files.ContainsKey(key))
_files.Add(key, pattern);
}
}
else {
var key = pattern.ToLowerInvariant();
if (_files.ContainsKey(key))
_files.Remove(key);
}
}
else {
f = pattern.LastIndexOf('/', f);
if (f < 0)
throw new FormatException("Can't deterim root.");
var root = pattern.Substring(0, f);
var matcher = new Minimatch.Minimatcher(pattern.Substring(f + 1));
if (!not) {
foreach (var filename in Directory.EnumerateFiles(root.Replace('/', '\\'), "*", SearchOption.AllDirectories)) {
var key = filename.Replace('\\', '/').ToLowerInvariant();
if (!_files.ContainsKey(key) && matcher.IsMatch(key.Substring(f + 1)))
_files.Add(key, filename);
}
}
else {
root = (root + '/').ToLowerInvariant();
var toremove = new List<string>();
foreach(var key in _files.Keys) {
if (key.StartsWith(root, StringComparison.Ordinal)) {
if (matcher.IsMatch(key.Substring(f + 1)))
toremove.Add(key);
}
}
foreach(var key in toremove)
_files.Remove(key);
}
}
// var glob = Glob.Parse();
}
private static string _absolutePattern(string cwd, string pattern)
{
pattern = pattern.Replace('\\', '/');
if (pattern.StartsWith("//", StringComparison.Ordinal))
return pattern; // UNC Path
if (pattern.Length > 2 && pattern[1] == ':') {
if (pattern.Length < 3 || pattern[2] != '/')
throw new FormatException("Invalid pattern.");
return pattern; // Absolute path <drive>:/
}
if (pattern.StartsWith("/", StringComparison.Ordinal))
return cwd.Substring(0,2) + pattern;
for (;;) {
if (pattern.StartsWith("../", StringComparison.Ordinal)) {
int i = cwd.LastIndexOf('/', cwd.Length - 1);
if (i < 0)
throw new FormatException("can't go below root.");
cwd = cwd.Substring(0, i+1);
pattern = pattern.Substring(3);
continue;
}
if (pattern.StartsWith("./", StringComparison.Ordinal)) {
pattern = pattern.Substring(2);
continue;
}
break;
}
return cwd + pattern;
}
}
}