Click here to Skip to main content
15,867,330 members
Articles / Desktop Programming / MFC
Article

Code Counter Tool

Rate me:
Please Sign up or sign in to vote.
4.63/5 (13 votes)
19 Dec 20013 min read 163.2K   4.6K   50   21
Code Counter is a GUI tool, which can be used for counting the number of source lines, commented lines and blank lines in a VC++ project.

Code Counter Image

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 Generic
    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.

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
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalit does not work Pin
Rajesh98293327-May-10 18:25
Rajesh98293327-May-10 18:25 
QuestionCodeCounter Pin
dharinilakki20-Apr-08 23:09
dharinilakki20-Apr-08 23:09 
QuestionHow to overwrite the file contents in vc++ Pin
Harish_kj10-Apr-07 19:43
Harish_kj10-Apr-07 19:43 
GeneralWell done Pin
Harish_kj10-Apr-07 19:37
Harish_kj10-Apr-07 19:37 
GeneralGood work Pin
tojohere15-Jan-06 15:17
tojohere15-Jan-06 15:17 
GeneralCode counter tool Pin
gsa20005-Jan-06 23:08
gsa20005-Jan-06 23:08 
Generalanother sample which support .Net, VC6 Pin
KingMarine27-Jan-05 18:07
KingMarine27-Jan-05 18:07 
another sample written by C#.

counting source code lines, comment lines, space lines.
Just select the project file, then you can get summary for all files included in this project.

Now support C#(.csproj), VB.Net(.vbproj), VC.Net(.vcproj), VC6 project file(DSW,DSP)

you can download it from [here]

enjoy it.


sun shaodi
QuestionVisual Studio.NET Support? Pin
Tom Archer6-Feb-04 5:59
Tom Archer6-Feb-04 5:59 
Generalgood work Pin
Bhikshapathi Gorantla8-Dec-03 13:33
Bhikshapathi Gorantla8-Dec-03 13:33 
GeneralCode Counter to find Delta Pin
snout3-Dec-03 4:04
snout3-Dec-03 4:04 
GeneralRe: Code Counter to find Delta Pin
Anonymous14-Jul-05 16:44
Anonymous14-Jul-05 16:44 
GeneralSourceSafe added comments Pin
JamesA_Dev19-Feb-03 4:43
JamesA_Dev19-Feb-03 4:43 
QuestionDSW? Pin
Orangy23-Dec-01 22:47
Orangy23-Dec-01 22:47 
AnswerRe: DSW? Pin
Prashanth Uppunda26-Dec-01 1:13
Prashanth Uppunda26-Dec-01 1:13 
AnswerRe: DSW? Pin
Anonymous9-Jul-02 23:33
Anonymous9-Jul-02 23:33 
GeneralPLC Pin
Thomas Freudenberg20-Dec-01 23:37
Thomas Freudenberg20-Dec-01 23:37 
GeneralRe: PLC Pin
Brian V Shifrin21-Dec-01 0:46
Brian V Shifrin21-Dec-01 0:46 
GeneralRe: PLC Pin
Thomas Freudenberg21-Dec-01 1:37
Thomas Freudenberg21-Dec-01 1:37 
GeneralRe: PLC Pin
Prashanth Uppunda26-Dec-01 0:35
Prashanth Uppunda26-Dec-01 0:35 
GeneralRe: PLC Pin
Christian Graus26-Dec-01 17:02
protectorChristian Graus26-Dec-01 17:02 
GeneralWhat is the point ??? Pin
Christian Graus26-Dec-01 17:37
protectorChristian Graus26-Dec-01 17:37 

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.