예제 #1
0
        public void ParseInput([Option("f", "path of the file to parse")] string path, [Option("o", "output file name")] string fileName)
        {
            var content    = File.ReadAllText(path);
            var traceAreas = content.Split("\r\n\r\n");
            var traces     = traceAreas.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Split("\r\n"));

            var results = new List <ParsingResult>();

            foreach (var trace in traces)
            {
                var result = new ParsingResult();
                for (int i = 1; i < trace.Length; i++)
                {
                    if (!hostMatcher.IsMatch(trace[i]))
                    {
                        result.Hops.Add(new Hop()
                        {
                            Host    = "*",
                            Address = "*"
                        });
                        continue;
                    }

                    var match = hostMatcher.Match(trace[i]);
                    if (i == 1)
                    {
                        result.Host    = match.Groups[2].Value;
                        result.Address = match.Groups[4].Value;
                    }
                    else
                    {
                        var hop = new Hop()
                        {
                            Host    = match.Groups[2].Value,
                            Address = match.Groups[4].Value
                        };
                        result.Hops.Add(hop);

                        var pingMatches = pingMatcher.Matches(trace[i]);
                        for (int j = 0; j < pingMatches.Count; j++)
                        {
                            if (j == 0)
                            {
                                hop.Ping1 = float.Parse(pingMatches[j].Groups[1].Value);
                            }
                            else if (j == 1)
                            {
                                hop.Ping2 = float.Parse(pingMatches[j].Groups[1].Value);
                            }
                            else if (j == 2)
                            {
                                hop.Ping3 = float.Parse(pingMatches[j].Groups[1].Value);
                            }
                        }
                    }
                }

                results.Add(result);
            }

            File.WriteAllText(fileName + ".json", JsonSerializer.Serialize(results));
        }