handle the errors in MB2WC/WC2MB gracefully in wxString ctors

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@17480 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2002-10-07 23:11:27 +00:00
parent c9bd8aeb25
commit 6bc69f3752

View File

@@ -229,24 +229,37 @@ wxString::wxString(const void *pStart, const void *pEnd)
// from multibyte string
wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
{
// first get necessary size
size_t nLen = psz ? conv.MB2WC((wchar_t *) NULL, psz, 0) : 0;
// nLength is number of *Unicode* characters here!
if ((nLen != (size_t)-1) && (nLen > nLength))
nLen = nLength;
// empty?
if ( (nLen != 0) && (nLen != (size_t)-1) ) {
if ( !AllocBuffer(nLen) ) {
wxFAIL_MSG( _T("out of memory in wxString::wxString") );
return;
// first get the size of the buffer we need
size_t nLen;
if ( psz )
{
// calculate the needed size ourselves or use a provide one
nLen = nLength == wxSTRING_MAXLEN ? conv.MB2WC(NULL, psz, 0) : nLength;
}
conv.MB2WC(m_pchData, psz, nLen);
}
else {
else
{
// nothing to convert
nLen = 0;
}
// anything to do?
if ( (nLen != 0) && (nLen != (size_t)-1) )
{
if ( !AllocBuffer(nLen) )
{
wxFAIL_MSG( _T("out of memory in wxString::wxString") );
return;
}
if ( conv.MB2WC(m_pchData, psz, nLen) != (size_t)-1 )
{
// initialized ok
return;
}
//else: the conversion failed -- leave the string empty (what else?)
}
Init();
}
}
#else // ANSI
@@ -255,27 +268,37 @@ wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
// from wide string
wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength)
{
// first get necessary size
size_t nLen = 0;
if (pwz)
{
if (nLength == wxSTRING_MAXLEN)
nLen = conv.WC2MB((char *) NULL, pwz, 0);
else
nLen = nLength;
}
// empty?
if ( (nLen != 0) && (nLen != (size_t)-1) ) {
if ( !AllocBuffer(nLen) ) {
wxFAIL_MSG( _T("out of memory in wxString::wxString") );
return;
// first get the size of the buffer we need
size_t nLen;
if ( pwz )
{
// calculate the needed size ourselves or use a provide one
nLen = nLength == wxSTRING_MAXLEN ? conv.WC2MB(NULL, pwz, 0) : nLength;
}
conv.WC2MB(m_pchData, pwz, nLen);
}
else {
else
{
// nothing to convert
nLen = 0;
}
// anything to do?
if ( (nLen != 0) && (nLen != (size_t)-1) )
{
if ( !AllocBuffer(nLen) )
{
wxFAIL_MSG( _T("out of memory in wxString::wxString") );
return;
}
if ( conv.WC2MB(m_pchData, pwz, nLen) != (size_t)-1 )
{
// initialized ok
return;
}
//else: the conversion failed -- leave the string empty (what else?)
}
Init();
}
}
#endif // wxUSE_WCHAR_T