forked from kyordhel/GPSRCmdGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wildcard.cs
223 lines (184 loc) · 4.95 KB
/
Wildcard.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GPSRCmdGen
{
public class Wildcard
{
#region Variables
/// <summary>
/// Stores the Wildcard id
/// </summary>
private int id;
/// <summary>
/// Stores the
/// </summary>
private int index;
/// <summary>
/// Stores the Wildcard metadata
/// </summary>
private string metadata;
/// <summary>
/// Stores the name of the wildcard
/// </summary>
private string name;
/// <summary>
///Indicates if the wildcard is obfuscated
/// </summary>
private bool obfuscated;
/// <summary>
/// Stores the type of the wildcard
/// </summary>
private string type;
/// <summary>
/// Stores the
/// </summary>
private string value;
#endregion
#region Constructors
private Wildcard() { }
#endregion
#region Properties
/// <summary>
/// Gets the Wildcard id
/// </summary>
public int Id { get { return this.id; } }
/// <summary>
/// Gets the
/// </summary>
public int Index { get { return this.index; } }
/// <summary>
/// Gets the Wildcard metadata
/// </summary>
public string Metadata { get { return this.metadata; } }
/// <summary>
/// Gets the name of the wildcard
/// </summary>
public string Name { get { return this.name; } }
/// <summary>
/// Gets a value indicating if the wildcard is obfuscated
/// </summary>
public bool Obfuscated { get { return this.obfuscated; } }
/// <summary>
/// Gets a value indicating if the wildcard is valid
/// </summary>
public bool Success { get { return !String.IsNullOrEmpty(this.Name); } }
/// <summary>
/// Gets the type of the wildcard
/// </summary>
public string Type { get { return this.type; } }
/// <summary>
/// Gets the
/// </summary>
public string Value { get { return this.value; } }
#endregion
#region Methods
public override string ToString()
{
//string s = String.Empty;
//if (!String.IsNullOrEmpty(this.Name))
// s += "Name=" + this.Name;
//if (!String.IsNullOrEmpty(this.Type))
// s += " Type=" + this.Type;
//if (this.Id != -1)
// s += String.Format(" Id={0}", this.Id);
//if (!String.IsNullOrEmpty(this.Metadata))
// s += " Metadata=" + this.Metadata;
//return s.TrimStart();
return this.value;
}
#endregion
#region Static Methods
public static Wildcard XtractWildcard(string s, ref int cc)
{
if ((cc >= s.Length) || (s[cc] != '{'))
return null;
// Create Wildcard and set index
Wildcard wildcard = new Wildcard();
wildcard.index = cc;
// Read wildcard name
++cc;
wildcard.name = ReadWildcardName(s, ref cc);
if (String.IsNullOrEmpty(wildcard.Name)) return null;
// Read obfuscator
wildcard.obfuscated = Scanner.ReadChar('?', s, ref cc);
// Read wildcard type
wildcard.type = ReadWildcardType(s, ref cc);
// Read wildcard id
wildcard.id = ReadWildcardId(s, ref cc);
// Read wildcard id
wildcard.metadata = ReadWildcardMetadata(s, ref cc);
// Set wildcard value
if (cc < s.Length) ++cc;
wildcard.value = s.Substring(wildcard.index, cc - wildcard.index);
return wildcard;
}
private static string ReadWildcardName(string s, ref int cc)
{
Scanner.SkipSpaces(s, ref cc);
int bcc = cc;
while ((cc < s.Length) && Scanner.IsLAlpha(s[cc])) ++cc;
return s.Substring(bcc, cc - bcc);
}
private static string ReadWildcardType(string s, ref int cc)
{
Scanner.SkipSpaces(s, ref cc);
int bcc = cc;
while ((cc < s.Length) && Scanner.IsLAlpha(s[cc])) ++cc;
string type = s.Substring(bcc, cc - bcc);
if (type == "meta")
{
cc -= 4;
return String.Empty;
}
return type;
}
private static string ReadWildcardMetadata(string s, ref int cc)
{
Scanner.SkipSpaces(s, ref cc);
char[] meta = new char[]{'m','e','t','a'};
foreach(char c in meta){
if (Scanner.ReadChar(c, s, ref cc)) continue;
FindCloseBrace(s, ref cc);
return null;
}
Scanner.SkipSpaces(s, ref cc);
if (!Scanner.ReadChar(':', s, ref cc))
{
FindCloseBrace(s, ref cc);
return null;
}
int bcc = cc;
FindCloseBrace(s, ref cc);
return s.Substring(bcc, cc - bcc);
}
private static int ReadWildcardId(string s, ref int cc)
{
ushort usId;
Scanner.SkipSpaces(s, ref cc);
if (!Scanner.XtractUInt16(s, ref cc, out usId)) return -1;
return usId;
}
/// <summary>
/// Finds the close brace.
/// </summary>
/// <param name="s">string to look inside</param>
/// <param name="cc">Read header.
/// Must be pointing to the next character of an open brace within the string s</param>
/// <returns><c>true</c>, if closing brace par was found, <c>false</c> otherwise.</returns>
protected internal static bool FindCloseBrace(string s, ref int cc)
{
int braces = 1;
while ((cc < s.Length) && (braces > 0))
{
if (s[cc] == '{') ++braces;
else if (s[cc] == '}') --braces;
++cc;
}
--cc;
return braces == 0;
}
#endregion
}
}