📄 w32mac.cpp
字号:
//
// Member: CStrIn::Init
//
// Synopsis: Converts a LPWSTR function argument to a LPSTR.
//
// Arguments: [pwstr] -- The function argument. May be NULL or an atom
// (HIWORD(pwstr) == 0).
//
// [cwch] -- The number of characters in the string to
// convert. If -1, the string is assumed to be
// NULL terminated and its length is calculated.
//
// Modifies: [this]
//
//----------------------------------------------------------------------------
void
CStrIn::Init(LPCWSTR pwstr, int cwch)
{
TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "CStrIn::Init");
int cchBufReq;
_cchLen = 0;
// Check if string is NULL or an atom.
if (HIWORD(pwstr) == 0)
{
_pstr = (LPSTR) pwstr;
return;
}
Assert(cwch == -1 || cwch > 0);
//
// Convert string to preallocated buffer, and return if successful.
//
_cchLen = W32->MbcsFromUnicode(_ach, ARRAY_SIZE(_ach), pwstr, cwch);
if (_cchLen > 0)
{
if(_ach[_cchLen-1] == 0)
_cchLen--; // account for terminator
_pstr = _ach;
return;
}
//
// Alloc space on heap for buffer.
//
TRACEINFOSZ("CStrIn: Allocating buffer for wrapped function argument.");
cchBufReq = WideCharToMultiByte(
CP_ACP, 0, pwstr, cwch, NULL, 0, NULL, NULL);
Assert(cchBufReq > 0);
_pstr = new char[cchBufReq];
if (!_pstr)
{
// On failure, the argument will point to the empty string.
TRACEINFOSZ("CStrIn: No heap space for wrapped function argument.");
_ach[0] = 0;
_pstr = _ach;
return;
}
Assert(HIWORD(_pstr));
_cchLen = -1 + W32->MbcsFromUnicode(_pstr, cchBufReq, pwstr, cwch);
Assert(_cchLen >= 0);
}
//+---------------------------------------------------------------------------
//
// Class: CStrInMulti (CStrIM)
//
// Purpose: Converts multiple strings which are terminated by two NULLs,
// e.g. "Foo\0Bar\0\0"
//
//----------------------------------------------------------------------------
class CStrInMulti : public CStrIn
{
public:
CStrInMulti(LPCWSTR pwstr);
};
//+---------------------------------------------------------------------------
//
// Member: CStrInMulti::CStrInMulti
//
// Synopsis: Converts mulitple LPWSTRs to a multiple LPSTRs.
//
// Arguments: [pwstr] -- The strings to convert.
//
// Modifies: [this]
//
//----------------------------------------------------------------------------
CStrInMulti::CStrInMulti(LPCWSTR pwstr)
{
TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "CStrInMulti::CStrInMulti");
LPCWSTR pwstrT;
// We don't handle atoms because we don't need to.
Assert(HIWORD(pwstr));
//
// Count number of characters to convert.
//
pwstrT = pwstr;
if (pwstr)
{
do {
while (*pwstrT++)
;
} while (*pwstrT++);
}
Init(pwstr, pwstrT - pwstr);
}
//+---------------------------------------------------------------------------
//
// Member: CStrOut::CStrOut
//
// Synopsis: Allocates enough space for an out buffer.
//
// Arguments: [pwstr] -- The Unicode buffer to convert to when destroyed.
// May be NULL.
//
// [cwchBuf] -- The size of the buffer in characters.
//
// Modifies: [this].
//
//----------------------------------------------------------------------------
CStrOut::CStrOut(LPWSTR pwstr, int cwchBuf)
{
TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "CStrOut::CStrOut");
Assert(cwchBuf >= 0);
_pwstr = pwstr;
_cwchBuf = cwchBuf;
if (!pwstr)
{
Assert(cwchBuf == 0);
_pstr = NULL;
return;
}
Assert(HIWORD(pwstr));
// Initialize buffer in case Windows API returns an error.
_ach[0] = 0;
// Use preallocated buffer if big enough.
if (cwchBuf * 2 <= ARRAY_SIZE(_ach))
{
_pstr = _ach;
return;
}
// Allocate buffer.
TRACEINFOSZ("CStrOut: Allocating buffer for wrapped function argument.");
_pstr = new char[cwchBuf * 2];
if (!_pstr)
{
//
// On failure, the argument will point to a zero-sized buffer initialized
// to the empty string. This should cause the Windows API to fail.
//
TRACEINFOSZ("CStrOut: No heap space for wrapped function argument.");
Assert(cwchBuf > 0);
_pwstr[0] = 0;
_cwchBuf = 0;
_pstr = _ach;
return;
}
Assert(HIWORD(_pstr));
_pstr[0] = 0;
}
//+---------------------------------------------------------------------------
//
// Member: CStrOut::Convert
//
// Synopsis: Converts the buffer from MBCS to Unicode.
//
//----------------------------------------------------------------------------
int
CStrOut::Convert()
{
TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "CStrOut::Convert");
int cch;
if (!_pstr)
return 0;
cch = MultiByteToWideChar(CP_ACP, 0, _pstr, -1, _pwstr, _cwchBuf);
Assert(cch > 0 || _cwchBuf == 0);
Free();
return cch - 1;
}
//+---------------------------------------------------------------------------
//
// Member: CStrOut::~CStrOut
//
// Synopsis: Converts the buffer from MBCS to Unicode.
//
// Note: Don't inline this function, or you'll increase code size as
// both Convert() and CConvertStr::~CConvertStr will be called
// inline.
//
//----------------------------------------------------------------------------
CStrOut::~CStrOut()
{
TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "CStrOut::~CStrOut");
Convert();
}
//
// MultiByte --> UNICODE routins
//
//+---------------------------------------------------------------------------
//
// Member: CConvertStr::Free
//
// Synopsis: Frees string if alloc'd and initializes to NULL.
//
//----------------------------------------------------------------------------
void
CConvertStr::Free()
{
TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "CConvertStr::Free");
if (_pstr != _ach && HIWORD(_pstr) != 0)
{
delete [] _pstr;
}
_pstr = NULL;
}
//+---------------------------------------------------------------------------
//
// Member: CConvertStrW::Free
//
// Synopsis: Frees string if alloc'd and initializes to NULL.
//
//----------------------------------------------------------------------------
void
CConvertStrW::Free()
{
TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "CConvertStrW::Free");
if (_pwstr != _awch && HIWORD(_pwstr) != 0 )
{
delete [] _pwstr;
}
_pwstr = NULL;
}
//+---------------------------------------------------------------------------
//
// Member: CStrInW::CStrInW
//
// Synopsis: Inits the class.
//
// NOTE: Don't inline these functions or you'll increase code size
// by pushing -1 on the stack for each call.
//
//----------------------------------------------------------------------------
CStrInW::CStrInW(LPCSTR pstr)
{
TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "CStrInW::CStrInW");
Init(pstr, -1, CP_ACP);
}
CStrInW::CStrInW(LPCSTR pstr, UINT uiCodePage)
{
TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "CStrInW::CStrInW");
Init(pstr, -1, uiCodePage);
}
CStrInW::CStrInW(LPCSTR pstr, int cch, UINT uiCodePage)
{
TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "CStrInW::CStrInW");
Init(pstr, cch, uiCodePage);
}
//+---------------------------------------------------------------------------
//
// Member: CStrInW::Init
//
// Synopsis: Converts a LPSTR function argument to a LPWSTR.
//
// Arguments: [pstr] -- The function argument. May be NULL or an atom
// (HIWORD(pwstr) == 0).
//
// [cch] -- The number of characters in the string to
// convert. If -1, the string is assumed to be
// NULL terminated and its length is calculated.
//
// Modifies: [this]
//
//----------------------------------------------------------------------------
void
CStrInW::Init(LPCSTR pstr, int cch, UINT uiCodePage)
{
TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "CStrInW::Init");
int cchBufReq;
_cwchLen = 0;
// Check if string is NULL or an atom.
if (HIWORD(pstr) == 0)
{
_pwstr = (LPWSTR) pstr;
return;
}
Assert(cch == -1 || cch > 0);
//
// Convert string to preallocated buffer, and return if successful.
//
_cwchLen = MultiByteToWideChar(
uiCodePage, 0, pstr, cch, _awch, ARRAY_SIZE(_awch));
if (_cwchLen > 0)
{
if(_awch[_cwchLen-1] == 0)
_cwchLen--; // account for terminator
_pwstr = _awch;
return;
}
//
// Alloc space on heap for buffer.
//
TRACEINFOSZ("CStrInW: Allocating buffer for wrapped function argument.");
cchBufReq = MultiByteToWideChar(
CP_ACP, 0, pstr, cch, NULL, 0);
Assert(cchBufReq > 0);
_pwstr = new WCHAR[cchBufReq];
if (!_pwstr)
{
// On failure, the argument will point to the empty string.
TRACEINFOSZ("CStrInW: No heap space for wrapped function argument.");
_awch[0] = 0;
_pwstr = _awch;
return;
}
Assert(HIWORD(_pwstr));
_cwchLen = -1 + MultiByteToWideChar(
uiCodePage, 0, pstr, cch, _pwstr, cchBufReq);
Assert(_cwchLen >= 0);
}
//+---------------------------------------------------------------------------
//
// Member: CStrOutW::CStrOutW
//
// Synopsis: Allocates enough space for an out buffer.
//
// Arguments: [pstr] -- The ansi buffer to convert to when destroyed.
// May be NULL.
//
// [cchBuf] -- The size of the buffer in characters.
//
// Modifies: [this].
//
//----------------------------------------------------------------------------
CStrOutW::CStrOutW(LPSTR pstr, int cchBuf, UINT uiCodePage)
{
TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "CStrOutW::CStrOutW");
Assert(cchBuf >= 0);
_pstr = pstr;
_cchBuf = cchBuf;
_uiCodePage = uiCodePage;
if (!pstr)
{
Assert(cchBuf == 0);
_pwstr = NULL;
return;
}
Assert(HIWORD(pstr));
// Initialize buffer in case Windows API returns an error.
_awch[0] = 0;
// Use preallocated buffer if big enough.
if (cchBuf <= ARRAY_SIZE(_awch))
{
_pwstr = _awch;
return;
}
// Allocate buffer.
TRACEINFOSZ("CStrOutW: Allocating buffer for wrapped function argument.");
_pwstr = new WCHAR[cchBuf * 2];
if (!_pwstr)
{
//
// On failure, the argument will point to a zero-sized buffer initialized
// to the empty string. This should cause the Windows API to fail.
//
TRACEINFOSZ("CStrOutW: No heap space for wrapped function argument.");
Assert(cchBuf > 0);
_pstr[0] = 0;
_cchBuf = 0;
_pwstr = _awch;
return;
}
Assert(HIWORD(_pwstr));
_pwstr[0] = 0;
}
//+---------------------------------------------------------------------------
//
// Member: CStrOutW::Convert
//
// Synopsis: Converts the buffer from Unicode to MBCS
//
//----------------------------------------------------------------------------
int
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -