-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathEx.cs
234 lines (216 loc) · 9.48 KB
/
PathEx.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.IO;
namespace DiskFill
{
public static class PathEx
{
/// <summary>
/// Compute parent directory of the specified directory.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static string ParentDir( string path )
{
int parPos = path.LastIndexOf( Path.DirectorySeparatorChar, path.Length-2 );
if (parPos<0)
throw new ArgumentException($"Path {path} has no parent dir.");
string parent = path.Substring( 0, parPos+1 );
return parent;
}
public static void MoveDirectory( string source, string dest )
{
// Create parent for destination if missing
string parent = ParentDir( dest );
if ( !Directory.Exists( parent ) )
Directory.CreateDirectory( parent );
Directory.Move( source, dest );
}
/// <summary>
/// Extracts the relative path from base path to the sub path.
/// </summary>
/// <param name="basePath">The path to calculate relative path from.</param>
/// <param name="subPath">The path to calculate relative path to.</param>
/// <returns>Relative path from base to sub.</returns>
public static string RelativePath( string basePath, string subPath )
{
string baseAbsDir = Path.GetFullPath( basePath ).Trim('\\');
string subFull = Path.GetFullPath( subPath ).Trim('\\');
int baseLen = baseAbsDir.Length;
if ( baseLen > subFull.Length )
// Subdirectory is not under base
throw new ArgumentException( "Subdirectory is not below base directory." );
if ( baseAbsDir.ToLower() != subFull.Substring(0,baseLen).ToLower() )
// Subdirectory is in different location than base
throw new ArgumentException( "Subdirectory is not below base directory." );
string relPath = subFull.Substring( baseLen );
return relPath;
}
/// <summary>
/// Check if a pattern containing wildcards fits the string. (globbing)
/// </summary>
/// <param name="wild"></param>
/// <param name="str"></param>
/// <returns></returns>
private static bool MatchWildPattern( string wild, string str )
{
int sidx = 0;
int widx = 0;
int slen = str.Length;
int wlen = wild.Length;
wild += "_";
while (sidx < slen )
{
if (widx >= wlen)
return false;
if (wild[ widx ] == '*')
break;
if ((wild[ widx ] != str[ sidx ]) && (wild[ widx ] != '?'))
return false;
widx++;
sidx++;
}
int cp = 0;
int mp = 0;
while (sidx < slen )
{
if (wild[ widx ] == '*')
{
widx++;
if ( widx >= wlen )
return true;
mp = widx;
cp = sidx+1;
}
else if ((wild[ widx ] == str[ sidx ]) || (wild[ widx ] == '?'))
{
widx++;
sidx++;
}
else
{
widx = mp;
sidx = cp++;
}
}
while (wild[ widx ] == '*')
widx++;
return widx >= wlen;
}
/// <summary>
/// Calculate sum of bytes in directory including any subdirectories.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static long DirectorySize( string path, bool includeSubDirs )
{
long size = 0;
string[] files = Directory.GetFiles( path );
foreach( string file in files )
{
FileInfo f = new FileInfo( file );
size += f.Length;
}
if ( includeSubDirs )
{
files = Directory.GetDirectories( path );
foreach( string dir in files )
{
size += DirectorySize( dir, includeSubDirs );
}
}
return size;
}
public static long FileSize( string file )
{
FileInfo fi = new FileInfo( file );
return fi.Length;
}
}
/*
[TestFixture]
public class PathTest
{
[Test]
public void TestMatchWildPattern()
{
Assertion.AssertEquals( PathEx.MatchWildPattern( "", ""), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "*", ""), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "*", "A"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "", "A"), false );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*", ""), false );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*", "AAB"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*", "BAA"), false );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*", "A"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B", ""), false );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B", "AAB"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B", "AB"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B", "AABA"), false );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B", "ABAB"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B", "ABBBB"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*C", ""), false );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*C", "ABC"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*C", "ABCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*C", "ABBBC"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*C", "ABBBBCCCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*C", "ABCBBBCBCCCBCBCCCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*", "AB"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*", "AABA"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*", "ABAB"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*", "ABBBB"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*C*", ""), false );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*C*", "ABC"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*C*", "ABCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*C*", "ABBBC"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*C*", "ABBBBCCCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A*B*C*", "ABCBBBCBCCCBCBCCCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A?", "AAB"), false );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A?B", "AAB"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A?*", "A"), false );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A?*", "ABBCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPattern( "A?*", "BAA"), false );
Assertion.AssertEquals( PathEx.MatchWildPattern( "*.*", "aaaaa"), false );
}
[Test]
public void TestMatchWildPatternToFileName()
{
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "", ""), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "*", ""), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "*", "A"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "", "A"), false );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*", ""), false );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*", "AAB"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*", "BAA"), false );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*", "A"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B", ""), false );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B", "AAB"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B", "AB"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B", "AABA"), false );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B", "ABAB"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B", "ABBBB"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*C", ""), false );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*C", "ABC"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*C", "ABCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*C", "ABBBC"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*C", "ABBBBCCCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*C", "ABCBBBCBCCCBCBCCCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*", "AB"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*", "AABA"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*", "ABAB"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*", "ABBBB"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*C*", ""), false );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*C*", "ABC"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*C*", "ABCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*C*", "ABBBC"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*C*", "ABBBBCCCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A*B*C*", "ABCBBBCBCCCBCBCCCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A?", "AAB"), false );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A?B", "AAB"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A?*", "A"), false );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A?*", "ABBCC"), true );
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "A?*", "BAA"), false );
// Added MatchWildPatternToFileName functionality: (DOS compatibility)
Assertion.AssertEquals( PathEx.MatchWildPatternToFileName( "*.*", "aaaaa"), true );
}
}
*/
}