/// <summary> /// Adds a file information item to the file list view. /// </summary> /// <param name="info">The file information data object.</param> private void AddFileListItem(LineCountInfo info) { FileInfo fileInfo = new FileInfo(info.FileName); ListViewItem lvi = new ListViewItem(); lvi.Text = fileInfo.Name; lvi.SubItems.Add(info.LineCountInfoDetails.TotalLines.ToString()); lvi.SubItems.Add(info.LineCountInfoDetails.CodeLines.ToString()); lvi.SubItems.Add(info.LineCountInfoDetails.CommentLines.ToString()); lvi.SubItems.Add(info.FileType); lvi.SubItems.Add(info.SumMode); lvi.Tag = info; lvi.ImageIndex = info.IconIndex; //lvi.StateImageIndex = iconIndex; if (tsmiGroupByType.Checked) { ListViewGroup group = lvFileList.Groups["groupType" + info.FileType.Substring(1)]; if (group == null) { group = new ListViewGroup("groupType" + info.FileType.Substring(1), info.FileType.Substring(1).ToUpper() + " Files"); lvFileList.Groups.Add(group); } lvi.Group = group; } else if (tsmiGroupByProj.Checked) { ListViewGroup group = lvFileList.Groups["groupProj" + info.ProjectSummary.ProjectName]; if (group == null) { group = new ListViewGroup("groupProj" + info.ProjectSummary.ProjectName, info.ProjectSummary.ProjectName + " Files"); lvFileList.Groups.Add(group); } lvi.Group = group; } lvFileList.Items.Add(lvi); }
/// <summary> /// Count each line in a text file, logging /// blank lines. /// </summary> /// <param name="info">The file information data to use.</param> internal static void CountLinesGeneric(LineCountInfo info) { StreamReader sr = new StreamReader(info.FileName); string line; while ((line = sr.ReadLine()) != null) { info.LineCountInfoDetails.Add("TotalLines", 1); if (line.Trim() == string.Empty) { info.LineCountInfoDetails.Add("BlankLines", 1); } info.SumMode = "Generic"; } sr.Close(); }
/// <summary> /// Count each line in an xml source file, scanning /// for comments, code, and blank lines. /// </summary> /// <param name="info">The file information data to use.</param> /// <remarks>This algorithm is based on one created by Oz Solomon, /// for his PLC line counter add-in for Visual Studio 2002/2003.</remarks> internal static void CountLinesXMLStyle(LineCountInfo info) { try { StreamReader reader = new StreamReader(info.FileName); string line; bool multiLineComment = false; bool hasCode = false; bool hasComments = false; while ((line = reader.ReadLine()) != null) { ParseXMLLine(line, ref multiLineComment, ref hasCode, ref hasComments); if (hasComments) { info.LineCountInfoDetails.Add("normcmtlines", 1); } if (hasCode) { info.LineCountInfoDetails.Add("codelines", 1); } if (!hasCode && !hasComments) { info.LineCountInfoDetails.Add("blanklines", 1); } info.LineCountInfoDetails.Add("totallines", 1); } reader.Close(); info.SumMode = "XML"; } catch { } }
/// <summary> /// Performs a complete counting and summation of all lines /// in all projects and files. /// </summary> private void SumSolution() { try { // Clean the list lvSummary.Items.Clear(); lvFileList.Items.Clear(); lvFileList.Groups.Clear(); // Configure progress bars tsprgTotal.Minimum = 0; tsprgTotal.Step = 1; tsprgTask.Minimum = 0; tsprgTask.Step = 1; // Skip if there are no projects if (m_summaryList == null || (m_summaryList != null && m_summaryList.Count == 1)) { MessageBox.Show("There are no projects loaded to summarize.", "Line Counter"); return; } // Get all projects summary LineCountSummary allProjects = m_summaryList[0]; allProjects.LineCountSummaryDetails.Reset(); AddSummaryListItem(allProjects, lvSummary.Groups["lvgAllProj"]); tsprgTotal.Maximum = m_summaryList.Count; tsprgTotal.Value = 0; for (int s = 1; s < m_summaryList.Count; s++) { tsprgTotal.PerformStep(); LineCountSummary summary = m_summaryList[s]; summary.LineCountSummaryDetails.Reset(); AddSummaryListItem(summary, lvSummary.Groups["lvgEachProj"]); tsprgTask.Maximum = summary.FileLineCountInfo.Count; tsprgTask.Value = 0; for (int i = 0; i < summary.FileLineCountInfo.Count; i++) { tsprgTask.PerformStep(); LineCountInfo info = summary.FileLineCountInfo[i]; if (m_countableTypes.Contains(info.FileType)) { info.LineCountInfoDetails.Reset(); #if IMPR2 foreach (CountingAlgorithmDescriptor desc in countingAlgorithms) { if (desc.CanCountLines(info)) { desc.GetAlgorithm().CountLines(info); break; } } #else try { CountLines counter = m_countAlgorithms[info.FileType]; counter(info); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } #endif info.LineCountInfoDetails.Summarize(); allProjects.LineCountSummaryDetails.Add(info.LineCountInfoDetails); summary.LineCountSummaryDetails.Add(info.LineCountInfoDetails); tstxtLinesCounted.Text = allProjects.LineCountSummaryDetails.TotalLines.ToString(); AddFileListItem(info); } } summary.LineCountSummaryDetails.Summarize(); LineCountDetails details = summary.LineCountSummaryDetails; summary.LinkedListViewItem.SubItems[1].Text = details.TotalLines.ToString(); summary.LinkedListViewItem.SubItems[2].Text = details.CodeLines.ToString(); summary.LinkedListViewItem.SubItems[3].Text = details.CommentLines.ToString(); summary.LinkedListViewItem.SubItems[4].Text = details.BlankLines.ToString(); summary.LinkedListViewItem.SubItems[5].Text = details.NetLines.ToString(); details = null; } allProjects.LineCountSummaryDetails.Summarize(); LineCountDetails totals = allProjects.LineCountSummaryDetails; allProjects.LinkedListViewItem.SubItems[1].Text = totals.TotalLines.ToString(); allProjects.LinkedListViewItem.SubItems[2].Text = totals.CodeLines.ToString(); allProjects.LinkedListViewItem.SubItems[3].Text = totals.CommentLines.ToString(); allProjects.LinkedListViewItem.SubItems[4].Text = totals.BlankLines.ToString(); allProjects.LinkedListViewItem.SubItems[5].Text = totals.NetLines.ToString(); } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); } tsprgTotal.Value = tsprgTotal.Maximum; }
public bool CanCountLines(LineCountInfo info) { return(Array.IndexOf(extensions, info.FileType.ToLowerInvariant()) >= 0); }
public void CountLines(LineCountInfo info) { LineCounterBrowser.CountLinesXMLStyle(info); }
public void CountLines(LineCountInfo info) { LineCounterBrowser.CountLinesGeneric(info); }
public bool CanCountLines(LineCountInfo info) { return (Array.IndexOf(extensions, info.FileType.ToLowerInvariant()) >= 0); }