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.
BOOL CLnCountFile::GetFileList(CString strMakFile)
{
CString strTemp;
CStdioFile makFile(strMakFile,CFile::modeRead|CFile::typeText );
if(makFile.m_pStream==NULL)
{
AfxMessageBox("Can not Open Project Make file");
return FALSE;
}
m_aryFiles.RemoveAll();
m_aryErrorFiles.RemoveAll();
if(strMakFile.Find(".map") != -1)
{
AddFilesToArrayForGeneric(makFile);
}
else
{
while(makFile.ReadString(strTemp)!=FALSE)
{
if(strTemp.Find("# Begin Source File")!=-1)
if(AddFilesToArray(makFile)==FALSE)
return FALSE;
}
}
m_nFileCount=m_nCurIndex;
m_nCurIndex=0;
return TRUE;
}
BOOL CLnCountFile::AddFilesToArray(CStdioFile &myFile)
{
BOOL bLocalFlag=FALSE;
CString strTemp;
while(myFile.ReadString(strTemp)!=FALSE)
{
if(strTemp.Find("# Begin Source File")!=-1)
{
AfxMessageBox("There is some error in the Make file");
return FALSE;
}
if(strTemp.Find("# End Source File")!=-1)
{
bLocalFlag=TRUE;
break;
}
if(strTemp.Find(".hlp")!=-1) continue;
if(strTemp.Find(".hpj")!=-1) continue;
if(strTemp.Find("_AFXDLL")!=-1) continue;
if(strTemp.Find("# ADD CPP")!=-1) continue;
if(strTemp.Find(".pch")!=-1) continue;
if(strTemp.Find("afxcom_.h")!=-1) continue;
if(strTemp.Find(".cpp")!=-1 ||
strTemp.Find(".c")!=-1 ||
strTemp.Find(".h")!=-1)
{
FilterFileString(strTemp);
if(CheckFileAdded(strTemp)==FALSE)
{
m_aryFiles.Add(strTemp);
m_nCurIndex++;
}
}
}
if(bLocalFlag==FALSE)
{
AfxMessageBox("There is some error in the Make file");
return FALSE;
}
return TRUE;
}
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.)
///////////////////////////////////////////////////////////
C:\Users\Prashanth\Projects\VC\CodeCounter\CodeCounter.cpp
C:\Users\Prashanth\Projects\VC\CodeCounter\CodeCounterDlg.cpp
C:\Users\Prashanth\Projects\VC\CodeCounter\LnCountFile.cpp
PrintInfo.cpp
SplashDlg.cpp
StdAfx.cpp
TreeCheckListBox.cpp
CodeCounter.h
CodeCounterDlg.h
LnCountFile.h
PrintInfo.h
SplashDlg.h
StdAfx.h
TreeCheckListBox.h
///////////////////////////////////////////////////////////
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.