-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathObjects.cs
236 lines (200 loc) · 5.5 KB
/
Objects.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
235
236
using System;
using System.Text.RegularExpressions;
namespace TaskBarRenamer
{
public class TaskBarWindow
{
#region Fields
public long Handle
{
get;
private set;
}
public string OriginalName
{
get;
private set;
}
public string NewName
{
get;
set;
}
public bool ForceName
{
get;
set;
}
public bool IsRenamed
{
get
{
return (NewName != null);
}
}
#endregion
#region Methods
public void SetPredecessor(TaskBarWindow predecessor)
{
if (predecessor == null)
return;
NewName = OriginalName;
OriginalName = predecessor.OriginalName;
ForceName = predecessor.ForceName;
}
public TaskBarWindow(EnumWindowsItem window)
{
if (window == null)
return;
ForceName = false;
Handle = (long)window.Handle;
OriginalName = window.Text;
}
public TaskBarWindow(long handle, string originalName, string newName, bool forceName)
{
Handle = handle;
OriginalName = originalName;
NewName = newName;
ForceName = forceName;
}
#endregion
}
public enum RenameType
{
Exact,
Wildcard,
RegExp
}
public class AutomaticEntry
{
#region Fields
public RenameType Type
{
get;
set;
}
public string From
{
get
{
switch (Type)
{
case RenameType.Wildcard:
return ("wc$" + FromName);
case RenameType.RegExp:
return ("re$" + FromName);
default:
return FromName;
}
}
}
public string FromName
{
get;
set;
}
public string ToName
{
get;
set;
}
public bool ForceName
{
get;
set;
}
private readonly Regex wildcard;
private readonly Regex regexp;
#endregion
#region Methods
public AutomaticEntry(string fromName, string toName, bool forceName)
{
if (fromName.StartsWith("wc$"))
{
Type = RenameType.Wildcard;
FromName = fromName.Remove(0, 3);
}
else if (fromName.StartsWith("re$"))
{
Type = RenameType.RegExp;
FromName = fromName.Remove(0, 3);
}
else
{
Type = RenameType.Exact;
FromName = fromName;
}
switch (Type)
{
case RenameType.Wildcard:
wildcard = BuildRegex(FromName, Type);
break;
case RenameType.RegExp:
regexp = BuildRegex(FromName, Type);
break;
}
ToName = toName;
ForceName = forceName;
}
public bool Matches(string name)
{
if (string.IsNullOrEmpty(name))
return false;
switch (Type)
{
case RenameType.Wildcard:
return wildcard.IsMatch(name);
case RenameType.RegExp:
return regexp.IsMatch(name);
}
return name.Equals(FromName, StringComparison.OrdinalIgnoreCase);
}
public string Rename(string name)
{
const int MAX_CAPTURES = 20;
switch (Type)
{
case RenameType.RegExp:
string result = ToName;
MatchCollection matches = regexp.Matches(name);
if (matches.Count > 0)
{
GroupCollection captures = matches[0].Groups;
for (int i = 0; i < MAX_CAPTURES; i++)
{
string replacement;
if (captures.Count > i) {
replacement = captures[i].Value;
}
else
{
replacement = string.Empty;
}
result = result.Replace((@"\" + i), replacement);
}
}
return result;
}
return ToName;
}
public static Regex BuildRegex(string pattern, RenameType type)
{
try
{
switch (type)
{
case RenameType.Wildcard:
return new Regex(
Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", "."),
RegexOptions.IgnoreCase
);
case RenameType.RegExp:
return new Regex(pattern, RegexOptions.IgnoreCase);
}
}
catch { }
return null;
}
#endregion
}
}