Click here to Skip to main content
Click here to Skip to main content
Go to top

Code Counter Tool

, 19 Dec 2001
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

No Biography provided

Comments and Discussions

 
Generalit does not work PinmemberRajesh98293327-May-10 18:25 
QuestionCodeCounter Pinmemberdharinilakki20-Apr-08 23:09 
QuestionHow to overwrite the file contents in vc++ PinmemberHarish_kj10-Apr-07 19:43 
GeneralWell done PinmemberHarish_kj10-Apr-07 19:37 
this project is really nice one. Congrats...Laugh | :laugh:
GeneralGood work Pinmembertojohere15-Jan-06 15:17 
GeneralCode counter tool Pinmembergsa20005-Jan-06 23:08 
Generalanother sample which support .Net, VC6 PinmemberKingMarine27-Jan-05 18:07 
QuestionVisual Studio.NET Support? PinmemberTom Archer6-Feb-04 5:59 
Generalgood work PinmemberPathi Gorantla8-Dec-03 13:33 
GeneralCode Counter to find Delta Pinmembersnout3-Dec-03 4:04 
GeneralRe: Code Counter to find Delta PinsussAnonymous14-Jul-05 16:44 
GeneralSourceSafe added comments PinmemberJames Agnini19-Feb-03 4:43 
QuestionDSW? PinmemberOrangy23-Dec-01 22:47 
AnswerRe: DSW? PinmemberPrashanth Uppunda26-Dec-01 1:13 
AnswerRe: DSW? PinsussAnonymous9-Jul-02 23:33 
GeneralPLC PinmemberThomas Freudenberg20-Dec-01 23:37 
GeneralRe: PLC PinmemberBrian V Shifrin21-Dec-01 0:46 
GeneralRe: PLC PinmemberThomas Freudenberg21-Dec-01 1:37 
GeneralRe: PLC PinmemberPrashanth Uppunda26-Dec-01 0:35 
GeneralRe: PLC PinmemberChristian Graus26-Dec-01 17:02 
GeneralWhat is the point ??? PinmemberChristian Graus26-Dec-01 17:37 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 20 Dec 2001
Article Copyright 2001 by Prashanth Uppunda
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid