Click here to Skip to main content
15,889,839 members
Articles / Programming Languages / C#

Simple Source Line Counter in C# for C#

Rate me:
Please Sign up or sign in to vote.
4.25/5 (28 votes)
24 Jan 20053 min read 102.6K   3.4K   28  
A simple source code line counter written in C#.
using System;
using System.IO;
using System.Collections;

namespace CSharpLineCounter
{
	public class CodeFileModel: blix.ListViewModel
	{
		#region Fields
		private string _SourceDirectory;
		private FileStatCollection _SourceFiles;
		private int _TotalLineCount;
		private int _SourceLineCount;
		private Brain _Brain;
		private bool _Thinking;

		private ModelSourceFileFoundDelegate _SourceFileFoundDelegate;
		#endregion

		#region CodeFileModel()
		public CodeFileModel()
		{
			Columns.Add(new blix.ListViewColumn("Name", typeof(string), 300));
			Columns.Add(new blix.ListViewColumn("Count", typeof(int), 50, blix.StandardTextAlignments.RightCenter));

			_SourceFiles = new FileStatCollection();
		}
		#endregion
		#region Populate(aSourceDirectory)
		public void Populate(string aSourceDirectory)
		{
			_Thinking = true;

			try
			{
				_SourceDirectory = aSourceDirectory;

				FindSourceFiles(_SourceDirectory);

				_SourceFiles.Sort();

				TriggerPopulated();
			}
			finally
			{
				_Thinking = false;
			}
		}
		#endregion
	
		#region FindSourceFiles
		public void FindSourceFiles(string aSourceDirectory)
		{
			string[] directories = Directory.GetDirectories(aSourceDirectory);

			if (directories.Length > 0)
			{
				foreach (string directory in directories)
					FindSourceFiles(directory);
			}

			string[] fileNames = Directory.GetFiles(aSourceDirectory, "*.cs");

			foreach (string fileName in fileNames)
			{
				FileStat fileStat = new FileStat(fileName);

				CountLines(fileStat);

				_SourceFiles.Add(fileStat);

				_SourceLineCount += fileStat.SourceLineCount;
				_TotalLineCount += fileStat.LineCount;

				TriggerSourceFileFound(fileStat);
			}	
		}
		#endregion
		#region CountLines
		private void CountLines(FileStat aFileStat)
		{
			StreamReader streamReader = new StreamReader(aFileStat.FileName);	

			try
			{
				while (streamReader.Peek() != -1)
				{
					string line = streamReader.ReadLine();

					line.Trim();

					aFileStat.LineCount++;

					if (_Brain.IsSource(line))
						aFileStat.SourceLineCount++;
				}
			}
			finally
			{
				streamReader.Close();
			}

			_Brain.Clear();
		}
		#endregion

		#region Clear
		public void Clear()
		{
			_SourceLineCount = 0;
			_TotalLineCount = 0;

			_SourceFiles.Clear();

			_SourceDirectory = String.Empty;

			TriggerRowsCleared();
		}
		#endregion
		#region GetRows
		public override System.Collections.IList GetRows()
		{
			return _SourceFiles;
		}
		#endregion
		#region GetRowValues
		public override System.Collections.IList GetRowValues(object aRow)
		{
			int index = _SourceFiles.IndexOf((FileStat) aRow);

			FileStat fileStat = _SourceFiles[index];

			ArrayList values = new ArrayList();

			values.Add(fileStat.Name);
			values.Add(fileStat.SourceLineCount);

			return values;
		}
		#endregion

		#region TotalLineCount
		public int TotalLineCount
		{
			get
			{
				return _TotalLineCount;
			}
		}
		#endregion
		#region SourceLineCount
		public int SourceLineCount
		{
			get
			{
				return _SourceLineCount;
			}
		}
		#endregion
		#region FileCount
		public int FileCount
		{
			get
			{
				return _SourceFiles.Count;
			}
		}
		#endregion

		#region SourceFileFound
		#region trigger SourceFileFound
		private void TriggerSourceFileFound(FileStat aFileStat)
		{
			if (_SourceFileFoundDelegate != null)
				_SourceFileFoundDelegate(aFileStat);
		}
		#endregion
		#region event SourceFileFound
		public event ModelSourceFileFoundDelegate SourceFileFound
		{
			add
			{
				_SourceFileFoundDelegate = (ModelSourceFileFoundDelegate) blix.Delegator.Add(_SourceFileFoundDelegate, value);
			}
			remove
			{
				_SourceFileFoundDelegate = (ModelSourceFileFoundDelegate) blix.Delegator.Remove(_SourceFileFoundDelegate, value);
			}
		}
		#endregion
		#endregion

		#region Brain
		public Brain Brain
		{
			get
			{
				return _Brain;
			}
			set
			{
				#region Precondition
				if (_Thinking)
					throw new Exception(GetType().FullName + "set_Brain(): Please do not swap my Brain while I'm thinking.");
				#endregion

				if (value == null)
					_Brain = new Simpleton();
				else
					_Brain = value;
			}
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO Sagerion, LLC
United States United States
I read About Face by Alan Cooper in 1995 and immediately recognized it as a founding document for the future of software. I also recognized we had a long, long way to go - and yes, even with the advent of iOS, we are still not there yet.

At my company, Sagerion (say-jair-ee-on), we can take a look at your planned or existing software and suggest ways of making it better - lots better. We can develop down-to-the-pixel blueprints showing exactly what our suggestions mean. We can help manage on-going development to make sure the top-notch user-experience we've suggested really does get built. Now, honestly, how often have you ever seen all those things happen?

You may or may not already have great development going on - but what does that matter if you don't have great design driving it?

Feel free to contact me at tom@sagerion.com, I would love to hear about your next ground-breaking project.

Comments and Discussions