#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; }
최근 댓글