Utility to Product Source Code Statistics

This utility is for developers that need to obtain statistics concerning their source code. It will scan a directory structure and obtain all available statistics information from the source code such as total code lines, comments lines, headers lines, code and resources lines. Scanning can be recursive and countable files extensions can de defined.

// Copyright by Valery R. Kim, 20.01.2000. Freeware.


// White space delimiters
#define STR_WHITE_SPACE "/r/n /t,;"

// Structure accumulating statistics information
typedef struct _SCAN_INFO
 unsigned long lHeaders;
 unsigned long lCode;
 unsigned long lResources;
 unsigned long lLines;
 unsigned long lComments; 

 _SCAN_INFO() { lLines=lComments=lResources=lCode=lHeaders=0; }

 _SCAN_INFO& operator+(_SCAN_INFO& ScanInfo)
  return *this;
 _SCAN_INFO& operator+=(_SCAN_INFO& ScanInfo)
  return *this;


// Default predefined countable extensions
static const char pPredefinedExts[]={"c cpp h hpp rc"};

// Function filling template vector by countable 
//files extensions
template void FillExts(CArray& pDest, char** pExts)
 char* pItem=NULL;
 int  i=0;

// Function filling template array from by default countable extensions
template void FillExts(CArray& pDest, const char pSrc[])
 char szBuf[_MAX_PATH];
 strcpy(szBuf, pSrc);

// Looking for substrings devided by white space
 char* pItem=strtok(szBuf, STR_WHITE_SPACE); 
  pItem=strtok(NULL, STR_WHITE_SPACE);

// Function scanning file for statistics information
void ScanFile(CString sFileName, PSCAN_INFO pScanInfo, 
unsigned long* plType)
 FILE *f=fopen(sFileName, "r");
 if(!f) return;

 char szBuf[4096];

 // Loop for file text lines
 while(fgets(szBuf, sizeof(szBuf), f)){


  // Trim left leading white spaces
  CString s(szBuf);

  if(s.Left(2)=="//") pScanInfo->lComments++;


// Recursive function looping by subfolders
template void FoldersLoop(CString sDir, CArray& pExts, 
bool bRecursive, SCAN_INFO& FolderScanInfo)
 // Don't make statistics for folders "." and ".."
 if(sDir[sDir.GetLength()-1]=='.') return;

 WIN32_FIND_DATA findData;

 HANDLE hFind=FindFirstFile(sDir+"//*.*", &findData);


  // No files in this folder

 CArray pFiles;
 CArray pTypes;

  if(findData.dwFileAttributes & 

   // Scan this folder
   FoldersLoop(sDir+"//"+findData.cFileName, pExts, 
    bRecursive, FolderScanInfo);
  else {

   // Get file extension
   CString sFileName(findData.cFileName);

   CString sExt=sFileName.Right(sFileName.GetLength()



    // Look if this extension is countable
    for(int i=0; i// File satisfies conditions for extension

      // 0-length offset from structure addr
      if(sExt=="hpp" || sExt=="h") pTypes.Add(0);        

      // 1-length offset from structure addr
      else if(sExt=="cpp" || sExt=="c") pTypes.Add(1);   

      // 2-length offset from structure addr
      else if(sExt=="rc") pTypes.Add(2);                 
 } while(FindNextFile(hFind, &findData));

 cout << sDir;

 // If we've found anything

  cout << endl;
  for(int i=0; i// Scan this file according to it's type
   ScanFile(pFiles[i], &ScanInfo, &(ScanInfo.lHeaders)+pTypes[i]);


   cout << ".";

 cout << endl;

 // Free templates data memory

// Main entry point
int main(int argc, char* argv[])
 // Check parameters list
  cout << "Bad arguments list. Format is as follows:" << endl
       << "StatCode.exe <dir_name> [-R] [exts]" << endl
       << "  key R - means recursive statistics" << endl
       << "  exts - list of countable files extensions divided by "
                "white spaces." << endl
       << "  By default *.cpp, *.c, *.h, *.cpp, *.rc files are "
                "counted." << endl;
  return 0;

 // Base directory
 CString sBaseDir(argv[1]);

 // Recursive flag
 CString s(argv[2]);
 bool bRecursive=(s=="-R");
 // Extensions
 int  nExtStart=2;
 if(bRecursive) nExtStart++;

 // Extensions buffer
 CArray pExts; 
  // Fill extensions buffer with predefined extensions
  FillExts(pExts, pPredefinedExts);
 else {
  // Get extensions from command line
  FillExts(pExts, argv+nExtStart);

 SCAN_INFO ScanInfo;

 // Start loop for folders from base dir
 FoldersLoop(sBaseDir, pExts, bRecursive, ScanInfo);


 // Ouput all statistics accumulated
 cout << endl 
      << "Total lines : " << ScanInfo.lLines << endl
      << "Comments : " << ScanInfo.lComments 
      << " (" << (int)(100.0*ScanInfo.lComments/ScanInfo.lLines) 
      << "%)" << endl
      << "Headers  : " << ScanInfo.lHeaders 
      << " (" << (int)(100.0*ScanInfo.lHeaders/ScanInfo.lLines) 
      << "%)" << endl
      << "Code  : " << ScanInfo.lCode 
      << " (" << (int)(100.0*ScanInfo.lCode/ScanInfo.lLines) 
      << "%)" << endl
      << "Resources : " << ScanInfo.lResources 
      << " (" << (int)(100.0*ScanInfo.lResources/ScanInfo.lLines) 
      << "%)" << endl;
 return 0;


Download source - 3 Kb


  • Compiler Bugs???

    Posted by fsergiu on 02/26/2006 01:52pm

    I got the following linker errors:
    nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
    nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
    Debug/Test.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.
    Why? What is werong?
    Thanks, Sergiu

  • RE: Way to go!

    Posted by Legacy on 08/01/2000 12:00am

    Originally posted by: slonmron

    I wonder why compilers don't do this.
    Actually they really know what the statistics are and it's
    easy to implement right there, because they parse all the code.

    Vary useful article!

  • What about old-fashion comments ? (/* this is also a comment */)

    Posted by Legacy on 01/27/2000 12:00am

    Originally posted by: Ga�tan Frenoy

    Very disappointed by my poor score (10% of comments on 150,000 lines), I've searched the reason and I've found
    that the /* old-fashioned comments */ are not counted :(

    And also, the ending line comments are not added to the lComments counter :(

    Anyway, this tool is cool and very easy to use and modify.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds