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

  • Modern contact centers support a variety of digital channels—email, chat, co-browsing, social, video—over multiple digital touchpoints (web or mobile devices). What they lack is the ability to link interactions in a step-by-step journey to provide a personal, contextual experience for each customer. Download this eBook. Learn how you can: Design a successful digital customer engagement strategy Provide agents with a 360-degree view of the customer across all digital channels and voice Deliver …

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud-based suite of productivity collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date