forked from kyordhel/GPSRCmdGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScanner.cs
318 lines (291 loc) · 10.4 KB
/
Scanner.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Collections.Generic;
using System.Text;
namespace GPSRCmdGen
{
/// <summary>
/// Provides methods for data parsing
/// </summary>
public static class Scanner
{
/// <summary>
/// Advances the read header to the next occurrence of the specified char
/// </summary>
/// <param name="c">The char to find</param>
/// <param name="input">The input string</param>
/// <param name="cc">Read header</param>
/// <returns>true if provided character c is found before the end of the string, false otherwise</returns>
public static bool AdvanceToChar(char c, string input, ref int cc)
{
while (cc < input.Length)
{
if (input[cc] == c)
return true;
++cc;
}
return false;
}
/// <summary>
/// Advances the read header to the next occurrence of the specified chars
/// </summary>
/// <param name="c">The array of chars to find</param>
/// <param name="input">The input string</param>
/// <param name="cc">Read header</param>
/// <returns>true if any of the provided characters in c is found before the end of the string, false otherwise</returns>
public static bool AdvanceToChar(char[] c, string input, ref int cc)
{
if ((c == null) || (c.Length < 1))
throw new ArgumentNullException();
List<char> lc = new List<char>(c);
while (cc < input.Length)
{
if (lc.Contains(input[cc]))
return true;
++cc;
}
return false;
}
/// <summary>
/// Reads the char from input at cc index and compares to the provided char c. If match advances the read header cc
/// </summary>
/// <param name="c">The expected char to read</param>
/// <param name="input">The input string</param>
/// <param name="cc">Read header</param>
/// <returns>true if provided character c matches the char at cc in input, false otherwise</returns>
public static bool ReadChar(char c, string input, ref int cc)
{
if ((cc < input.Length) && (input[cc] == c))
{
++cc;
return true;
}
return false;
}
/// <summary>
/// Advances the read header until no spaces are found
/// </summary>
/// <param name="s">Input string</param>
/// <param name="cc">Read header</param>
public static void SkipSpaces(string s, ref int cc)
{
if ((cc < 0) || (cc >= s.Length))
return;
while (Scanner.IsSpace(s[cc])) ++cc;
}
/// <summary>
/// Extracts an unsigned 32 bit integer from the input string
/// </summary>
/// <param name="input">String from which the integer will be extracted</param>
/// <param name="cc">The search starting position</param>
/// <param name="value">When this method returns contains the id found in s if the extraction succeded, or 0 if the extraction failed.</param>
/// <returns>True if the extraction succeded and a valid id was found in s starting at cc, false otherwise</returns>
public static bool XtractByte(string input, ref int cc, out byte value)
{
int bcc = cc;
int length;
string sValue;
value = 0;
while ((cc < input.Length) && IsAlNum(input[cc]))
++cc;
length = Math.Min(cc - bcc, input.Length - bcc);
if ((length < 1) || (length > 10))
return false;
sValue = input.Substring(bcc, length);
return Byte.TryParse(sValue, out value);
}
/// <summary>
/// Extracts an 32 bit integer from the input string
/// </summary>
/// <param name="input">String from which the integer will be extracted</param>
/// <param name="cc">The search starting position</param>
/// <param name="value">When this method returns contains the id found in s if the extraction succeded, or 0 if the extraction failed.</param>
/// <returns>True if the extraction succeded and a valid id was found in s starting at cc, false otherwise</returns>
public static bool XtractSByte(string input, ref int cc, out sbyte value)
{
int bcc = cc;
int length;
string sValue;
value = 0;
if ((cc < input.Length) && (input[cc] == '-'))
++cc;
while ((cc < input.Length) && IsNumeric(input[cc]))
++cc;
length = Math.Min(cc - bcc, input.Length - bcc);
if ((length < 1) || (length > 4))
return false;
sValue = input.Substring(bcc, length);
return SByte.TryParse(sValue, out value);
}
/// <summary>
/// Extracts an unsigned 32 bit integer from the input string
/// </summary>
/// <param name="input">String from which the integer will be extracted</param>
/// <param name="cc">The search starting position</param>
/// <param name="value">When this method returns contains the id found in s if the extraction succeded, or 0 if the extraction failed.</param>
/// <returns>True if the extraction succeded and a valid id was found in s starting at cc, false otherwise</returns>
public static bool XtractUInt16(string input, ref int cc, out ushort value)
{
int bcc = cc;
int length;
string sValue;
value = 0;
while ((cc < input.Length) && IsNumeric(input[cc]))
++cc;
length = Math.Min(cc - bcc, input.Length - bcc);
if ((length < 1) || (length > 5))
return false;
sValue = input.Substring(bcc, length);
return UInt16.TryParse(sValue, out value);
}
/// <summary>
/// Extracts an 32 bit integer from the input string
/// </summary>
/// <param name="input">String from which the integer will be extracted</param>
/// <param name="cc">The search starting position</param>
/// <param name="value">When this method returns contains the id found in s if the extraction succeded, or 0 if the extraction failed.</param>
/// <returns>True if the extraction succeded and a valid id was found in s starting at cc, false otherwise</returns>
public static bool XtractInt16(string input, ref int cc, out short value)
{
int bcc = cc;
int length;
string sValue;
value = 0;
if ((cc < input.Length) && (input[cc] == '-'))
++cc;
while ((cc < input.Length) && IsNumeric(input[cc]))
++cc;
length = Math.Min(cc - bcc, input.Length - bcc);
if ((length < 1) || (length > 6))
return false;
sValue = input.Substring(bcc, length);
return Int16.TryParse(sValue, out value);
}
/// <summary>
/// Extracts an unsigned 32 bit integer from the input string
/// </summary>
/// <param name="input">String from which the integer will be extracted</param>
/// <param name="cc">The search starting position</param>
/// <param name="value">When this method returns contains the id found in s if the extraction succeded, or 0 if the extraction failed.</param>
/// <returns>True if the extraction succeded and a valid id was found in s starting at cc, false otherwise</returns>
public static bool XtractUInt32(string input, ref int cc, out uint value)
{
int bcc = cc;
int length;
string sValue;
value = 0;
while ((cc < input.Length) && IsNumeric(input[cc]))
++cc;
length = Math.Min(cc - bcc, input.Length - bcc);
if ((length < 1) || (length > 10))
return false;
sValue = input.Substring(bcc, length);
return UInt32.TryParse(sValue, out value);
}
/// <summary>
/// Extracts an 32 bit integer from the input string
/// </summary>
/// <param name="input">String from which the integer will be extracted</param>
/// <param name="cc">The search starting position</param>
/// <param name="value">When this method returns contains the id found in s if the extraction succeded, or 0 if the extraction failed.</param>
/// <returns>True if the extraction succeded and a valid id was found in s starting at cc, false otherwise</returns>
public static bool XtractInt32(string input, ref int cc, out int value)
{
int bcc = cc;
int length;
string sValue;
value = 0;
if ((cc < input.Length) && (input[cc] == '-'))
++cc;
while ((cc < input.Length) && IsNumeric(input[cc]))
++cc;
length = Math.Min(cc - bcc, input.Length - bcc);
if ((length < 1) || (length > 11))
return false;
sValue = input.Substring(bcc, length);
return Int32.TryParse(sValue, out value);
}
/// <summary>
/// Indicates whether a ANSI character is letter or digit.
/// </summary>
/// <param name="c">A ASNI character</param>
/// <returns>true if c is a letter or digit; otherwise, false.</returns>
public static bool IsAlNum(char c)
{
return ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
}
/// <summary>
/// Indicates whether a ANSI character is letter.
/// </summary>
/// <param name="c">A ASNI character</param>
/// <returns>true if c is a letter; otherwise, false.</returns>
public static bool IsAlpha(char c)
{
return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
}
/// <summary>
/// Indicates whether a ANSI character is lower case letter.
/// </summary>
/// <param name="c">A ASNI character</param>
/// <returns>true if c is an lower case letter; otherwise, false.</returns>
public static bool IsLAlpha(char c)
{
return ((c >= 'a') && (c <= 'z'));
}
/// <summary>
/// Indicates whether a ANSI character is lower case letter or digit.
/// </summary>
/// <param name="c">A ASNI character</param>
/// <returns>true if c is an lower case letter or digit; otherwise, false.</returns>
public static bool IsLAlNum(char c)
{
return ((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'z'));
}
/// <summary>
/// Indicates whether a ANSI character is digit.
/// </summary>
/// <param name="c">A ASNI character</param>
/// <returns>true if c is a digit; otherwise, false.</returns>
public static bool IsNumeric(char c)
{
return ((c >= '0') && (c <= '9'));
}
/// <summary>
/// Indicates whether a ANSI character is \f, \n, \r, \t, \v, or space
/// </summary>
/// <param name="c">A ASNI character</param>
/// <returns>true if c is a space character; otherwise, false.</returns>
public static bool IsSpace(char c)
{
switch (c)
{
case ' ':
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
return true;
default:
return false;
}
}
/// <summary>
/// Indicates whether a ANSI character is upper case letter.
/// </summary>
/// <param name="c">A ASNI character</param>
/// <returns>true if c is an upper case letter; otherwise, false.</returns>
public static bool IsUAlpha(char c)
{
return ((c >= 'A') && (c <= 'Z'));
}
/// <summary>
/// Indicates whether a ANSI character is upper case letter or digit.
/// </summary>
/// <param name="c">A ASNI character</param>
/// <returns>true if c is an upper case letter or digit; otherwise, false.</returns>
public static bool IsUAlNum(char c)
{
return ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z'));
}
}
}