Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#

Extensible Line Code Counter

Rate me:
Please Sign up or sign in to vote.
2.92/5 (5 votes)
2 Jan 2009LGPL36 min read 37K   7   13
Utility for counting lines of source code. It can easily be extended to count any source code types

History/Inception

Despite google, despite all of the other code sites, I cannot seem to find a program that reliable counts the number of code lines in my projects. Given my interesting in making small flexible tools, I decided it was time to build my own program for counting lines of code.


Downloads

Binaries
Source Code
Documentation (word .doc)
VB Extension Example


Design 

From the start, I wanted to build a code line counter that was flexible. So I separated the logic that tabulates the counts from the logic that determines what is “countable”. I also made the logic that determines what is countable, loaded at runtime. These two decisions make for a dynamic and flexible utility program. CodeCounter is easily extended to process other source code types, such as fortran. It is as simple as implementing a few functions defined in an interface.


What does CodeCounter do

It counts the lines of code in source files. Current source code type supported are C#, Xaml and SQL. CodeCounter can easily be extended to count source code from other file types. Read on to learn how.


Installation

If you downloaded the binaries, you need to copy all of the files in the ZIP into some location. I suggest some where in your path so that the program is always accessible. In the binary zip file you should have found the following files:


Big Woo.NET.dll  

CodeCounter.exe

CodeCounter.exe.config

CodeCounter.Library.dll


All of the files need to be copied into the same location. For me, I copied my files to C:\Program Files\tools. You should pick what works best for you.


Using CodeCounter

From the command line, just run the program with no inputs:

c:\src>CodeCounter.exe

CodeCounter by matt raffel

(c) 2009 - matt.raffel@mindspring.com

-verbose

-recurse

-help

-supportedTypes

-about

{filename}

FileName can be full filename or partial with wild card


As you can see, there's not much. Pretty simple. Here's what the commands mean and do:


-verbose

by default the only information shown on the screen is the results of the line counting. Errors are also displayed, of course. When including this option, detail information is listed, listing each file found.

-recurse

by default CodeCounter ingores subdirectories. Recurse turns off this behavior indicating searches to include subdirectories looking for matching input.

-help

shows the above itty bitty help

{filename}

this is the important input. {filename} is the file or wildcard file mask of the files you want counted. You can have more than one {filename} input. {filename} can be relative or literal path to a file and include any of the standard windows wildcards.

-about

Opens the webpage http://www.mattraffel.com/aboutme.aspx

-supportedTypes

Lists the file types that CodeCounter is configured to process, and it shows the class responsible for the file type processing


The command line arguments can be entered in any order. The only command argument that is required is the filenames.

 

Examples

C:\src>CodeCounter *.cs

This example expects all C# files in the current directory to be counted. C# files in subdirectories will not be counted.

C:\src>CodeCounter test.cs

This example says to count the code for test.cs in the current directory and that's it.

C:\src>CodeCounter test.cs *.sql -recurse

This command expects test.cs and all .sql files, including those in subdirectories to be counted. There is a little bit of a surprise here, in that all test.cs files will be counted.

C:\src>CodeCounter test.cs *.sql -verbose

Same as the previous command, except the amount of output display is significantly more.


Extending CodeCounter

One of the key design features of my utility is the ease of extending CodeCounter to include additional file types.

If you are familiar with .NET, and particular C#, then you will find extending codecounter is very simple.

Before you get started, you might want to get the latest code first. You do not need the code, but it might be more instructive to look at the C# code counting implementation (class CSharpCodeCounterLogic) for understanding how to implement your own code counter.

In summary:

  1. To start, create your own .NET project.

  2. Add a reference to the CodeCounter.Library.dll.

  3. Create a new class that implements the ICodeCounterLogic interface.

  4. Write your code, build the assembly.

  5. Copy the assembly, preferrable to the same location as CodeCounter.exe.

  6. Edit CodeCounter.exe.config, updating the codeCounters section with your new information.


Caution: I would caution against adding your types to CodeCounter.Library.dll as any updates from me would cause you to lose your changes. I encourage you to put your ICodeCounterLogic implementation in its own assembly.


The ICodeCounterLogic interface


CodeCounterLineType LineType(string line)

This is the logic method of the interface. The engine calls this method for each line read in from the a file.


Return: CodeCounterLineType

string[] FileTypesHandled()

Method defines the types supported by the interface implementation. At a minimum, a one element array should be returned. This method is used by the help “engine”.


Return:string[]


Example: new string[] {"cs"};

bool CanProcessFile(string file)

Method determines if the interface implementation is able to process the input file. This method should examine the file to determine if the file contains code it understands. In most cases, evaluating file name extension might be sufficent.


Return: false or true

bool EngineCanDetermineBlankLines()

Currently unused method.

void PrefileProcessing(string fileName)

Called by the engine prior to opening the file. This gives the interface implementation an opportunity to perform any additional work it might need prior to beginning line counting.


Caution: if you open the file, make sure you close it or you open it in shared mode, or the engine will not be able to open the file.

void PostfileProcessing(string fileName)

Called by the engine after the file is closed, and successfully counting the lines of the file.



The CodeCounterLineType enum


EmptyLine

Indicates an empty Line

Code

Indicates the line contains code only

CodeAndComment

Indicates the line contains both code and comments

CommentOnly

Indicates the line is a comment

Statement

Indicates a statement.  

StatementAndComment

Indicates a statement followed by a comment




The codecounter.exe.config file

Once you have your assembly built (and assuming unit tested), you will want to configure CodeCounter to include your assembly. This is done by editing the CodeCounter config file, adding your assembly to its collection of assembles.

<counterImplementations>

<logic key="CS"

assemblyInformation=

"CSharpCodeCounterLogic, CodeCounter.Library.CSharpCodeCounterLogic, CodeCounter.Library.dll"/>

</counterImplementations> 


Look for the counterImplementations section in the config file. You will want to duplicate one of the existing lines in this section. Copy or create a new “logic” line. For key, you can put anything you like as long as it is unique. Since file extension is unique, I have been using that.

For assemblyInformation, you enter the information about your assembly. The format of this field is familiar, however I have to warn you it does not conform to the standards. You need to enter, in comma delimited fashion, the class name, the class name with complete namespace, and the assembly name.

In the example above, the class name is CSharpCodeCounterLogic. The class name with namespace is CodeCounter.Library.CSharpCodeCounterLogic. And the assemby name is CodeCounter.Library.dll.

For the assembly name, if you exclude a path, it will assume the assembly is in the same folder as CodeCounter.exe.

 

VB Extension Example 

If you download the VB extension example, you will find an updated CodeCounter able to count lines of code in VB.NET files (*.vb).    Source code included!  :) 

I feel the VB example shows how simple it is to add new source code types to CodeCounter.


I hope you find my utility program useful.   I look forward to hearing from you.

 

Update 

2009.01.03 - I updated to the code (code only), to separate the reporting of lines and statements.


License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionBroken download links Pin
blueacorn20-May-14 12:46
professionalblueacorn20-May-14 12:46 
AnswerRe: Broken download links Pin
mattraffel3-Sep-14 7:19
mattraffel3-Sep-14 7:19 
GeneralMy vote of 4 Pin
blueacorn10-Oct-13 13:55
professionalblueacorn10-Oct-13 13:55 
GeneralPointless Pin
PIEBALDconsult2-Jan-09 12:27
mvePIEBALDconsult2-Jan-09 12:27 
GeneralRe: Pointless Pin
DarrenShultz2-Jan-09 12:54
DarrenShultz2-Jan-09 12:54 
GeneralRe: Pointless Pin
PIEBALDconsult2-Jan-09 14:34
mvePIEBALDconsult2-Jan-09 14:34 
GeneralRe: Pointless Pin
AndrewSmith2-Jan-09 14:57
AndrewSmith2-Jan-09 14:57 
GeneralRe: Pointless Pin
PIEBALDconsult2-Jan-09 15:30
mvePIEBALDconsult2-Jan-09 15:30 
GeneralRe: Pointless [modified] Pin
mattraffel3-Jan-09 4:40
mattraffel3-Jan-09 4:40 
GeneralRe: Pointless Pin
PIEBALDconsult3-Jan-09 5:32
mvePIEBALDconsult3-Jan-09 5:32 
GeneralRe: Pointless Pin
mattraffel5-Jan-09 12:45
mattraffel5-Jan-09 12:45 
PIEBALDconsult wrote:
You need to present and explain your code in the article; otherwise few people here (including me) will download and look at the code.


Thnx for the feedback. I had hoped that the design and Extending Code counter sections were helpful. I will work to provide some better information for readers.

Thnx
Matt
QuestionHow about ... Pin
sam.hill2-Jan-09 11:29
sam.hill2-Jan-09 11:29 
AnswerRe: How about ... Pin
mattraffel3-Jan-09 4:49
mattraffel3-Jan-09 4:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.