2014-10-29 1 views
0

여기 내 상황이 있습니다. 사용자가 단어에서 붙여 넣거나 문서를 Excel에서 제외 할 수있는 잘못된 문자를 필터링해야하는 문제가 있습니다.C++ - 사용자가 그리드에 붙여 넣을 때 잘못된 문자 제거

내가하는 일은 다음과 같습니다.

우선 나는 지금이 31

String __fastcall TMainForm::filterInput(String l_sConversion) 
{ 
    // Used to store every character that was stripped out. 
    String filterChars = ""; 

    // Not Used. We never received the whitelist 
    String l_SWhiteList = ""; 

    // Our String without the invalid characters. 
    AnsiString l_stempString; 

    // convert the string into an array of chars 
    wchar_t* outputChars = l_sConversion.w_str(); 
    char * pszOutputString = NULL; 

    //convert any unicode characters to ASCII 
    ConvertUnicodeToAscii(outputChars, pszOutputString); 

    l_stempString = (AnsiString)pszOutputString; 

    //We're going backwards since we are removing characters which changes the length and position. 
    for (int i = l_stempString.Length(); i > 0; i--) 
    { 
     char l_sCurrentChar = l_stempString[i]; 

     //If we don't have a valid character, filter it out of the string. 
     if (((unsigned int)l_sCurrentChar < 31) ||((unsigned int)l_sCurrentChar > 127)) 
     { 
      String l_sSecondHalf = ""; 
      String l_sFirstHalf = ""; 
      l_sSecondHalf = l_stempString.SubString(i + 1, l_stempString.Length() - i); 
      l_sFirstHalf = l_stempString.SubString(0, i - 1); 
      l_stempString = l_sFirstHalf + l_sSecondHalf; 
      filterChars += "\'" + ((String)(unsigned int)(l_sCurrentChar)) + "\' "; 
     } 
    } 

    if (filterChars.Length() > 0) 
    { 
     LogInformation(__LINE__, __FUNC__, Utilities::LOG_CATEGORY_GENERAL, "The Following ASCII Values were filtered from the string: " + filterChars); 
    } 

    // Delete the char* to avoid memory leaks. 
    delete [] pszOutputString; 
    return l_stempString; 
} 

127 사이의 값이없는 모든 문자를 필터링하고있어

extern "C" COMMON_STRING_FUNCTIONS long ConvertUnicodeToAscii(wchar_t * pwcUnicodeString, char* &pszAsciiString) 
{ 
    int nBufLen = WideCharToMultiByte(CP_ACP, 0, pwcUnicodeString, -1, NULL, 0, NULL, NULL)+1; 
    pszAsciiString = new char[nBufLen]; 
    WideCharToMultiByte(CP_ACP, 0, pwcUnicodeString, -1, pszAsciiString, nBufLen, NULL, NULL); 
    return nBufLen; 
} 

다음을 ASCII로 유니 코드 문자를 변환하려고 해요 단어 문서에서 글 머리 기호를 복사하려고하면 제외하고 작동하는 것 같습니다.

o Bullet1 :
 subbullet1.

oBullet1? subbullet1 같은 것을 얻을 것이다.

내 필터 기능은 onchange 이벤트에서 호출됩니다.

글 머리 기호는 값 o와 물음표로 대체됩니다.

내가 뭘 잘못하고 있고, 이것을 시도하는 더 좋은 방법이있다.

저는 Visual C++ 솔루션을 사용하지 않으므로 C++ 빌더 XE5를 사용하고 있습니다. 당신이 (실제로 BTW, ASCII로 변환하지입니다) ASCII로 변환을 수행 할 때

+0

는'CP_ACP'는 ASCII를 나타내지 않는, 그것은 어떤 언어가 될 수있는 OS의 현재 로케일을 나타냅니다

이와 비슷한 더 많은 것을보십시오. ASCII 자체는 대신 코드 페이지 20127입니다. 또한'AnsiStringT <20127>'을 대신 사용할 수 있고 RTL이 변환을 처리 할 수있을 때 자신의 변환 함수를 정의하는 것은 불필요합니다. –

답변

0

는 목표 코드 페이지에서 지원하지 않는 유니 코드 문자가 손실됩니다 - 중 ?로 대체, 감소, 또는 가까운으로 대체 근사값 - 스캔 루프에서 사용할 수 없습니다. 변환을 전혀해서는 안되며 대신 소스 유니 코드 데이터를 그대로 스캔하십시오.

#include <System.Character.hpp> 

String __fastcall TMainForm::filterInput(String l_sConversion) 
{ 
    // Used to store every character sequence that was stripped out. 
    String filterChars; 

    // Not Used. We never received the whitelist 
    String l_SWhiteList; 

    // Our String without the invalid sequences. 
    String l_stempString; 

    int numChars; 
    for (int i = 1; i <= l_sConversion.Length(); i += numChars) 
    { 
     UCS4Char ch = TCharacter::ConvertToUtf32(l_sConversion, i, numChars); 
     String seq = l_sConversion.SubString(i, numChars); 

     //If we don't have a valid codepoint, filter it out of the string. 
     if ((ch <= 31) || (ch >= 127)) 
      filterChars += (_D("\'") + seq + _D("\' ")); 
     else 
      l_stempString += seq; 
    } 

    if (!filterChars.IsEmpty()) 
    { 
     LogInformation(__LINE__, __FUNC__, Utilities::LOG_CATEGORY_GENERAL, _D("The Following Values were filtered from the string: ") + filterChars); 
    } 

    return l_stempString; 
} 
+0

도움 주셔서 감사합니다. 당신이 썼던 것이 대부분의 총알을 제거하는 것처럼 보이지만 명확한 총알은 여전히 ​​'o'로 변환됩니다. 이것은 Windows 복사/붙여 넣기 동작입니다. – themaniac27

+0

ASCII에 글 머리 기호가 없으므로이 코드는 모든 글 머리표를 제거해야합니다. –

관련 문제