Source Line of Code Counter
Since I couldn't find a SLOC (source line of code) counter that satisfies me, I decided to write one. This SLOC counter equipped with a file system browser which is very handy for precisely defining inclusion and exclusion of files/directories. Besides that, the SLOC counter supports pattern matching for file inclusion and exclusion, and directory inclusion using regular expression. The usage section of this post has a detailed list on how to use pattern match.
This LOC counter is written in Visual C# 2005. That means you need Microsoft .NET Framework 2.0 to run this program. You can download a free copy of .NET Framework from here.
- File/Directory browser for selecting multiple project location
- Regular expression pattern match
- Counting results can be sorted by file name or line count
- Export counting results to file
- Custom counting rules can be defined
Select a project directory and press the Start button to start counting.
You can select a counting rule to refine the counting result, e.g., to strip out comments for designated languages.
In SLOC counter, one can define patterns through regular expression to filter unwanted files. To define multiple patterns, use comma to separate each pattern. Below are examples on how to use patterns.
If you are new to regular express, this site has some excellent tutorials to get you started.
Include only files with extension .php, .html or .css:
Exclude subversion directory:
Include files which file names contain "config" and not under directories which names contain ".svn":
Custom Counting Rules
Counting rule define how lines in a file are counted. Each counting rule is defined by a regular expression. In the event that the line in the file which is being counted is matched, this line will be excluded from counting. Typical scenario for using counting rule is to exclude comments.
There are two types of counting rules: line and page, stored in <name>.line.conf and <name>.page.conf respectively, where <name> is the name of the counting rule.
In line counting rule, each line represents a counting rule in the form of regular expression. If the line in the source file matches any of the rules defined in <name>.line.conf, the this source code line will be ignored from counting.
Page counting rules are defined in a similar way except they are paired. In
Here's counting rules for PHP language are defined:
^ *<\?php *$ ^ *\/\/.* ^ *$ ^ *\?> *$
^ *\/\* \*\/ *$
Rules in php.line.conf tells the program to ignores PHP tags <php and ?>, and single line comment //.
Rules in php.page.conf tells the program to ignores multiline comment which denoted by /* */.
Creating Your Own Counting Rule
Create a counting rule in addition to bundled ones is easy. All you need to do is create these two files under the directory where you launch your application: