Click here to Skip to main content
15,886,780 members
Articles / Desktop Programming / MFC

Code Counter Tool

Rate me:
Please Sign up or sign in to vote.
4.63/5 (13 votes)
19 Dec 20013 min read 163.5K   4.6K   50  
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.
///////////////////////////////////////////////////////////////////////
//
//CodeCounterDlg.cpp file is part of Code Counter Tool
//Copyright (c) Prashanth Uppunda
//Contact prashum@yahoo.com for any queries
//
//////////////////////////////////////////////////////////////////////

// CodeCounterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CodeCounter.h"
#include "LnCountFile.h"
#include "TreeCheckListBox.h"
#include "CodeCounterDlg.h"
#include "PrintInfo.h"
#include "SplashDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

//Source control testing 2
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCodeCounterDlg dialog

CCodeCounterDlg::CCodeCounterDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCodeCounterDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCodeCounterDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_hTop=NULL;
	m_bJunkFlag = FALSE;
	m_bCanClose = FALSE;
	m_bFileNameSort = FALSE;
	m_bSourceLinesSort = FALSE;
	m_bBlankLinesSort = FALSE;
	m_bCommentedLinesSort = FALSE;
}

void CCodeCounterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCodeCounterDlg)
	DDX_Control(pDX, IDC_ALL, m_btnAll);
	DDX_Control(pDX, IDC_TREE1, m_myFileTree);
	DDX_Control(pDX, IDC_LIST2, m_listResult);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCodeCounterDlg, CDialog)
	//{{AFX_MSG_MAP(CCodeCounterDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_DESTROY()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ALL, OnAll)
	ON_BN_CLICKED(IDC_SEL, OnSel)
	ON_WM_SHOWWINDOW()
	ON_BN_CLICKED(IDC_TAKE, OnTake)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_PRINT, OnPrint)
	ON_WM_RBUTTONDBLCLK()
	ON_BN_CLICKED(ID_HELP, OnHelp)
	ON_WM_RBUTTONDOWN()
	ON_BN_CLICKED(ID_SAVE, OnSave)
	ON_WM_DROPFILES()
	ON_BN_CLICKED(ID_SAVECSV, OnSavecsv)
	ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
	ON_WM_CLOSE()
	ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST2, OnColumnclickList2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCodeCounterDlg message handlers

BOOL CCodeCounterDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	CString strAboutMenu;
	strAboutMenu.LoadString(IDS_ABOUTBOX);
	if (!strAboutMenu.IsEmpty())
	{
		pSysMenu->AppendMenu(MF_SEPARATOR);
		pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	//myMenu.CreateMenu();
	m_myMenu.LoadMenu(IDR_MENU1);

	SetMenu(&m_myMenu);
	
	m_strPathHolder = _T("");

	m_listResult.InsertColumn(0,_T("File Name"),LVCFMT_CENTER,200);
	m_listResult.InsertColumn(1,_T("Source Lines"),LVCFMT_CENTER,170);
	m_listResult.InsertColumn(2,_T("Commented Lines"),LVCFMT_CENTER,170);
	m_listResult.InsertColumn(2,_T("Blank Lines"),LVCFMT_CENTER,170);
	
	m_myFileTree.SetDlgPointer(this);
	m_myFileTree.Create();
	PrintInfoRead();

	CSplashDlg splashDlg;
	splashDlg.DoModal();
	//m_listResult.SetTextColor(RGB(128,0,256));

	DragAcceptFiles(TRUE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}


void CCodeCounterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		//CAboutDlg dlgAbout;
		CSplashDlg dlgAbout;
		dlgAbout.SetDelayTime(-1);
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}


void CCodeCounterDlg::OnDestroy()
{
	WinHelp(0L, HELP_QUIT);
	CDialog::OnDestroy();
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCodeCounterDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCodeCounterDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CCodeCounterDlg::FillFileTree()
{
	m_myFileTree.DeleteAllItems();
	CString strTemp;
	int i=0;
	BOOL bCPPFlag=FALSE,bCFlag=FALSE,bHFlag=FALSE,bJAVAFlag=FALSE;
	HTREEITEM hCPP,hC,hH,hCommon,hJAVA;

	m_hTop=m_myFileTree.InsertItem(m_strProjectName,CHECK,CHECK,/*TVI_ROOT*/NULL);
	m_myFileTree.SetItemData(m_hTop,-1);

	hCPP=m_myFileTree.InsertItem("\".CPP\" Files",CHECK,CHECK,m_hTop);
	m_myFileTree.SetItemData(hCPP,-1);
	hH=m_myFileTree.InsertItem("\".H\" Files",CHECK,CHECK,m_hTop);
	m_myFileTree.SetItemData(hH,-1);
	hC=m_myFileTree.InsertItem("\".C\" Files",CHECK,CHECK,m_hTop);
	m_myFileTree.SetItemData(hC,-1);
	hJAVA=m_myFileTree.InsertItem("\".JAVA\" Files",CHECK,CHECK,m_hTop);
	m_myFileTree.SetItemData(hJAVA,-1);

	m_aryFiles.RemoveAll();
	m_aryCheckFlag.RemoveAll();

	while(m_lnCountFile.GetNextFile(strTemp)!=FALSE)
	{
		m_aryFiles.Add(strTemp);
		m_aryCheckFlag.Add(TRUE);
		if(m_aryFiles[i].Find(".CPP")!= -1 || m_aryFiles[i].Find(".cpp")!= -1)
		{
			bCPPFlag=TRUE;
			hCommon=m_myFileTree.InsertItem(m_aryFiles[i],CHECK,CHECK,hCPP);
			m_myFileTree.SetItemData(hCommon,i);
		}
		else
		if(m_aryFiles[i].Find(".C")!= -1 || m_aryFiles[i].Find(".c")!= -1)
		{
			bCFlag=TRUE;
			hCommon=m_myFileTree.InsertItem(m_aryFiles[i],CHECK,CHECK,hC);
			m_myFileTree.SetItemData(hCommon,i);
		}
		else
		if(m_aryFiles[i].Find(".H")!= -1 || m_aryFiles[i].Find(".h")!= -1)
		{
			bHFlag=TRUE;
			hCommon=m_myFileTree.InsertItem(m_aryFiles[i],CHECK,CHECK,hH);
			m_myFileTree.SetItemData(hCommon,i);
		}
		else
		if(m_aryFiles[i].Find(".JAVA")!= -1 || m_aryFiles[i].Find(".java")!= -1)
		{
			bJAVAFlag=TRUE;
			hCommon=m_myFileTree.InsertItem(m_aryFiles[i],CHECK,CHECK,hJAVA);
			m_myFileTree.SetItemData(hCommon,i);
		}
		i++;
	}

	if(bCPPFlag==FALSE)
		m_myFileTree.DeleteItem(hCPP);
	if(bCFlag==FALSE)
		m_myFileTree.DeleteItem(hC);
	if(bHFlag==FALSE)
		m_myFileTree.DeleteItem(hH);
	if(bJAVAFlag==FALSE)
		m_myFileTree.DeleteItem(hJAVA);

	return TRUE;
}

void CCodeCounterDlg::OnAll() 
{
	GetDlgItem(IDC_TREE1)->EnableWindow(FALSE);
	if(m_hTop!=NULL)
	{
		m_myFileTree.CheckAllItems(m_hTop,1);		 
		for(int i=0;i<m_aryCheckFlag.GetSize();i++)
			m_aryCheckFlag[i]=TRUE;
	}
}

void CCodeCounterDlg::OnSel() 
{
	GetDlgItem(IDC_TREE1)->EnableWindow(TRUE);
}


void CCodeCounterDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
	// TODO: Add your message handler code here
	CenterWindow();
}

void CCodeCounterDlg::OnTake() 
{
	static char BASED_CODE szFilter[] = "VC++ Project make file(*.mak,*.dsp)|*.mak;*.dsp|Generic File(*.map)|*.map||";
	CFileDialog dlg(TRUE,"","",OFN_LONGNAMES,szFilter);
	dlg.DoModal();

	CString strTemp = m_strProjectName;
	m_strProjectName="";
	m_strProjectName=dlg.GetPathName();
	if(m_strProjectName=="" || m_strProjectName=="*.mak")
	{
		m_strProjectName=strTemp;
		return;
	}

	DoSettingsForNewPjt();
}

void CCodeCounterDlg::OnExit() 
{
	PostMessage(WM_CLOSE,0,0);	
}

void CCodeCounterDlg::OnStart() 
{
	if(m_strPathHolder != _T(""))
	{
		if(SetCurrentDirectory(m_strPathHolder) != TRUE)
		{
			AfxMessageBox("Error Setting the Current Directory");
			return;
		}
	}

	int nGrandTotal=0;
	CString strFormat;
	int nTotalLines=0,nTotalBlank=0,nTotalComment=0,nCommentLines=0,nBlankLines=0;
	m_lnCountFile.ResetErrorFileArray();
	m_aryCountInfo.RemoveAll();
	m_bFileNameSort = FALSE;
	m_bSourceLinesSort = FALSE;
	m_bBlankLinesSort = FALSE;
	m_bCommentedLinesSort = FALSE;

	for(int i=0;i<m_aryFiles.GetSize();i++)
	{
		if(m_aryCheckFlag[i]==TRUE)
		{
			if(m_lnCountFile.GetFileLineInfo(m_aryFiles[i],nTotalLines,nBlankLines,nCommentLines)==TRUE)
			{
				CCountInfo countInfo;
				countInfo.m_strFileName = m_aryFiles[i];
				countInfo.m_nSourceLines = nTotalLines-(nBlankLines+nCommentLines);
				countInfo.m_nBlankLines = nBlankLines;
				countInfo.m_nCommentLines = nCommentLines;

				m_aryCountInfo.Add(countInfo);

				nGrandTotal+= (nTotalLines-(nBlankLines+nCommentLines));
				nTotalBlank+=nBlankLines;
				nTotalComment+=nCommentLines;
			}
		}
	}

	DisplayListDetails();

	strFormat.Format("%d",nGrandTotal);
	GetDlgItem(IDC_GRAND)->SetWindowText(strFormat);
	strFormat.Format("%d",nTotalBlank);
	GetDlgItem(IDC_GRAND2)->SetWindowText(strFormat);
	strFormat.Format("%d",nTotalComment);
	GetDlgItem(IDC_GRAND3)->SetWindowText(strFormat);

	strFormat=m_lnCountFile.GetErrorFileInfo();
	if(strFormat!=_T(""))
		AfxMessageBox("The following files could not be open\n and hence Source Line Information will not be available for this files\n"+strFormat);
}

void CCodeCounterDlg::OnPrint() 
{
	 HDC   hdcPrn ; // handle to printer dc
	CString strHeadings;// For headlines
	CString strDash ;// To store Line
	
 	CPrintDialog  *printDlg = new CPrintDialog (FALSE);


	// Initialization
	printDlg->m_pd.nMinPage = printDlg->m_pd.nMaxPage = 1;
	printDlg->m_pd.nFromPage = printDlg->m_pd.nToPage = 1;

	if(printDlg->DoModal() == IDCANCEL)
		return ;
	
	hdcPrn =  printDlg->CreatePrinterDC(); 
	if( hdcPrn != NULL )
	{
		PrintString("CodeCounter", PRINT_NEW_DOC, hdcPrn);

		PrintString(NULL, PRINT_NEW_PAGE, hdcPrn);

		CTime curTime;
		CString strDate;

		curTime = CTime::GetCurrentTime();
		strDate.Format("Date : %d-%d-%d  Time : %d:%d:%d",curTime.GetDay(),curTime.GetMonth(),
			curTime.GetYear(),curTime.GetHour(),curTime.GetMinute(),curTime.GetSecond());

		PrintString("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", PRINT_STRING, hdcPrn);
		PrintString("Code Counter Report", PRINT_STRING, hdcPrn);
		PrintString(strDate, PRINT_STRING, hdcPrn);
		PrintString("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", PRINT_STRING, hdcPrn);
		PrintString(" ",PRINT_STRING, hdcPrn);

		CString strFileName	;
		CString strSource;
		CString strBlank;
		CString strComment;

		strDash = CString("----------------------------------------------------------------------------------------------------");	

		PrintString("Source Code Count Details of "+m_strProjectName, PRINT_STRING, hdcPrn);
		PrintString(strDash,PRINT_STRING,hdcPrn);
		PrintString("File Name", PRINT_STRING, hdcPrn,0,FALSE);
		PrintString("Source Lines", PRINT_STRING, hdcPrn,30,FALSE);
		PrintString("Blank Lines", PRINT_STRING, hdcPrn,50,FALSE);
		PrintString("Commented Lines", PRINT_STRING, hdcPrn,70,TRUE);
	
		PrintString(strDash, PRINT_STRING, hdcPrn);

		CStringArray aryLtCtrlItems ;  // Array To Store ListControl Items And used to print
    	int i32Subitem = 4 ;  // ListControl items
		int i32SubItemIndex;
		int i32ListIndex;
		CString strTempBuf;  
		for( i32ListIndex = 0; i32ListIndex < m_listResult.GetItemCount(); i32ListIndex++)
		{  
			aryLtCtrlItems.RemoveAll();
			for(i32SubItemIndex = 0; i32SubItemIndex < i32Subitem;i32SubItemIndex++)
			{
  				aryLtCtrlItems .Add(m_listResult.GetItemText(i32ListIndex,i32SubItemIndex));
			}
			/*if( aryLtCtrlItems [0] == CString('-',FieldWidth_0+1)
				&& aryLtCtrlItems [1] == CString('-',FieldWidth_1+1)
				&& aryLtCtrlItems [2] == CString('-',FieldWidth_2+1)
				&& aryLtCtrlItems [3] == CString('-',FieldWidth_3+1)
			{
				PrintString(strDash, PRINT_STRING, hdcPrn);
			}
			else
			{  */

				strTempBuf = aryLtCtrlItems[0];//.Left(FieldWidth_0);
				PrintString(strTempBuf, PRINT_STRING, hdcPrn,0,FALSE);
				strTempBuf = aryLtCtrlItems[1];//.Left(FieldWidth_1);
				PrintString(strTempBuf, PRINT_STRING, hdcPrn,30,FALSE);
				strTempBuf = aryLtCtrlItems[2];//.Left(FieldWidth_2);
				PrintString(strTempBuf, PRINT_STRING, hdcPrn,50,FALSE);
				strTempBuf = aryLtCtrlItems[3];//.Left(FieldWidth_3);
				PrintString(strTempBuf, PRINT_STRING, hdcPrn,70,TRUE);
			//}
		}

		CString strTemp1;

	  PrintString(strDash, PRINT_STRING, hdcPrn);
	  PrintString("Grand Total          ",PRINT_STRING, hdcPrn,0,FALSE);
	  GetDlgItem(IDC_GRAND)->GetWindowText(strTempBuf);
	  strTemp1 = "  " + strTempBuf;
	  GetDlgItem(IDC_GRAND2)->GetWindowText(strTempBuf);
	  strTemp1 += "                " + strTempBuf;
	  GetDlgItem(IDC_GRAND3)->GetWindowText(strTempBuf);
	  strTemp1 += "                  " + strTempBuf;

	  PrintString(strTemp1, PRINT_STRING, hdcPrn,28,TRUE);
	  PrintString(strDash, PRINT_STRING, hdcPrn);
	  PrintString(NULL, PRINT_END_PAGE, hdcPrn);
	  PrintString(NULL, PRINT_END_DOC, hdcPrn);
	}
	 delete printDlg ;
}

void CCodeCounterDlg::OnRButtonDblClk(UINT nFlags, CPoint point) 
{
	CDialog::OnRButtonDblClk(nFlags, point);
}


void CCodeCounterDlg::Serialize(CArchive& ar) 
{
	if (ar.IsStoring())
	{	// storing code
	}
	else
	{	// loading code
	}
}

void CCodeCounterDlg::OnHelp() 
{

}

void CCodeCounterDlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	ClientToScreen(&point);

	CMenu myPopMenu;

	myPopMenu.CreatePopupMenu();
	myPopMenu.AppendMenu(MF_STRING, ID_HELP_ABOUT,"About CodeCounter...");

	//myPopMenu.LoadMenu(IDR_MENU1);
	myPopMenu.TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);

	ScreenToClient(&point);
	CDialog::OnLButtonDown(nFlags, point);
}

void CCodeCounterDlg::OnSave() 
{
	CString strFileName;
	// TODO: Add your control notification handler code here
	static char BASED_CODE szFilter[] = "Text File(*.txt)|*.txt|";

	CFileDialog dlg(FALSE,"","CodeCounter.txt",OFN_LONGNAMES,szFilter);
	if(dlg.DoModal() == IDCANCEL)
		return;

	strFileName = dlg.GetPathName();

	if(strFileName == _T(""))
		return;
	
	CStdioFile saveFile;
	if(saveFile.Open(strFileName,CFile::modeWrite|CFile::typeText))
	{
		saveFile.Close();
		if(AfxMessageBox("This File allready exists\nDo you want to overwrite ?",
			MB_ICONQUESTION|MB_YESNO) == IDYES)
		{
			if(!saveFile.Open(strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeText))
			{
				AfxMessageBox("Error opening file",MB_ICONERROR);
				return;
			}
		}
		else
			return;
	}
	else
	{
		if(!saveFile.Open(strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeText))
		{
			AfxMessageBox("Error opening file",MB_ICONERROR);
			return;
		}
	}

	CString strHeadings;// For headlines
	CString strDash ;// To store Line
	CTime curTime;
	CString strDate;

	curTime = CTime::GetCurrentTime();
	strDate.Format("Date : %d-%d-%d  Time : %d:%d:%d",curTime.GetDay(),curTime.GetMonth(),
		curTime.GetYear(),curTime.GetHour(),curTime.GetMinute(),curTime.GetSecond());

	saveFile.WriteString("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	saveFile.WriteString("Code Counter Report\n");
	saveFile.WriteString(strDate+ "\n");
	saveFile.WriteString("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");

	CString strSource;
	CString strBlank;
	CString strComment;

	strDash = CString("----------------------------------------------------------------------------------------------------");	

	saveFile.WriteString("Source Code Count Details of "+m_strProjectName + "\n");
	saveFile.WriteString(strDash + "\n");
	saveFile.WriteString("File Name			Source Lines		Blank Lines		Commented Lines\n");

	saveFile.WriteString(strDash + "\n");

	CStringArray aryLtCtrlItems ;  // Array To Store ListControl Items And used to print
	int i32Subitem = 4 ;  // ListControl items
	int i32SubItemIndex;
	int i32ListIndex;
	CString strTempBuf;  
	for( i32ListIndex = 0; i32ListIndex < m_listResult.GetItemCount(); i32ListIndex++)
	{  
		aryLtCtrlItems.RemoveAll();
		for(i32SubItemIndex = 0; i32SubItemIndex < i32Subitem;i32SubItemIndex++)
		{
  			aryLtCtrlItems .Add(m_listResult.GetItemText(i32ListIndex,i32SubItemIndex));
		}

		if(aryLtCtrlItems[0].GetLength() < 32)
		{
			for(int i = aryLtCtrlItems[0].GetLength(); i <= 32; i++)
				aryLtCtrlItems[0] += " ";
		}
		strTempBuf = aryLtCtrlItems[0] ;
		strTempBuf += aryLtCtrlItems[1]+ "			   ";
		strTempBuf += aryLtCtrlItems[2] + "			   ";
		strTempBuf += aryLtCtrlItems[3] + "			";

		saveFile.WriteString(strTempBuf + "\n");
	}

	CString strTemp1;

	  saveFile.WriteString(strDash + "\n");
	  saveFile.WriteString("Grand Total          ");
	  GetDlgItem(IDC_GRAND)->GetWindowText(strTempBuf);
	  strTempBuf = "            " + strTempBuf;
	  GetDlgItem(IDC_GRAND2)->GetWindowText(strTemp1);
	  strTempBuf += "                      " + strTemp1;
	  GetDlgItem(IDC_GRAND3)->GetWindowText(strTemp1);
	  strTempBuf += "                     " + strTemp1;
	  saveFile.WriteString(strTempBuf + "\n");
	  saveFile.WriteString(strDash + "\n");

	saveFile.Close();
	//AfxMessageBox("Not yet implemented");
}

void CCodeCounterDlg::OnDropFiles( HDROP hDropInfo )
{
	char szFileName[256];

	DragQueryFile(hDropInfo,0,szFileName,255);

	m_strProjectName = CString(szFileName);

	DoSettingsForNewPjt();
}

void CCodeCounterDlg::DoSettingsForNewPjt()
{
	//Set the current directory

	CString strTempDir = m_strProjectName.Left(m_strProjectName.ReverseFind('\\'));

	m_strPathHolder = strTempDir;

	if(SetCurrentDirectory(strTempDir) != TRUE)
	{
		AfxMessageBox("Error Setting the Current Directory");
		return;
	}

	m_lnCountFile.GetFileList(m_strProjectName);

	int nPos=m_strProjectName.ReverseFind('\\');
	if(nPos!=-1)
		m_strProjectName=m_strProjectName.Mid(nPos+1);

	m_strProjectName = m_strProjectName.Left(m_strProjectName.Find('.'));

	m_strProjectName+= " Project Files";

	m_btnAll.SetCheck(1);
	((CButton*)GetDlgItem(IDC_SEL))->SetCheck(0);
	GetDlgItem(IDC_TREE1)->EnableWindow(FALSE);

	FillFileTree();
	m_listResult.DeleteAllItems();
	GetDlgItem(IDC_GRAND)->SetWindowText("");
	GetDlgItem(IDC_GRAND2)->SetWindowText("");
	GetDlgItem(IDC_GRAND3)->SetWindowText("");

	GetDlgItem(IDC_START)->EnableWindow(TRUE);
	GetDlgItem(ID_SAVE)->EnableWindow(TRUE);
	GetDlgItem(ID_SAVECSV)->EnableWindow(TRUE);
	GetDlgItem(IDC_PRINT)->EnableWindow(TRUE);

	m_myMenu.EnableMenuItem(IDC_START,MF_ENABLED);
	m_myMenu.EnableMenuItem(ID_SAVE,MF_ENABLED);
	m_myMenu.EnableMenuItem(ID_SAVECSV,MF_ENABLED);
	m_myMenu.EnableMenuItem(IDC_PRINT,MF_ENABLED);

	GetDlgItem(IDC_ALL)->EnableWindow(TRUE);
	GetDlgItem(IDC_SEL)->EnableWindow(TRUE);

	//SetWindowPos(&wndTop,m_oldRect.top,m_oldRect.left,m_oldRect.right-m_oldRect.left,m_oldRect.bottom-m_oldRect.top,SWP_NOMOVE);
	CenterWindow();
}

void CCodeCounterDlg::OnSavecsv() 
{
	// TODO: Add your control notification handler code here
	CString strFileName;
	// TODO: Add your control notification handler code here
	static char BASED_CODE szFilter[] = "CSV File(*.csv)|*.csv|";

	CFileDialog dlg(FALSE,"","CodeCounter.csv",OFN_LONGNAMES,szFilter);
	if(dlg.DoModal() == IDCANCEL)
		return;

	strFileName = dlg.GetPathName();

	if(strFileName == _T(""))
		return;
	
	CStdioFile saveFile;
	if(saveFile.Open(strFileName,CFile::modeWrite|CFile::typeText))
	{
		saveFile.Close();
		if(AfxMessageBox("This File allready exists\nDo you want to overwrite ?",
			MB_ICONQUESTION|MB_YESNO) == IDYES)
		{
			if(!saveFile.Open(strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeText))
			{
				AfxMessageBox("Error opening file",MB_ICONERROR);
				return;
			}
		}
		else
			return;
	}
	else
	{
		if(!saveFile.Open(strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeText))
		{
			AfxMessageBox("Error opening file",MB_ICONERROR);
			return;
		}
	}

	CString strHeadings;// For headlines
	CString strDash ;// To store Line
	CTime curTime;
	CString strDate;

	curTime = CTime::GetCurrentTime();
	strDate.Format("Date : %d-%d-%d  Time : %d:%d:%d",curTime.GetDay(),curTime.GetMonth(),
		curTime.GetYear(),curTime.GetHour(),curTime.GetMinute(),curTime.GetSecond());

	saveFile.WriteString("Code Counter Report\n");
	saveFile.WriteString(strDate+ "\n");

	CString strSource;
	CString strBlank;
	CString strComment;


	saveFile.WriteString("Source Code Count Details of "+m_strProjectName + "\n");
	saveFile.WriteString("File Name,Source Lines,Blank Lines,Commented Lines\n");


	CStringArray aryLtCtrlItems ;  // Array To Store ListControl Items And used to print
	int i32Subitem = 4 ;  // ListControl items
	int i32SubItemIndex;
	int i32ListIndex;
	CString strTempBuf;  
	for( i32ListIndex = 0; i32ListIndex < m_listResult.GetItemCount(); i32ListIndex++)
	{  
		aryLtCtrlItems.RemoveAll();
		for(i32SubItemIndex = 0; i32SubItemIndex < i32Subitem;i32SubItemIndex++)
		{
  			aryLtCtrlItems .Add(m_listResult.GetItemText(i32ListIndex,i32SubItemIndex));
		}

		strTempBuf = aryLtCtrlItems[0]+ ",";
		strTempBuf += aryLtCtrlItems[1]+ ",";
		strTempBuf += aryLtCtrlItems[2] + ",";
		strTempBuf += aryLtCtrlItems[3];

		saveFile.WriteString(strTempBuf + "\n");
	}

	CString strTemp1;

	  saveFile.WriteString("Grand Total");
	  GetDlgItem(IDC_GRAND)->GetWindowText(strTempBuf);
	  strTempBuf = ","+ strTempBuf;
	  GetDlgItem(IDC_GRAND2)->GetWindowText(strTemp1);
	  strTempBuf += "," + strTemp1;
	  GetDlgItem(IDC_GRAND3)->GetWindowText(strTemp1);
	  strTempBuf += "," + strTemp1;
	  saveFile.WriteString(strTempBuf + "\n");

	saveFile.Close();
	//AfxMessageBox("Not yet implemented");	
}

void CCodeCounterDlg::OnHelpAbout() 
{
	// TODO: Add your command handler code here
	//CAboutDlg dlg;
	CSplashDlg dlg;
	dlg.SetDelayTime(-1);
	dlg.DoModal();
}

void CCodeCounterDlg::OnOK()
{
}

void CCodeCounterDlg::OnCancel()
{
	if(m_bCanClose == TRUE)
		CDialog::OnCancel();
}

void CCodeCounterDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	m_bCanClose = TRUE;
	CDialog::OnClose();
}

void CCodeCounterDlg::OnColumnclickList2(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here

	int nIndex1=0, nIndex2=0;
	switch(pNMListView->iSubItem)
	{
	case 0:	//Sort on File Name
		for(nIndex1=0; nIndex1 < m_aryCountInfo.GetSize()-1; nIndex1++)
		{
			for(nIndex2=nIndex1+1; nIndex2 < m_aryCountInfo.GetSize(); nIndex2++)
			{
				if(m_bFileNameSort == FALSE)
				{
					if(m_aryCountInfo[nIndex1].m_strFileName > m_aryCountInfo[nIndex2].m_strFileName)
					{
						CCountInfo countInfo;
						countInfo = m_aryCountInfo[nIndex1];
						m_aryCountInfo[nIndex1] = m_aryCountInfo[nIndex2];
						m_aryCountInfo[nIndex2] = countInfo;
					}
				}
				else
				{
					if(m_aryCountInfo[nIndex1].m_strFileName < m_aryCountInfo[nIndex2].m_strFileName)
					{
						CCountInfo countInfo;
						countInfo = m_aryCountInfo[nIndex1];
						m_aryCountInfo[nIndex1] = m_aryCountInfo[nIndex2];
						m_aryCountInfo[nIndex2] = countInfo;
					}
				}
			}
		}
		m_bFileNameSort = !m_bFileNameSort;
		break;
	case 1:	//Sort on Source Code Count
		for(nIndex1=0; nIndex1 < m_aryCountInfo.GetSize()-1; nIndex1++)
		{
			for(nIndex2=nIndex1+1; nIndex2 < m_aryCountInfo.GetSize(); nIndex2++)
			{
				if(m_bSourceLinesSort == FALSE)
				{
					if(m_aryCountInfo[nIndex1].m_nSourceLines > m_aryCountInfo[nIndex2].m_nSourceLines)
					{
						CCountInfo countInfo;
						countInfo = m_aryCountInfo[nIndex1];
						m_aryCountInfo[nIndex1] = m_aryCountInfo[nIndex2];
						m_aryCountInfo[nIndex2] = countInfo;
					}
				}
				else
				{
					if(m_aryCountInfo[nIndex1].m_nSourceLines < m_aryCountInfo[nIndex2].m_nSourceLines)
					{
						CCountInfo countInfo;
						countInfo = m_aryCountInfo[nIndex1];
						m_aryCountInfo[nIndex1] = m_aryCountInfo[nIndex2];
						m_aryCountInfo[nIndex2] = countInfo;
					}
				}
			}
		}
		m_bSourceLinesSort = !m_bSourceLinesSort;
		break;
	case 2:	//Sort on Blank Lines count
		for(nIndex1=0; nIndex1 < m_aryCountInfo.GetSize()-1; nIndex1++)
		{
			for(nIndex2=nIndex1+1; nIndex2 < m_aryCountInfo.GetSize(); nIndex2++)
			{
				if(m_bBlankLinesSort == FALSE)
				{
					if(m_aryCountInfo[nIndex1].m_nBlankLines > m_aryCountInfo[nIndex2].m_nBlankLines)
					{
						CCountInfo countInfo;
						countInfo = m_aryCountInfo[nIndex1];
						m_aryCountInfo[nIndex1] = m_aryCountInfo[nIndex2];
						m_aryCountInfo[nIndex2] = countInfo;
					}
				}
				else
				{
					if(m_aryCountInfo[nIndex1].m_nBlankLines < m_aryCountInfo[nIndex2].m_nBlankLines)
					{
						CCountInfo countInfo;
						countInfo = m_aryCountInfo[nIndex1];
						m_aryCountInfo[nIndex1] = m_aryCountInfo[nIndex2];
						m_aryCountInfo[nIndex2] = countInfo;
					}
				}
			}
		}
		m_bBlankLinesSort = !m_bBlankLinesSort;
		break;
	case 3:	//Sort on Commented Lines count
		for(nIndex1=0; nIndex1 < m_aryCountInfo.GetSize()-1; nIndex1++)
		{
			for(nIndex2=nIndex1+1; nIndex2 < m_aryCountInfo.GetSize(); nIndex2++)
			{
				if(m_bCommentedLinesSort == FALSE)
				{
					if(m_aryCountInfo[nIndex1].m_nCommentLines > m_aryCountInfo[nIndex2].m_nCommentLines)
					{
						CCountInfo countInfo;
						countInfo = m_aryCountInfo[nIndex1];
						m_aryCountInfo[nIndex1] = m_aryCountInfo[nIndex2];
						m_aryCountInfo[nIndex2] = countInfo;
					}
				}
				else
				{
					if(m_aryCountInfo[nIndex1].m_nCommentLines < m_aryCountInfo[nIndex2].m_nCommentLines)
					{
						CCountInfo countInfo;
						countInfo = m_aryCountInfo[nIndex1];
						m_aryCountInfo[nIndex1] = m_aryCountInfo[nIndex2];
						m_aryCountInfo[nIndex2] = countInfo;
					}
				}
			}
		}
		m_bCommentedLinesSort = !m_bCommentedLinesSort;
		break;
	default:
		break;
	}

	DisplayListDetails();
	*pResult = 0;
}

void CCodeCounterDlg::DisplayListDetails()
{
	CString strFormat;
	m_listResult.DeleteAllItems();
	for(int nIndex =0; nIndex <m_aryCountInfo.GetSize(); nIndex++)
	{
		m_listResult.InsertItem(nIndex,m_aryCountInfo[nIndex].m_strFileName);
		strFormat.Format("%d",m_aryCountInfo[nIndex].m_nSourceLines);		
		m_listResult.SetItem(nIndex,1,LVIF_TEXT,strFormat,0,0,0,0);
		strFormat.Format("%d",m_aryCountInfo[nIndex].m_nBlankLines);
		m_listResult.SetItem(nIndex,2,LVIF_TEXT,strFormat,0,0,0,0);	
		strFormat.Format("%d",m_aryCountInfo[nIndex].m_nCommentLines);
		m_listResult.SetItem(nIndex,3,LVIF_TEXT,strFormat,0,0,0,0);	
	}
	m_listResult.UpdateWindow();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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