2012-08-10 4 views
0

많은 언어를 지원하는 C 프로그램을 만들고 있습니다. 이 프로그램은 char 대신 WCHAR 유형을 사용하여 전자 메일을 보냅니다.유니 코드 문자가 올바르게 표시되지 않습니다.

<!-- language: lang-c --> 
curl_easy_setopt(hnd, CURLOPT_READFUNCTION, payload_source); 
curl_easy_setopt(hnd, CURLOPT_READDATA, &upload_ctx); 

static const WCHAR *payload_text[]={ 
    L"To: <[email protected]>\n", 
    L"From: <[email protected]>(Example User)\n", 
    L"Subject: Hello!\n", 
    L"\n", 
    L"Message sent\n", 
    NULL 
}; 

struct upload_status { 
    int lines_read; 
}; 

static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp){ 
    struct upload_status *upload_ctx = (struct upload_status *)userp; 
    const WCHAR *data; 

    if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) { 
     return 0; 
    } 

    data = payload_text[upload_ctx->lines_read]; 
    if (data) { 
     size_t len = wcslen(data); 
     memcpy(ptr, data, len); 
     upload_ctx->lines_read ++; 
     return len; 
    } 
    return 0; 
} 
+0

당신이 어떤 환경과 컴파일러를 사용 했습니까? – AlexLordThorsen

+0

또한 전자와 m은 어떻게 생겼습니까? 그들은 정상적인 성격보다 약간 강조점이 있습니까? – AlexLordThorsen

답변

0

memcpy()는, 바이트에서 작동하지 : 문제는 내가 이메일을 수신하고 그것을 읽을 때, 일부 문자가 올바르게 전자, m 같은 심지어 영어 사람을 표시하지 않는 것입니다 ... 이것은 예입니다 문자. 그 사람 sizeof(wchar_t) > 1을 고려하지 않았습니다. 일부 시스템에서는 2 바이트이고 다른 시스템에서는 4 바이트입니다. 이 descrepency wchar_t 이식 코드를 작성하는 나쁜 선택합니다. 대신 icu 또는 iconv와 같은 유니 코드 라이브러리를 사용해야합니다.

memcpy()으로 전화 할 때는 sizeof(wchar_t)을 고려해야합니다. 또한 대상 버퍼가 복사하려는 텍스트 바이트의 크기보다 작을 수 있음을 고려해야합니다. lines_read을 추적하는 것만으로는 충분하지 않습니다. 복사 한 현재 줄의 바이트 수를 추적해야하므로 현재 텍스트 줄이 여러 대상 버퍼에 걸쳐있을 때 처리 할 수 ​​있습니다.

더이 대신 같은 것을보십시오 :

static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp) 
{ 
    struct upload_status *upload_ctx = (struct upload_status *) userp; 
    unsigned char *buf = (unsignd char *) ptr; 
    size_t available = (size * nmemb); 
    size_t total = 0; 

    while (available > 0) 
    { 
     wchar_t *data = payload_text[upload_ctx->lines_read]; 
     if (!data) break; 

     unsigned char *rawdata = (unsigned char *) data; 

     size_t remaining = (wcslen(data) * sizeof(wchar_t)) - upload_ctx->line_bytes_read; 
     while ((remaining > 0) && (available > 0)) 
     { 
      size_t bytes_to_copy = min(remaining, available); 
      memcpy(buf, rawdata, bytes_to_copy); 

      buf += bytes_to_copy; 
      available -= bytes_to_copy; 
      total = bytes_to_copy; 

      rawdata += bytes_to_copy; 
      remaining -= bytes_to_copy; 

      upload_ctx->line_bytes_read += bytes_to_copy; 
     } 

     if (remaining < 1) 
     { 
      upload_ctx->lines_read ++; 
      upload_ctx->line_bytes_read = 0; 
     } 
    } 

    return total; 
} 
관련 문제