|
//====================================================================
// Although great care has gone into developing this software,
// it is provided without any guarantee of reliability, accuracy
// of information, or correctness of operation. I am not responsible
// for any damages that may occur as a result of using this software.
// Use this software entirely at your own risk.
// Copyright 2003, Chris Richardson
//
// Description: Some global helper stuff.
//
//====================================================================
#include "stdafx.h"
#include "global.h"
void ParseSeparatedList( const TCHAR * p_pszList, TCHAR * p_pszSeps, CStdStringArray & p_roOutput )
{
TCHAR a_szItem[MAX_PATH + 1] = {0};
const TCHAR * a_pszCur = p_pszList;
const TCHAR * a_pszSep = NULL;
do
{
// Remove excess whitespace
while( *a_pszCur == _T(' ') )
a_pszCur++;
a_pszSep = _tcspbrk( a_pszCur, p_pszSeps );
if( !a_pszSep )
{
a_pszSep = p_pszList + _tcslen( p_pszList );
}
if( a_pszSep - a_pszCur > 0 )
{
_tcsncpy( a_szItem, a_pszCur, a_pszSep - a_pszCur );
a_szItem[a_pszSep - a_pszCur] = _T('\0');
p_roOutput.push_back( a_szItem );
}
a_pszCur = a_pszSep;
if( !a_pszCur || !*a_pszCur )
break;
a_pszCur++;
}while( 1 );
}
//
// ------------------------------------------------------------------
//
CTCharString GetFullPath( const TCHAR * p_pszDirectory,
const TCHAR * p_pszRelative )
{
TCHAR a_szDrive[MAX_PATH + 1] = {0};
_tsplitpath( p_pszRelative, a_szDrive, NULL, NULL, NULL );
if( _tcslen( a_szDrive ) )
// The full path was already specified.
return p_pszRelative;
int a_iDirLen = _tcslen( p_pszDirectory );
TCHAR a_szRelativePath[MAX_PATH + 1] = {0};
_tcsncpy( a_szRelativePath, p_pszDirectory, min( MAX_PATH, a_iDirLen ) );
_tcsncpy( a_szRelativePath + a_iDirLen, p_pszRelative, min( MAX_PATH - a_iDirLen, _tcslen( p_pszRelative ) ) );
// Now get the full (non-relative) path to the project.
TCHAR a_szFullPath[MAX_PATH + 1] = {0};
GetFullPathName( a_szRelativePath, sizeof( a_szFullPath ), a_szFullPath, NULL );
return a_szFullPath;
}
//
// ------------------------------------------------------------------
//
TCHAR * ExtractString( TCHAR * p_pszOut, TCHAR * p_pszStart, TCHAR p_cSep )
{
// Find the first separator.
TCHAR * a_pszSep = _tcschr( p_pszStart, p_cSep );
if( !a_pszSep )
return p_pszStart;
a_pszSep++;
// Find the second separator.
TCHAR * a_pszEndSep = _tcschr( a_pszSep, p_cSep );
if( !a_pszEndSep )
return p_pszStart;
// Remove the end separator.
*a_pszEndSep = 0;
if( p_pszOut )
{
// Copy to the output.
_tcscpy( p_pszOut, a_pszSep );
// Return a pointer to the character just after the extracted string.
return a_pszEndSep+1;
}
else
// Return a pointer to the extracted string.
return a_pszSep;
}
//
// ------------------------------------------------------------------
//
void HandleComboBox( CComboBox * p_poCombo, CString & p_rsString )
{
p_poCombo->GetWindowText( p_rsString );
int a_iIndex = p_poCombo->FindStringExact( 0, p_rsString );
if( a_iIndex == CB_ERR )
{
if( !p_rsString.IsEmpty() )
{
// Insert a new string at the beginning.
p_poCombo->InsertString( 0, p_rsString );
p_poCombo->SetCurSel( 0 );
}
}
else
{
// Put the entry at the top of the list.
p_poCombo->DeleteString( a_iIndex );
p_poCombo->InsertString( 0, p_rsString );
p_poCombo->SetCurSel( 0 );
}
}
//
// ------------------------------------------------------------------
//
// WildcardCompare
// The original function (wildcmp) was written by Jack Handy,
// and the article can be found at the following URL:
// http://www.codeproject.com/string/wildcmp.asp
//
// I modified it to use TCHARs instead of chars,
// and to return bool instead of int.
bool WildcardCompare( const TCHAR *string, const TCHAR *wild )
{
const TCHAR *cp, *mp;
while ((*string) && (*wild != _T('*')))
{
if ((_totlower( *wild ) != _totlower( *string )) && (*wild != _T('?')))
{
return false;
}
wild++;
string++;
}
while (*string)
{
if (*wild == _T('*'))
{
if (!*++wild)
{
return true;
}
mp = wild;
cp = string+1;
}
else
if ((_totlower( *wild ) == _totlower( *string )) || (*wild == _T('?')))
{
wild++;
string++;
}
else
{
wild = mp;
string = cp++;
}
}
while (*wild == _T('*'))
{
wild++;
}
return !*wild;
}
//
// ------------------------------------------------------------------
//
bool GetRegKey( const TCHAR * p_pszKey, CRegKey & p_roKey )
{
// Get a handle to a buried registry key (i.e. "HKCU\Whatever\SomeMore\EvenMore", gives us a handle to the "EvenMore" key).
//
// Get a list of the keys in the string.
CStdStringArray a_oKeys;
ParseSeparatedList( p_pszKey, _T("\\"), a_oKeys );
if( !a_oKeys.size() )
return false;
HKEY a_hParentKey = NULL;
// The first key is the root.
CTCharString a_sKeyName = a_oKeys[0];
if( !a_sKeyName.compare( _T("HKEY_CURRENT_USER") ) )
a_hParentKey = HKEY_CURRENT_USER;
else
if( !a_sKeyName.compare( _T("HKEY_LOCAL_MACHINE") ) )
a_hParentKey = HKEY_LOCAL_MACHINE;
else
if( !a_sKeyName.compare( _T("HKEY_CLASSES_ROOT") ) )
a_hParentKey = HKEY_CLASSES_ROOT;
else
return false;
// We don't need the first key anymore.
a_oKeys.erase( a_oKeys.begin() );
// Jump down until we are at the end.
CRegKey a_oKey;
CRegKey a_oPrevKey;
for( int i = 0; i<a_oKeys.size(); ++i )
{
a_sKeyName = a_oKeys[i];
if( a_oKey.Open( a_hParentKey, a_sKeyName.c_str() ) != ERROR_SUCCESS )
return false;
if( a_oPrevKey.m_hKey )
a_oPrevKey.Close();
a_hParentKey = a_oKey;
a_oPrevKey = a_oKey;
a_oKey.Detach();
}
p_roKey = a_oPrevKey;
a_oPrevKey.Detach();
return true;
}
//
// ------------------------------------------------------------------
//
|
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.