bool CEntityManager::CompareNames(StdString searchString, StdString compString)
{
	static StdString token(_T("\\"));
	size_t subLen;
	// if searchString is null, positive match w/ everything
	if(_tcscmp(searchString, _T("")) == 0)
	{
		return true;
	}
	// if compString is null, no match
	if(_tcscmp(compString, _T("")) == 0)
	{
		return false;
	}
	const TCHAR* searchStringArray = searchString.c_str();
	const TCHAR* compStringArray = compString.c_str();

	while( _tcscmp(searchStringArray, _T("")) != 0 )
	{
		subLen = _tcscspn(compStringArray, token); //length of substring to token

		if (subLen > 0)
		{
			// compare substrings up to token 
			if( _tcsncmp(searchStringArray, compStringArray, subLen) == 0 )
			{
				return true;
			}

			// now check if compString's substring's next char is == token 
			if( compStringArray[subLen] != token.c_str()[0] )
			{
				// did not return above, therefore does not match
				return false;			
			}		

			// continue...advance ptr
			compStringArray = _tcsninc(compStringArray, subLen+1);
		}
		else
		{
			// string out
			return false;
		}
	}
	// if exited while loop..then true
	return true;
}
Example #2
0
void CXMLNode::UniformString(CString& str)
{
	// Non-alphanumeric characters which will not be ignored
	static LPCTSTR pszOK = _T("'-&/,;#()");

	str.Trim();
	BOOL bSpace = TRUE;

	for ( int nPos = 0 ; nPos < str.GetLength() ; nPos++ )
	{
		int nChar = (int)(unsigned short)str.GetAt( nPos );

		if ( nChar <= 32 )
		{
			if ( bSpace )
			{
				str = str.Left( nPos ) + str.Mid( nPos + 1 );
				nPos--;
			}
			else
			{
				if ( nChar != 32 ) str.SetAt( nPos, 32 );
				bSpace = TRUE;
			}
		}
		else if ( ! _istalnum( TCHAR( nChar ) ) && nChar < 0xC0 && _tcschr( pszOK, TCHAR( nChar ) ) == NULL )
		{
			if ( nPos == 0 || str.GetAt( nPos - 1 ) == ' ' )
				str = str.Left( nPos ) + str.Mid( nPos + 1 );
			else
			{
				LPTSTR pszTemp = _tcsninc( str, nPos );
				pszTemp[ 0 ] = ' ';
				bSpace = TRUE;
			}
		}
		else
		{
			bSpace = FALSE;
		}
	}
}
DWORD	LogDataToDisk(TCHAR *szModuleFilePath,
							TCHAR*	szData,
							BOOL bAlertData)
{
	TCHAR		szFullPathDirectory[MAX_PATH + 100]				=	{0};
	TCHAR		szFileName[MAX_PATH + 100]						=	{0};	
	TCHAR		szLogPath[MAX_PATH + 100]						=	{0};
	TCHAR		szTempLogPath[MAX_PATH + 100]					=	{0};
	TCHAR		szFullLogPath[MAX_PATH + 100]					=	{0};
	TCHAR		szMutexName[MAX_PATH + 100]						=	{0};
	TCHAR		szLogFileMapName[MAX_PATH + 100]				=	{0};
	TCHAR*		lpTraverser									=	NULL;
	TCHAR*		szFindChar									=	NULL;
	TCHAR*		pszString									=	NULL;
	TCHAR*		pszFinalError								=	NULL;
	TCHAR		chString									=	NULL;

	HANDLE		hFile										=	NULL;
	HANDLE		hMutex										=	NULL;

	DWORD		nResult										=	0;
	DWORD		dwNumOfBytesWritten							=	0;

	long		lRet										=	0;
	int			iCtr										=	0;
	int			iSubCtr										=	0;
	int			iLen										=	0;	

	BOOL		bFirstCreate								=	FALSE;

	__try
	{
		__try
		{
			if((NULL == szData)	||
				(NULL == szModuleFilePath))
			{
				return 1;
			}


			iLen = _tcslen(szModuleFilePath);
			if(0 == iLen)
			{
				return 1;
			}
			_tcscat(szLogFileMapName,_T("FILE_MAP_"));
			_tcscat(szMutexName,_T("FILE_MUTEX_"));

			for(iCtr = 0; iCtr < iLen; ++iCtr)
			{
				pszString = _tcsninc(szModuleFilePath,iCtr);	
				chString = _tcsnextc(pszString);
				if(_T('\\') == chString)
				{
					_tcscat(szLogFileMapName,_T("-"));
					_tcscat(szMutexName,_T("-"));
				}
				else
				{
					_tcsncat(szLogFileMapName,pszString,1);
					_tcsncat(szMutexName,pszString,1);
				}
			}


			//Create the Mutex and wait
			hMutex = CreateMutex(NULL,FALSE,szMutexName);
			if(NULL == hMutex)
			{
				return 1;
			}

			nResult = WaitForSingleObject(hMutex,(300 * 1000));
			if(WAIT_OBJECT_0 != nResult)
			{
				::ReleaseMutex(hMutex);
				CloseHandle(hMutex);
				hMutex = NULL;
				return 1;
			}
			

			_tcscpy(szFullLogPath,szModuleFilePath);
			//Remove the file name
			szFindChar = strrchr(szFullLogPath,'\\');
			if(NULL != szFindChar)
			{
				_tcscpy(szFileName,szFindChar);
				*szFindChar = NULL;
				//++szFindChar;
			}


			_tcscpy(szFullPathDirectory,szFullLogPath);
			_tcscat(szFullPathDirectory,szFileName);

			if(0 == ::SetFileAttributes(szFullPathDirectory,FILE_ATTRIBUTE_NORMAL))
			{
				CreateLogDirectory(szFullLogPath);
				bFirstCreate = TRUE;
			}
		
			//::SetFileAttributes(szFullPathDirectory,FILE_ATTRIBUTE_NORMAL);

			//Create or opens the existing log file
			hFile = CreateFile(szFullPathDirectory,
								GENERIC_READ | GENERIC_WRITE,
								FILE_SHARE_READ,
								NULL,
								OPEN_ALWAYS,
								FILE_ATTRIBUTE_NORMAL,
								NULL);
					
			if (hFile == INVALID_HANDLE_VALUE)
			{
				return 1;
			}


			
			if(TRUE == bFirstCreate)
			{
				if(FALSE == bAlertData)
				{
					::WriteFile(hFile,
								"DateTime\tApplication\tApp_Status\tMonitor\tCounter\tCounterStatus\r\n",
								lstrlen("DateTime\tApplication\tApp_Status\tMonitor\tCounter\tCounterStatus\r\n"),
								&dwNumOfBytesWritten,
								NULL);

					dwNumOfBytesWritten = 0;
				}
			}
			
			
			SetFilePointer(hFile, 0, NULL, FILE_END);

			::WriteFile(hFile,szData,lstrlen(szData),&dwNumOfBytesWritten,NULL);

			CloseHandle(hFile);
			hFile = NULL;


			::ReleaseMutex(hMutex);
			CloseHandle(hMutex);
			hMutex = NULL;



			return 0;
		}
		__except(EXCEPTION_EXECUTE_HANDLER)
		{
			//Its better to flush all the buffers
			//FlushFileBuffers(hFile);

			//Close the file handle
			if(NULL != hFile)
			{
				CloseHandle(hFile);
				hFile = NULL;
			}


			if(NULL != hMutex)
			{
				::ReleaseMutex(hMutex);
				CloseHandle(hMutex);
				hMutex = NULL;
			}

			return 1;
		}
	}

	__finally
	{
		//Its better to flush all the buffers
		//FlushFileBuffers(hFile);

		//Close the file handle
		if(NULL != hFile)
		{
			CloseHandle(hFile);
			hFile = NULL;
		}

		if(NULL != hMutex)
		{
			::ReleaseMutex(hMutex);
			CloseHandle(hMutex);
			hMutex = NULL;
		}

	}
	return 0;
}