📄 plx_charset.c
字号:
}
else if (*lpUtf8 < 0x80)
{
if ( pCode != NULL)
*pCode = *lpUtf8;
(*lppUtf8)++;
(*pBufSize)--;
}
else
{
(*lppUtf8)++;
(*pBufSize)--;
goto lbl_nextbyte;
}
return TRUE;
}
int PlxMultiByteToUTF8(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr,
int cchMultiByte, LPSTR lpUtf8CharStr, int cUtf8Char)
{
// TEXTMETRIC tm;
int nSrcChars;
int nDstChars;
int nBytes;
char* lpSrc = NULL;
BYTE* lpDst = NULL;
LONG lDistance;
BOOL bForSize = FALSE;
PBYTEHUGE pCode;
WCHAR wCode;
BOOL bIsGb18030 = FALSE;
if (!InitConvChars())
{
ASSERT(0);
//SetLastError(1);
return 0;
}
if (CodePage != CP_ACP ||
dwFlags != 0 || lpMultiByteStr == NULL)
{
ASSERT(0);
//SetLastError(1);
return 0;
}
/* to get the required buffer size in wide characters */
if (cUtf8Char == 0)
bForSize = TRUE;
if (!bForSize && lpUtf8CharStr == 0)
{
/* null pointer,function fails */
ASSERT(0);
//SetLastError(1);
return 0;
}
/* lpMultiByteStr is a null-terminated string */
if (cchMultiByte == -1)
{
nSrcChars = strlen(lpMultiByteStr) + 1;
}
else
nSrcChars = cchMultiByte;
if (nSrcChars <= 0)
return 0;
/* check if default font is GB18030 */
// if (GetTextMetrics(NULL, &tm))
{
#if !defined(REMOVE_GB18030_SUPPORT)
if (tm.tmCharSet == CHARSET_GB18030)
bIsGb18030 = TRUE;
#endif // REMOVE_GB18030_SUPPORT
}
/***********************************************************************
*
* In GBK scope:
* high byte of the multi-byte character is between 0x81 and 0xfe,
* low byte of the multi-byte character is between 0x40 and 0xfe.
*
**********************************************************************/
lpSrc = (char *)lpMultiByteStr;
lpDst = (BYTE *)lpUtf8CharStr;
nBytes = 0;
nDstChars = 0;
while (1)
{
BYTE ch1, ch2, ch3, ch4;
int ret, count;
ret = CheckIsGbChar(lpSrc, nSrcChars - nBytes, bIsGb18030,
&ch1, &ch2, &ch3, &ch4);
switch (ret)
{
case 1:
// ASCII character
if (ch1 == 0x80) // for unicode 0x20AC
wCode = 0x20AC;
else
{
if (ch1 > 0x80) // wrong character
wCode = 0x20;
else
wCode = (WCHAR)ch1;
}
break;
case 2:
// 2 byte code
if (ch1 == 0xaa && (ch2 >= 0xa1 && ch2 <= 0xc8))
{
// GSM character in user-defined code area
lDistance = (ch2 - 0xa1) * 2;
pCode = (BYTE *)gb2gsm + lDistance;
}
else
{
lDistance = (ch1 - 0x81) * 191L * 2L + (ch2 - 0x40) * 2;
pCode = (PBYTEHUGE)pFileGb2Uni + lDistance;
}
wCode = *pCode + (*(pCode + 1) << 8);
break;
#if !defined(REMOVE_GB18030_SUPPORT)
case 4:
// 4 byte code
{
indextbl_t g2u;
lDistance = (((ch1 - 0x81) * 10L + (ch2 - 0x30)) * 126L +
ch3 - 0x81) * 10L + ch4 - 0x30;
if (lDistance > 0x99FBL)
return 0;
/* GB+81308130 - GB+8431A439 */
g2u = __gb18030_to_ucs_index[lDistance >> 8];
if ((lDistance & 0xFF) >= g2u.tblbegin &&
(lDistance & 0xFF) <= g2u.tblend)
wCode = __gb18030_4byte_to_ucs[lDistance - g2u.tbloffset];
else
wCode = g2u.algoffset + (lDistance & 0xFF);
}
break;
#endif // REMOVE_GB18030_SUPPORT
default:
return 0;
}
count = GetUTF8CharCount(wCode);
nDstChars += count;
if (!bForSize)
{
if (nDstChars > cUtf8Char)
return 0;
Unicode2UTF8(wCode, &lpDst);
}
lpSrc += ret;
nBytes += ret;
if (nBytes >= nSrcChars)
break;
}
return nDstChars;
}
#if !defined(REMOVE_GB18030_SUPPORT)
static int UTF8ToGB18030(UINT CodePage, DWORD dwFlags,
LPCSTR lpUtf8CharStr, int cUtf8Char,
LPSTR lpMultiByteStr, int cchMultiByte,
LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar)
{
WCHAR wCode;
int nSrcChars;
int nDstChars;
BYTE* lpSrc = NULL;
BYTE* lpDst = NULL;
BOOL bForSize = FALSE;
/* to get the required buffer size in multi bytes */
if (cchMultiByte == 0)
bForSize = TRUE;
if (CodePage != CP_ACP ||
dwFlags != 0 || lpUtf8CharStr == NULL ||
lpDefaultChar != NULL || lpUsedDefaultChar != NULL)
{
ASSERT(0);
//SetLastError(1);
return 0;
}
if (!bForSize && lpMultiByteStr == 0)
{
/* null pointer,function fails */
ASSERT(0);
//SetLastError(1);
return 0;
}
/* lpUtf8CharStr is a null-terminated string */
if (cUtf8Char == -1)
{
nSrcChars = strlen(lpUtf8CharStr) + 1;
}
else
nSrcChars = cUtf8Char;
lpSrc = (BYTE *)lpUtf8CharStr;
lpDst = (BYTE *)lpMultiByteStr;
nDstChars = 0;
while (UTF82Unicode(&lpSrc, &nSrcChars, &wCode))
{
int count;
count = Ucs2ToGB18030(wCode, &lpDst, &nDstChars,
cchMultiByte, bForSize);
if (count == 0)
return 0;
}
return nDstChars;
}
#endif // REMOVE_GB18030_SUPPORT
int PlxUTF8ToMultiByte(UINT CodePage, DWORD dwFlags, LPCSTR lpUtf8CharStr,
int cUtf8Char, LPSTR lpMultiByteStr, int cchMultiByte,
LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar)
{
WCHAR wCode;
BYTE *lpSrc;
int nSrcChars;
int nDstChars;
// TEXTMETRIC tm;
BOOL bIsGb18030 = FALSE;
// used as the flag whether need to return the converted result.
BOOL bForSize = (cchMultiByte == 0);
/* check if default font is GB18030 */
// if (GetTextMetrics(NULL, &tm))
{
#if !defined(REMOVE_GB18030_SUPPORT)
if (tm.tmCharSet == CHARSET_GB18030)
bIsGb18030 = TRUE;
#endif // REMOVE_GB18030_SUPPORT
}
#if !defined(REMOVE_GB18030_SUPPORT)
if (bIsGb18030)
{
return UTF8ToGB18030(CodePage, dwFlags, lpUtf8CharStr,
cUtf8Char, lpMultiByteStr, cchMultiByte,
lpDefaultChar, lpUsedDefaultChar);
}
#endif // REMOVE_GB18030_SUPPORT
if (!InitConvChars())
{
ASSERT(0);
//SetLastError(1);
return 0;
}
/* now we can handle only if:
* (CodePage = CP_ACP) && (dwFlags == 0) &&
* (lpDefaultChar != NULL) && (lpUsedDefaultChar == NULL)
*/
if (CodePage != CP_ACP ||
dwFlags != 0 || lpUtf8CharStr == NULL ||
lpDefaultChar != NULL || lpUsedDefaultChar != NULL)
{
ASSERT(0);
//SetLastError(1);
return 0;
}
if (!bForSize && lpMultiByteStr == 0)
{
/* null pointer,function fails */
ASSERT(0);
//SetLastError(1);
return 0;
}
/* lpUtf8CharStr is a null-terminated string */
if (cUtf8Char == -1)
{
nSrcChars = strlen(lpUtf8CharStr) + 1;
}
else
nSrcChars = cUtf8Char;
lpSrc = (BYTE *)lpUtf8CharStr;
nDstChars = 0;
while (UTF82Unicode(&lpSrc, &nSrcChars, &wCode))
{
int count;
count = Ucs2ToGB2312(CodePage, wCode, (BYTE *)lpMultiByteStr,
&nDstChars, &cchMultiByte, bForSize);
if (count == 0)
return 0;
}
return nDstChars;
}
/*********************************************************************\
* Function : hp_wcsncpy
* Purpose : Copy a string.
* Params :
* strDestination : Destination string
* strSource : Null-terminated source string
* Return :
* return the destination string
* Remarks
**********************************************************************/
/*static WCHAR *hp_wcsncpy(WCHAR *strDestination, const WCHAR *strSource, int count)
{
WCHAR* lpSrc;
WCHAR* lpDst;
int i = 0;
lpSrc = (WCHAR *)strSource;
lpDst = strDestination;
while (*lpSrc != 0)
{
*lpDst = *lpSrc;
lpSrc++;
lpDst++;
i ++;
if (i >= count)
break;
}
if (i < count)
*lpDst = 0;
return strDestination;
}
*/
/*********************************************************************\
* Function : hp_wcslen
* Purpose : calculate the wide-char number in wide-char string.
* Params :
* string : the string to be calculate length.
* Return :
* return the length of string.
* Remarks : this wide-char string must be terminated with 0x0.
**********************************************************************/
static int hp_wcslen(const WCHAR *string)
{
int count = 0;
while (string[count] != 0x0)
count++;
return count;
}
int PlxUTF8ToWideChar(LPCSTR lpUtf8Str, int cUtf8Len,
LPSTR lpWideCharStr, int cWideCharLen)
{
BYTE* lpSrc = (BYTE*)lpUtf8Str;
WCHAR *pwCode = (WCHAR *)lpWideCharStr;
int len = 0;
int lestbuflen = cWideCharLen - 2;
if ( cWideCharLen == 0 ) /*in order to get the lenghth of buffer needed*/
{
while (UTF82Unicode(&lpSrc, &cUtf8Len, NULL))
{
len += 2;
}
return len;
}
if ( len > lestbuflen)
{
return len;
}
while (UTF82Unicode(&lpSrc, &cUtf8Len, pwCode))
{
len += 2;
if ( len >= lestbuflen)
{
if (NULL != lpWideCharStr)
{
lpWideCharStr[len] = lpWideCharStr[len + 1]= 0;
}
return len;
}
if ( pwCode != NULL)
pwCode++;
}
if (NULL != lpWideCharStr)
{
lpWideCharStr[len] = lpWideCharStr[len + 1]= 0;
}
return len;
}
int PlxWideCharToUTF8(LPCSTR lpWideCharStr, int cWideCharLen,
LPSTR lpUtf8Str, int cUtf8Len)
{
BYTE *pUtf8 = (BYTE*)lpUtf8Str;
WCHAR *pwSrc = (WCHAR*)lpWideCharStr;
int len = 0;
if ( cUtf8Len == 0 )
{
while (cWideCharLen > 0)
{
int count = GetUTF8CharCount(*pwSrc);
pwSrc++;
cWideCharLen--;
len += count;
}
return len;
}
while (cWideCharLen > 0)
{
int count = GetUTF8CharCount(*pwSrc);
if ( (len + count) > cUtf8Len )
return len;
len += count;
if ( lpUtf8Str != NULL)
Unicode2UTF8(*pwSrc, &pUtf8);
pwSrc++;
cWideCharLen--;
}
lpUtf8Str[len] = 0;
return len;
}
#if defined(_MSC_VER)
#pragma warning( default : 4100 4127 4244 4701 )
#endif // _MSC_VER
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -