Here's the code.
public class PCLintOutputReader { public PCLintOutputReader(string filePath) { _content = File.ReadAllText(filePath); } public List<ErrorMessageInfo> ParseTextErrorMessages() { const string errorPatternAsString = @"T:\s*(?<Type>\w*)\r\n" + @"N:\s*(?<ErrorNumber>[0-9]*)\r\n" + @"F:\s*(?<FileName>.*)\r\n" + @"L:\s*(?<LineNumber>[0-9]*)\r\n" + @"M:\s*(?<Message>.*)\r\n"; Regex errorPattern = new Regex(errorPatternAsString, RegexOptions.Compiled | RegexOptions.IgnoreCase); var errorMessages = new List<ErrorMessageInfo>(); var matches = errorPattern.Matches(_content); foreach (Match match in matches) { GroupCollection groups = match.Groups; errorMessages.Add( ErrorMessageInfo.Parse(groups["Type"].Value, groups["ErrorNumber"].Value, groups["FileName"].Value, groups["LineNumber"].Value, groups["Message"].Value) ); } return errorMessages; } private string _content; } public class ErrorMessageInfo { public ErrorMessageInfo() { } public MessageType Type { get; set; } public int ErrorNumber { get; set; } public string FileName { get; set; } public int LineNumber { get; set; } public string Message { get; set; } public static ErrorMessageInfo Parse(string aType, string aErrorNumber, string aFileName, string aLineNumber, string aMessage) { ErrorMessageInfo errorMessage = new ErrorMessageInfo(); switch (aType) { case "Info": errorMessage.Type = MessageType.Info; break; case "Warning": errorMessage.Type = MessageType.Warning; break; case "Error": errorMessage.Type = MessageType.Error; break; default: throw new ArgumentException("Could not identify the type of PC-Lint error.", aType); } int temp = 0; int.TryParse(aErrorNumber, out temp); errorMessage.ErrorNumber = temp; int.TryParse(aLineNumber, out temp); errorMessage.LineNumber = temp; errorMessage.FileName = aFileName; errorMessage.Message = aMessage; return errorMessage; } } public enum MessageType { Info, Warning, Error }
Disclaimer: This is a very amateur implementation of the solution. Kindly use the code at your own risk and don't blame me if your laptop bursts into flames. Please forgive any obvious coding convention errors. And let me know if you find any bugs.
Have fun!
No comments:
Post a Comment