Introduction

Code Counter is a GUI tool developed using VC++, which can be used for counting number of source lines, commented lines and blank lines, mainly in a VC++ project. For VC++ projects, the .dsp file will be parsed to get the list of files in the project. However, you can also use Code Counter for non VC++ projects like, a Java project or a C/C++ project developed on UNIX etc., by giving the list of files, in a .map text file. The Code Counter results can be stored in CSV/txt format, and can also be printed.

Using the Tool

The first thing to do is to select a project. On clicking the "Select Project" button, the file open dialog appears, and you can select either a .dsp file or .map file here. Once the project is selected, selection can be made in the tree checklist box, for counting in required files. The result of Code Counting will be displayed in the list control, which can be saved to a .csv file, or .txt file and can also be printed.

Implementation

Code Counter is developed as an MFC application using VC++ 6.0. The implementation mainly consists of classes for reading/parsing the .dsp file for getting the list of source files in a project, the tree check list box for allowing file selection, and parsing source file for checking comments.

Reading .dsp file & Parsing source file for comments

The CLnCountFile class implements these functionalities. The .dsp file is a text file which is read and parsed using some string manipulation functions. Source files will be succeeded by a special tag "# Begin Source File" in the .dsp file. If .map file is selected, no special parsing is required, as the file directly contains the list of files. In this case, the files should be given along with their absolute path, unless the map file resides in the directory where all the files are stored. If one or more source files could not be opened, then an error dialog will be displayed showing the list of files which could not be opened for parsing.

Once the list of files is got, then each file selected in the tree will be parsed for comments, blank lines and source lines. Both C++ and C style comments will be considered here. Also, if a line contains source code as well as comment, it will be considered as a source line.

Tree CheckList box

The file check/uncheck can be done in the tree control for selecting/deselecting files for code counting. The CTreeCheckListBox class implements this functionality by deriving itself from CTreeCtrl class.

.map file

In case of VC++ projects developed using Microsoft developer studio, the .dsp file can be parsed for getting the list of files in the project. But Code Counter uses a .map file for counting lines in C/C++/Java projects developed in other environments/platforms. For example: we can prepare a .map file for counting lines in a C/C++ project developed on UNIX, after copying the files using FTP. This file is a simple text file, containing the list of files in the project. The file names can be given with absolute path necessary. A sample .map file is given below. (This sample .map file is for CodeCounter project itself. Here the absolute paths are not required, if the map file is stored in the source directory. However, giving absolute path does not lead to any problems.)

Conclusion

The Code Counter can be used at the end of project development, to get the exact amount of source written for the project. Hence this may serve as a good productivity tool. Also, using this tool, one can measure how much of code has been written in a day or a week, by storing each code counting result in an external file. Code counter can also put a measure on how much comments are provided in the project.

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.

while overwriting the existing details , if the new strings are lesser in size than the existing one, it is putting some box like characters at the last of that spaecific record . could anyone help me...

Hi Prashanth. Good program and code. . I also just wanted to give another link to a windows-based code counter which counts C/C++ and C# files as well as many other types of languages. http://www.geronesoft.com/ (Code Counter Pro).

It doesn't read the map files too and doesn't remove source safe comments but it's a good, easy to use tool to have around when you need it.

Nice tool. I was wondering if there was a tool out there that, using ClearCase, can find the delta code of a project. By delta code, I mean the amount of lines added, deleted, and modified since a certain date.

snout,
Have you looked into EZ-Metrix for delta code counts? It doesn't leverage ClearCase, but rather it compares two files (or directories of files) an calculates the number of changed, new, deleted and unchanged SLOC. It's a web-based tool (IE only) that you can point at your two source files through your file system (local or network drives both work) and it does the rest.

James Heires, PMP
President
James Heires Consulting, Inc.
Home of EZ-Metrix(tm) code counting utility
http://www.ez-metrix.com
jtheires@netins.net

Nice program, but (as in most cases) there is a similar tool (namely PLC, Project Line Counter) already available at WndTabs.com by Oz Solomonovich. This does reporting not only by exporting cvs/txt, but customizable HTML pages as well. But AFAIK it cannot parse .map files, so this is a feature only your tool is providing

Disclaimer:Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.

Brian V Shifrin wrote:That it we can not have another one. That's it. Everyone is banned from writing Line Counters.

That's not what I meant. Maybe my comment has been misleading. I don't want to degrade Prashanth Uppunda's work, but point to alternatives. Everyone is free to choose that tool which fits his needs best.

E.g. when I have to develop a certain functionality, I first look around at CodeProject or CodeGuru. Then I choose that one, which is closest to my needs. Sometimes I took the best parts of all found solutions and paste them together

Brian V Shifrin wrote:Why is it ok to have 1,000,000 toolbar/rebar or list classes

Disclaimer:Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.

I developed this tool, couple of years back, when I needed to count the line of code in some existing UNIX based projects. However now, I thought, before putting it to CodeProject, let us support the basic things also (like .dsp). Anyway sorry for putting a functionality which was allready existing in another tool.