Projekt

Obecné

Profil

Stáhnout (1.9 KB) Statistiky
| Větev: | Tag: | Revize:
1 bab6f59d silhavyj
using System.Text.RegularExpressions;
2
3
namespace LDClient.detection {
4
5 f63d5489 silhavyj
    /// <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 bab6f59d silhavyj
    public static class DebuggerInfoParser {
10
11 f63d5489 silhavyj
        /// <summary>
12
        /// Number of serial numbers expected to be in the .txt file (number of matches - regex).
13
        /// </summary>
14 bab6f59d silhavyj
        private const int ExpectedNumberOfMatches = 2;
15
        
16 f63d5489 silhavyj
        /// <summary>
17
        /// Regular expression used to find the serial numbers.
18
        /// </summary>
19 bab6f59d silhavyj
        private static readonly Regex SerialNumberRegex = new("(?<=Serial Number: )(.*)");
20
        
21 f63d5489 silhavyj
        /// <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 bab6f59d silhavyj
        public static (string headSerialNumber, string bodySerialNumber) Parse(string dataTxt) {
29 f63d5489 silhavyj
            // Find all matches in the content of the file that satisfy the regular expression.
30 bab6f59d silhavyj
            var matches = SerialNumberRegex.Matches(dataTxt);
31
32 f63d5489 silhavyj
            // Make sure an exact number of matches has been found.
33 bab6f59d silhavyj
            if (matches.Count != ExpectedNumberOfMatches) {
34
                throw new ArgumentException($"Expected {ExpectedNumberOfMatches} matches to be found in the text (actually found: {matches.Count})");
35
            }
36
            
37 f63d5489 silhavyj
            // Return the two serial numbers (head and body).
38 0932a9e2 Pultak
            return (matches[1].ToString().Trim(), matches[0].ToString().Trim());
39 bab6f59d silhavyj
        }
40
    }
41
}