Trim

개발/연구 / / 2013. 8. 29. 16:08
#ifdef _UNICODE
#define Trim TrimW
#else
#define Trim TrimA
#endif

DWORD TrimW(wchar_t* pszTarget)
{
	BOOL bFirstCheck = FALSE;
	BOOL bContSpace = FALSE;
	wchar_t* PtrRef = pszTarget;
	wchar_t* PtrMrk = pszTarget;

	if ( pszTarget == NULL )
		return ERROR_INVALID_PARAMETER;

	while( *PtrRef )
	{
		if ( !bFirstCheck )
		{
			if ( (*PtrRef) == _T(' ') )
			{
				PtrRef++;
				continue;
			}
			else
			{
				*PtrMrk = *PtrRef;
				bFirstCheck = TRUE;
			}
		}
		else
		{
			if ( (*PtrRef) == _T(' ') )
			{
				if ( bContSpace )
				{
					PtrRef++;
					continue;
				}
				else
				{
					bContSpace = TRUE;
					*PtrMrk = *PtrRef;
				}
			}
			else
			{
				*PtrMrk = *PtrRef;
				bContSpace = FALSE;
			}
		}
		PtrRef++;
		PtrMrk++;
	}


	*PtrMrk = _T('\0');
	PtrMrk--;

	while ( (PtrMrk > pszTarget) && (*PtrMrk) == _T(' ') )
	{
		*PtrMrk = _T('\0');
		PtrMrk--;
	}

	return ERROR_SUCCESS;
}


DWORD TrimA(char* pszTarget)
{
	DWORD dwError = ERROR_SUCCESS;

	if ( pszTarget != NULL )
	{
		int NeedSize = MultiByteToWideChar(CP_ACP, 0, pszTarget, -1, NULL, NULL);
		TCHAR* pBuffer = new TCHAR[NeedSize+1];
		if ( pBuffer )
		{
			if ( MultiByteToWideChar(CP_ACP, 0, pszTarget, -1, pBuffer, NeedSize) )
			{
				if ( TrimW(pBuffer) == ERROR_SUCCESS )
				{
					if ( WideCharToMultiByte(CP_ACP, 0, pBuffer, -1, pszTarget, (int)strlen(pszTarget), NULL, NULL) )
					{

					}
					else
						dwError = GetLastError();
				}
				else
					dwError = GetLastError();
			}
			else
				dwError = GetLastError();

			delete[] pBuffer;
		}
		else
			dwError = ERROR_NOT_ENOUGH_MEMORY;
	}
	else
		dwError = ERROR_INVALID_PARAMETER;

	return dwError;
}
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기