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