Click here to Skip to main content
12,448,362 members (55,036 online)
Click here to Skip to main content

Stats

47.5K views
480 downloads
18 bookmarked
Posted

Generator for Include File Guards

, 18 Jul 2001
A little utility that generates header file guards against multiple inclusion.
#include "stdinc.h"
#include "resource.h"
#include "maindialog.h"
#include "aboutbox.h"

const TCHAR fmtGuid[] = _TEXT(
	"%08lx_%04x_%04x_%02x%02x_%02x%02x%02x%02x%02x%02x");

const TCHAR fmtGuard[] = _TEXT(
	"#if !defined(%s_INCLUDED_%s)\r\n"
	"#define %s_INCLUDED_%s\r\n"
	"\r\n"
	"#endif // !defined(%s_INCLUDED_%s)\r\n");

BOOL CMainDialog::PreTranslateMessage(MSG* pMsg)
{
	return IsDialogMessage(pMsg);
}

BOOL CMainDialog::OnIdle()
{
	return FALSE;
}


void CMainDialog::CloseDialog(int val)
{
	m_header.Detach();
	m_guard.Detach();

	DestroyWindow();
	::PostQuitMessage(val);
}

CString CMainDialog::GetWindowText(const CWindow& wnd)
{
	int length = wnd.GetWindowTextLength() + 1;

	CString str;
	wnd.GetWindowText(str.GetBuffer(length), length);
	str.ReleaseBuffer();

	return str;
}

// Create guid and transform into text format
CString CMainDialog::CreateGuid()
{
	GUID guid;
	CoCreateGuid(&guid);

	CString str;
	str.Format(
		fmtGuid,
		guid.Data1,
		guid.Data2,
		guid.Data3,
		guid.Data4[0],	guid.Data4[1], guid.Data4[2], guid.Data4[3],
		guid.Data4[4],	guid.Data4[5], guid.Data4[6], guid.Data4[7]);

	return str;
}

CString CMainDialog::CreateGuard(CString strHeader, CString strGuid)
{
	if (strHeader.Find(_TEXT('.')) == -1)
	{
		strHeader = strHeader + _TEXT(".h");
	}

	strHeader.Replace(_TEXT('.'), _TEXT('_'));

	strHeader.MakeUpper();
	strGuid.MakeUpper();

	CString str;
	str.Format(
		fmtGuard, 
		strHeader, strGuid,
		strHeader, strGuid,
		strHeader, strGuid);

	return str;
}

LRESULT CMainDialog::OnInitDialog(HWND, LPARAM)
{
	// center the dialog on the screen
	CenterWindow();

	// set icons
	HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
	SetIcon(hIcon, TRUE);
	HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
	SetIcon(hIconSmall, FALSE);

	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	UIAddChildWindowContainer(m_hWnd);

	// attach header file edit control
	HWND hHeader = GetDlgItem(IDC_HEADER);
	ATLASSERT(hHeader != 0);
	m_header.Attach(hHeader);

	// attach guard edit control
	HWND hGuard = GetDlgItem(IDC_GUARD);
	ATLASSERT(hGuard != 0);
	m_guard.Attach(hGuard);

	return true;
}

LRESULT CMainDialog::OnAppAbout(uint /*notifyCode*/, int /*id*/, HWND /*hCtrl*/)
{
	CAboutBox about;
	about.DoModal();
	return 0;
}

LRESULT CMainDialog::OnOK(uint /*notifyCode*/, int /*id*/, HWND /*hCtrl*/)
{
	CString strHeader = GetWindowText(m_header);

	CString strGuid = CreateGuid();

	CString strGuard = CreateGuard(strHeader, strGuid);
	m_guard.SetWindowText(strGuard);

	GotoDlgCtrl(m_guard);

	return 0;
}

LRESULT CMainDialog::OnCancel(uint /*notifyCode*/, int id, HWND /*hCtrl*/)
{
	CloseDialog(id);
	return 0;
}

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

Share

About the Author

Oliver Anhuth
Web Developer
Germany Germany
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160811.3 | Last Updated 19 Jul 2001
Article Copyright 2001 by Oliver Anhuth
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid