199 lines
3.6 KiB
C++
199 lines
3.6 KiB
C++
|
//---------------------------------------------------------------------------
|
||
|
//
|
||
|
// Microsoft Windows
|
||
|
// Copyright (C) Microsoft Corporation, 1992 - 1995
|
||
|
//
|
||
|
// File: sconv.cxx
|
||
|
//
|
||
|
// Contents: Ansi to Unicode conversions
|
||
|
//
|
||
|
// History: KrishnaG Jan 22 1996
|
||
|
//----------------------------------------------------------------------------
|
||
|
#include "procs.hxx"
|
||
|
#pragma hdrstop
|
||
|
|
||
|
#define NULL_TERMINATED 0
|
||
|
|
||
|
int
|
||
|
AnsiToUnicodeString(
|
||
|
LPSTR pAnsi,
|
||
|
LPWSTR pUnicode,
|
||
|
DWORD StringLength
|
||
|
)
|
||
|
{
|
||
|
int iReturn;
|
||
|
|
||
|
if( StringLength == NULL_TERMINATED )
|
||
|
StringLength = strlen( pAnsi );
|
||
|
|
||
|
iReturn = MultiByteToWideChar(CP_ACP,
|
||
|
MB_PRECOMPOSED,
|
||
|
pAnsi,
|
||
|
StringLength + 1,
|
||
|
pUnicode,
|
||
|
StringLength + 1 );
|
||
|
|
||
|
//
|
||
|
// Ensure NULL termination.
|
||
|
//
|
||
|
pUnicode[StringLength] = 0;
|
||
|
|
||
|
return iReturn;
|
||
|
}
|
||
|
|
||
|
|
||
|
int
|
||
|
UnicodeToAnsiString(
|
||
|
LPWSTR pUnicode,
|
||
|
LPSTR pAnsi,
|
||
|
DWORD StringLength
|
||
|
)
|
||
|
{
|
||
|
LPSTR pTempBuf = NULL;
|
||
|
INT rc = 0;
|
||
|
|
||
|
if( StringLength == NULL_TERMINATED ) {
|
||
|
|
||
|
//
|
||
|
// StringLength is just the
|
||
|
// number of characters in the string
|
||
|
//
|
||
|
StringLength = wcslen( pUnicode );
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// WideCharToMultiByte doesn't NULL terminate if we're copying
|
||
|
// just part of the string, so terminate here.
|
||
|
//
|
||
|
|
||
|
pUnicode[StringLength] = 0;
|
||
|
|
||
|
//
|
||
|
// Include one for the NULL
|
||
|
//
|
||
|
StringLength++;
|
||
|
|
||
|
//
|
||
|
// Unfortunately, WideCharToMultiByte doesn't do conversion in place,
|
||
|
// so allocate a temporary buffer, which we can then copy:
|
||
|
//
|
||
|
|
||
|
if( pAnsi == (LPSTR)pUnicode )
|
||
|
{
|
||
|
pTempBuf = (LPSTR)LocalAlloc( LPTR, StringLength * 2);
|
||
|
pAnsi = pTempBuf;
|
||
|
}
|
||
|
|
||
|
if( pAnsi )
|
||
|
{
|
||
|
rc = WideCharToMultiByte( CP_ACP,
|
||
|
0,
|
||
|
pUnicode,
|
||
|
StringLength,
|
||
|
pAnsi,
|
||
|
StringLength*2,
|
||
|
NULL,
|
||
|
NULL );
|
||
|
}
|
||
|
|
||
|
/* If pTempBuf is non-null, we must copy the resulting string
|
||
|
* so that it looks as if we did it in place:
|
||
|
*/
|
||
|
if( pTempBuf && ( rc > 0 ) )
|
||
|
{
|
||
|
pAnsi = (LPSTR)pUnicode;
|
||
|
strcpy( pAnsi, pTempBuf );
|
||
|
LocalFree( pTempBuf );
|
||
|
}
|
||
|
|
||
|
return rc;
|
||
|
}
|
||
|
|
||
|
|
||
|
LPWSTR
|
||
|
AllocateUnicodeString(
|
||
|
LPSTR pAnsiString
|
||
|
)
|
||
|
{
|
||
|
LPWSTR pUnicodeString = NULL;
|
||
|
|
||
|
if (!pAnsiString)
|
||
|
return NULL;
|
||
|
|
||
|
pUnicodeString = (LPWSTR)LocalAlloc(
|
||
|
LPTR,
|
||
|
strlen(pAnsiString)*sizeof(WCHAR) +sizeof(WCHAR)
|
||
|
);
|
||
|
|
||
|
if (pUnicodeString) {
|
||
|
|
||
|
AnsiToUnicodeString(
|
||
|
pAnsiString,
|
||
|
pUnicodeString,
|
||
|
NULL_TERMINATED
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return pUnicodeString;
|
||
|
}
|
||
|
|
||
|
|
||
|
void
|
||
|
FreeUnicodeString(
|
||
|
LPWSTR pUnicodeString
|
||
|
)
|
||
|
{
|
||
|
if (!pUnicodeString)
|
||
|
return;
|
||
|
|
||
|
LocalFree(pUnicodeString);
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
|
||
|
DWORD
|
||
|
ComputeMaxStrlenW(
|
||
|
LPWSTR pString,
|
||
|
DWORD cchBufMax
|
||
|
)
|
||
|
{
|
||
|
DWORD cchLen;
|
||
|
|
||
|
//
|
||
|
// Include space for the NULL.
|
||
|
//
|
||
|
|
||
|
cchBufMax--;
|
||
|
|
||
|
cchLen = wcslen(pString);
|
||
|
|
||
|
if (cchLen > cchBufMax)
|
||
|
return cchBufMax;
|
||
|
|
||
|
return cchLen;
|
||
|
}
|
||
|
|
||
|
|
||
|
DWORD
|
||
|
ComputeMaxStrlenA(
|
||
|
LPSTR pString,
|
||
|
DWORD cchBufMax
|
||
|
)
|
||
|
{
|
||
|
DWORD cchLen;
|
||
|
|
||
|
//
|
||
|
// Include space for the NULL.
|
||
|
//
|
||
|
cchBufMax--;
|
||
|
|
||
|
cchLen = lstrlenA(pString);
|
||
|
|
||
|
if (cchLen > cchBufMax)
|
||
|
return cchBufMax;
|
||
|
|
||
|
return cchLen;
|
||
|
}
|
||
|
|