Projekt

Obecné

Profil

Stáhnout (1.77 KB) Statistiky
| Větev: | Tag: | Revize:
1 bab6f59d silhavyj
using System.Text.RegularExpressions;
2
3 e6a01bd8 Pultak
namespace LDClient.detection; 
4
5
/// <summary>
6
/// This class parses the .txt file generated from the debugger.
7
/// Its primary interest is to find two serial numbers (head + body). 
8
/// </summary>
9
public static class DebuggerInfoParser {
10 bab6f59d silhavyj
11 f63d5489 silhavyj
    /// <summary>
12 e6a01bd8 Pultak
    /// Number of serial numbers expected to be in the .txt file (number of matches - regex).
13 f63d5489 silhavyj
    /// </summary>
14 e6a01bd8 Pultak
    private const int ExpectedNumberOfMatches = 2;
15 bab6f59d silhavyj
        
16 e6a01bd8 Pultak
    /// <summary>
17
    /// Regular expression used to find the serial numbers.
18
    /// </summary>
19
    private static readonly Regex SerialNumberRegex = new("(?<=Serial Number: )(.*)");
20 bab6f59d silhavyj
        
21 e6a01bd8 Pultak
    /// <summary>
22
    /// Takes the content of a .txt file and tries to find the two serial numbers (head and body).
23
    /// If it succeed, it will return the two numbers.
24
    /// </summary>
25
    /// <param name="dataTxt">the content of a .txt file (generated from the debugger)</param>
26
    /// <returns>two serial numbers (head and body) of the debugger</returns>
27
    /// <exception cref="ArgumentException">throws an exception if it fails to find the serial numbers</exception>
28
    public static (string headSerialNumber, string bodySerialNumber) Parse(string dataTxt) {
29
        // Find all matches in the content of the file that satisfy the regular expression.
30
        var matches = SerialNumberRegex.Matches(dataTxt);
31 bab6f59d silhavyj
32 e6a01bd8 Pultak
        // Make sure an exact number of matches has been found.
33
        if (matches.Count != ExpectedNumberOfMatches) {
34
            throw new ArgumentException($"Expected {ExpectedNumberOfMatches} matches to be found in the text (actually found: {matches.Count})");
35 bab6f59d silhavyj
        }
36 e6a01bd8 Pultak
            
37
        // Return the two serial numbers (head and body).
38
        return (matches[1].ToString().Trim(), matches[0].ToString().Trim());
39 bab6f59d silhavyj
    }
40
}