Changeset 36428 in vbox for trunk/src/VBox/Main/glue
- Timestamp:
- Mar 25, 2011 12:46:45 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/glue/string.cpp
r35128 r36428 53 53 size_t cwc; 54 54 int vrc = ::RTStrCalcUtf16LenEx(a_pszSrc, a_cchMax, &cwc); 55 AssertRCReturnVoid(vrc); /* throw instead? */ 55 if (RT_FAILURE(vrc)) 56 { 57 /* ASSUME: input is valid Utf-8. Fake out of memory error. */ 58 AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTStrNLen(a_pszSrc, a_cchMax), a_pszSrc)); 59 throw std::bad_alloc(); 60 } 56 61 57 62 m_bstr = ::SysAllocStringByteLen(NULL, cwc * sizeof(OLECHAR)); 58 if (m_bstr) 63 if (RT_UNLIKELY(!m_bstr)) 64 throw std::bad_alloc(); 65 66 PRTUTF16 pwsz = (PRTUTF16)m_bstr; 67 vrc = ::RTStrToUtf16Ex(a_pszSrc, a_cchMax, &pwsz, cwc + 1, NULL); 68 if (RT_FAILURE(vrc)) 59 69 { 60 PRTUTF16 pwsz = (PRTUTF16)m_bstr; 61 vrc = ::RTStrToUtf16Ex(a_pszSrc, a_cchMax, &pwsz, cwc + 1, NULL); 62 if (RT_FAILURE(vrc)) 63 { 64 /* This should not happen! */ 65 AssertRC(vrc); 66 cleanup(); 67 } 70 /* This should not happen! */ 71 AssertRC(vrc); 72 cleanup(); 73 throw std::bad_alloc(); 68 74 } 69 else70 throw std::bad_alloc();71 75 } 72 76 … … 80 84 size_t cb = length() + 1; 81 85 *pstr = (char*)nsMemory::Alloc(cb); 82 if ( !*pstr)86 if (RT_UNLIKELY(!*pstr)) 83 87 throw std::bad_alloc(); 84 88 memcpy(*pstr, c_str(), cb); … … 137 141 * copying from a UTF-16 string. 138 142 * 139 * As with the iprt::ministring::copyFrom() variants, this unconditionally 140 * sets the members to a copy of the given other strings and makes141 * no assumptions about previous contents. This can therefore be used142 * both in copy constructors, when member variables have no defined143 * value, and inassignments after having called cleanup().143 * As with the iprt::ministring::copyFrom() variants, this unconditionally sets 144 * the members to a copy of the given other strings and makes no assumptions 145 * about previous contents. This can therefore be used both in copy 146 * constructors, when member variables have no defined value, and in 147 * assignments after having called cleanup(). 144 148 * 145 149 * This variant converts from a UTF-16 string, most probably from 146 150 * a Bstr assignment. 147 151 * 148 * @param s 152 * @param a_pbstr The source string. The caller guarantees that this 153 * is valid UTF-16. 154 * 155 * @sa iprt::MiniString::copyFromN 149 156 */ 150 void Utf8Str::copyFrom(CBSTR s)157 void Utf8Str::copyFrom(CBSTR a_pbstr) 151 158 { 152 if ( s && *s)159 if (a_pbstr && *a_pbstr) 153 160 { 154 int vrc = RTUtf16ToUtf8Ex((P RTUTF16)s, // PCRTUTF16 pwszString161 int vrc = RTUtf16ToUtf8Ex((PCRTUTF16)a_pbstr, 155 162 RTSTR_MAX, // size_t cwcString: translate entire string 156 163 &m_psz, // char **ppsz: output buffer 157 164 0, // size_t cch: if 0, func allocates buffer in *ppsz 158 165 &m_cch); // size_t *pcch: receives the size of the output string, excluding the terminator. 159 if (RT_FAILURE(vrc)) 166 if (RT_SUCCESS(vrc)) 167 m_cbAllocated = m_cch + 1; 168 else 160 169 { 161 if ( vrc == VERR_NO_STR_MEMORY 162 || vrc == VERR_NO_MEMORY 163 ) 164 throw std::bad_alloc(); 170 if ( vrc != VERR_NO_STR_MEMORY 171 && vrc != VERR_NO_MEMORY) 172 { 173 /* ASSUME: input is valid Utf-16. Fake out of memory error. */ 174 AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTUtf16Len(a_pbstr) * sizeof(RTUTF16), a_pbstr)); 175 } 165 176 166 // @todo what do we do with bad input strings? throw also? for now just keep an empty string167 177 m_cch = 0; 168 178 m_cbAllocated = 0; 169 179 m_psz = NULL; 180 181 throw std::bad_alloc(); 170 182 } 171 else172 m_cbAllocated = m_cch + 1;173 183 } 174 184 else
Note:
See TracChangeset
for help on using the changeset viewer.