Click here to Skip to main content
12,702,455 members (36,165 online)
Click here to Skip to main content

Stats

206.2K views
3.5K downloads
107 bookmarked
Posted

Classes for computational geometry

, 26 Dec 2001 CPOL
Some classes and utility functions for general computational geometry
// utils.cpp : implementation file
//
#include "stdafx.h"
//#include "SHELLAPI.H"
#include "utils.h"


BOOL PeekAndPump()
{
	static MSG msg;

	while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) {
		if (!AfxGetApp()->PumpMessage()) {
			::PostQuitMessage(0);
			return FALSE;
		}	
	}

	return TRUE;
}

BOOL FileOpen(FILE **fp, LPCTSTR Name, char *mode, BOOL bIssueWarning /* = TRUE */)
{
	*fp = fopen(Name,mode);
	if (!(*fp)) {
		if (bIssueWarning) {
			CString str;
			str.Format("Unable to open %s.",Name);
			AfxMessageBox(str);
		}
		return FALSE;
	}
	return TRUE;
}

BOOL SetBarText(LPCTSTR fmt, ...)
{
	va_list args;
	char buffer[512];

	CFrameWnd *pFrame = (CFrameWnd*)AfxGetMainWnd();

	/* pFrame->SetMessageText() */
	CWnd *bar = pFrame->GetMessageBar();

	if (bar != NULL) {
		va_start(args, fmt);
		vsprintf(buffer, fmt, args);
		va_end(args);
		bar->SetWindowText(buffer);
	}
	return (bar != NULL);
}

void Message(LPCTSTR fmt, ...)
{
	va_list args;
	char buffer[1024];
	CString str;
	str.Empty();

	va_start(args, fmt);
	vsprintf(buffer, fmt, args);
	va_end(args);
	str += buffer;
	AfxMessageBox(str);
}

BOOL Error(LPCTSTR fmt, ...)
{
	va_list args;
	char buffer[1024];
	CString str = "Error occurred: "; 

	va_start(args, fmt);
	vsprintf(buffer, fmt, args);
	va_end(args);
	str += buffer;
	AfxMessageBox(str);

	return FALSE;
}

BOOL Error(FILE* fp, LPCTSTR fmt, ...)
{
	va_list args;
	char buffer[1024];
	CString str = "Error occurred: "; 

	va_start(args, fmt);
	vsprintf(buffer, fmt, args);
	va_end(args);
	str += buffer;
	AfxMessageBox(str);

	if (fp) fclose(fp);
	return FALSE;
}

CString GetFieldFromString(CString& ref, int nIndex, TCHAR ch)
{
	CString strReturn;
	LPCTSTR pstrStart = ref.LockBuffer();
	LPCTSTR pstrBuffer = pstrStart;
	int nCurrent = 0;
	int nStart = 0;
	int nEnd = 0;
	int nOldStart = 0;

	while (nCurrent <= nIndex && *pstrBuffer != _T('\0') && *pstrBuffer != _T('\n'))
	{
		if (*pstrBuffer == ch)
		{
			nOldStart = nStart;
			nStart = nEnd+1;
			nCurrent++;
		}
		nEnd++;
		pstrBuffer++;
	}

	ref.UnlockBuffer();

	if (nCurrent < nIndex) 
//	if (*pstrBuffer == _T('\0'))
	{
		TRACE("Warning: GetStringField - Couldn't find field %d.\n", nIndex);
		return strReturn;
	}
	return ref.Mid(nOldStart, nEnd-nOldStart-1);
}

BOOL EnumSerialPorts(CStringArray& PortNames)
{
	HKEY hKey;
	DWORD PortCount;

	PortNames.RemoveAll();

	if ( ERROR_SUCCESS != RegOpenKey(HKEY_LOCAL_MACHINE,
									_T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 
									&hKey ))
	{
		TRACE("Registry Key is missing.");
		return FALSE;
	}

    RegQueryInfoKey (hKey, NULL, NULL, NULL, NULL, NULL, NULL, &PortCount, 
					 NULL, NULL, NULL, NULL);

	if (PortCount == 0)
    {
		RegCloseKey(hKey);
		TRACE("There are no serial ports.");
		return FALSE;
	}

	TCHAR vname[50];
	DWORD vnsize = 50;
	char pname[50];
	DWORD pnsize = 50;

	for (DWORD nPort = 0; nPort < PortCount; nPort++ )
	{
		RegEnumValue(hKey, nPort, &vname[0], &vnsize, NULL,
					 NULL, (unsigned char*)&pname[0], &pnsize );
		PortNames.Add(pname);
	}
	return TRUE;
}
/*
#include "winsock2.h"
// Link in with ws2_32.lib
BOOL GetIPaddress(CString& strIPaddress, CString& strIPnumbers)
{
	CString strIPaddress;
	char buffer[1024];
	hostent *hostinfo = NULL;
		
	if (gethostname(buffer, 1024) == SOCKET_ERROR)
		return FALSE;  
	
	if ((hostinfo = gethostbyname(buffer)) == NULL)
		return FALSE;  
	
	strIPaddress = inet_ntoa (*(struct in_addr *) *hostinfo->h_addr_list);

    strIPnumbers.Format("%u.%u.%u.%u", (BYTE) phe->h_addr_list[0] [0],
									   (BYTE) phe->h_addr_list[0] [1],
									   (BYTE) phe->h_addr_list[0] [2],
									   (BYTE) phe->h_addr_list[0] [3]);
	
	return TRUE;
 }
*/

/*
LONG CMyView::TwipsToPixels(long lTwips, short nDirection)
{
	const int nTwipsPerInch = 1440;
	int nPixelsPerInch;
	CClientDC dc(this);
	if (nDirection == 0)     // Horizontal
		nPixelsPerInch = dc.GetDeviceCaps(LOGPIXELSX);
	else                    // Vertical
		nPixelsPerInch = dc.GetDeviceCaps(LOGPIXELSY);
	return (LONG) (lTwips*nPixelsPerInch)/nTwipsPerInch;
}

LONG CMyView::PixelsToTwips(long lPixels, short nDirection)
{
	const int nTwipsPerInch = 1440;
	int nPixelsPerInch;
	CClientDC dc(this);
	if (nDirection == 0)     // Horizontal
		nPixelsPerInch = dc.GetDeviceCaps(LOGPIXELSX);
	else                    // Vertical
		nPixelsPerInch = dc.GetDeviceCaps(LOGPIXELSY);
	return (LONG) (lPixels*nTwipsPerInch)/nPixelsPerInch;
}
*/

/*
#include "shlobj.h"

    char szTitle[] = "Select Directory for Whatever Reason";
	char szPath[MAX_PATH];

	BROWSEINFO bi;
    bi.hwndOwner = GetSafeHwnd();
    bi.pidlRoot = NULL;
    bi.pszDisplayName = NULL;
    bi.lpszTitle = szTitle;
    bi.ulFlags = BIF_RETURNONLYFSDIRS;
    bi.lpfn = NULL;
    bi.lParam = NULL;

    LPITEMIDLIST pidl = SHBrowseForFolder(&bi);

    if (pidl != NULL)
    {
		SHGetPathFromIDList(pidl, szPath);

		LPMALLOC pMalloc;    
		SHGetMalloc(&pMalloc);
		pMalloc->Free(pidl);
	}
*/

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

You may also be interested in...

Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 27 Dec 2001
Article Copyright 2001 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid