Monday, July 19, 2010

A compact PC-Lint error output file parser

This code has been hanging around in my drafts folder for quite some time now. I needed to get it out. I wrote this simple parser for extracting information from the .out file PC-Lint provides as output after its analysis. This information will further be fed to a VS add-in that would integrate this content with the editor. It's going to be a cool one! I'm going to setup the alpha version of the add-in next month on Google Code.

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