Click here to Skip to main content
15,897,371 members
Articles / Desktop Programming / MFC

Counting Lines in a MS VC++ 6.0 project

Rate me:
Please Sign up or sign in to vote.
4.33/5 (15 votes)
4 Aug 2001 152.8K   1.9K   33  
A program that counts the lines (source, comments, blank) in every file included in a MS VC++ 6.0 project
// THIS FILE IS PROVIDED FOR INFORMATIONAL PURPOSE ONLY.
// NO COMERCIAL USE!!!
// Information: decl_spec@yahoo.com

#include "stdafx.h"
#include "LineCounter.h"
#include "ViewCodeDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CViewCodeDlg dialog


CViewCodeDlg::CViewCodeDlg() : m_filename(NULL),
	CDialog(CViewCodeDlg::IDD)
{
	//{{AFX_DATA_INIT(CViewCodeDlg)
	//}}AFX_DATA_INIT

}

CViewCodeDlg::~CViewCodeDlg()
{
	delete [] m_filename;
}

BEGIN_MESSAGE_MAP(CViewCodeDlg, CDialog)
	//{{AFX_MSG_MAP(CViewCodeDlg)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CViewCodeDlg message handlers

// Hei, I was thinking making this dialog to support syntax coloring,
// but... who needs that, when MS VC++ is doing that very well :)
BOOL CViewCodeDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	// Removing Restore && Minimize && Maximize menu items
	// IMPORTANT:	After we remove Restore, Minimize item position is 2
	//				After we remove Minimize, Maximize item position is 2
	HMENU sys_menu = ::GetSystemMenu(GetSafeHwnd(), FALSE);
	RemoveMenu(sys_menu, 0, MF_BYPOSITION);
	RemoveMenu(sys_menu, 2, MF_BYPOSITION);
	RemoveMenu(sys_menu, 2, MF_BYPOSITION);

	CHARFORMAT format;
	format.cbSize = sizeof(CHARFORMAT);
	format.dwMask = CFM_CHARSET | CFM_FACE | CFM_SIZE;
	format.bCharSet = DEFAULT_CHARSET;
	format.bPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
	format.yHeight = 175;
	char facename[32] = "Courier New";
	strcpy(format.szFaceName, facename);

	int result = m_CodeWindow.SetDefaultCharFormat(format);

	if (!m_filename)
	{
		m_CodeWindow.ReplaceSel("No Source File");
		SetWindowText("No Source File");
		return TRUE;
	}
	
	SetWindowText(m_filename);
	FILE *input = fopen(m_filename, "rb");
	if (!input)
	{
		m_CodeWindow.ReplaceSel("Could not open file: ");
		m_CodeWindow.ReplaceSel(m_filename);
		SetWindowText("Error");
	}
	else
	{
		fseek(input, 0, SEEK_END);
		int input_size = ftell(input);
		fseek(input, 0, SEEK_SET);
		char *input_buf = new char[input_size+1];
		fread(input_buf, 1, input_size, input);
		fclose(input);
		input_buf[input_size] = 0;
		SetCursor(LoadCursor(NULL, IDC_WAIT));
		m_CodeWindow.ReplaceSel(input_buf);
		delete [] input_buf;
		m_CodeWindow.PostMessage(WM_KEYDOWN, VK_HOME, NULL);
		SetCursor(LoadCursor(NULL, IDC_ARROW));
	}
	
	return TRUE;
}

void CViewCodeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CViewCodeDlg)
	DDX_Control(pDX, ID_CODEVIEW, m_CodeWindow);
	//}}AFX_DATA_MAP
}

void CViewCodeDlg::SetFileName(const char *filename)
{
	delete [] m_filename;
	m_filename = new char[strlen(filename) + 1];
	strcpy(m_filename, filename);
}

void CViewCodeDlg::OnOK()
{
}

void CViewCodeDlg::OnSize(UINT nType, int cx, int cy) 
{
	if (m_CodeWindow)
		m_CodeWindow.SetWindowPos(NULL, 0, 0, cx-20, cy-20, SWP_NOMOVE);
	CDialog::OnSize(nType, cx, cy);
}

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

Comments and Discussions