-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.cs
More file actions
39 lines (35 loc) · 1.48 KB
/
Common.cs
File metadata and controls
39 lines (35 loc) · 1.48 KB
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
using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace TianWen.DAL
{
public static class Common
{
private static readonly Regex VersionParser = new Regex(@"(\d+)(?:[,]?\s*)?",
RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
/// <summary>
/// Returns SDK version with this format: <code>1, 51, [1], [0]</code>
/// </summary>
/// <returns></returns>
public static Version ParseVersionString(IntPtr charPtr)
{
var verStr = Marshal.PtrToStringAnsi(charPtr);
if (string.IsNullOrEmpty(verStr))
{
return new Version();
}
else
{
var matches = VersionParser.Matches(verStr);
switch (matches.Count)
{
case 2: return new Version(VersionPart(matches, 0), VersionPart(matches, 1));
case 3: return new Version(VersionPart(matches, 0), VersionPart(matches, 1), VersionPart(matches, 2));
case 4: return new Version(VersionPart(matches, 0), VersionPart(matches, 1), VersionPart(matches, 2), VersionPart(matches, 3));
default: return new Version();
}
}
}
static int VersionPart(MatchCollection matches, int part) => Convert.ToInt32(matches[part].Groups[1].Value);
}
}