string.cpp revision acb85e6b36e32c7960c769bde511f0bade006c3d
/* $Id$ */
/** @file
* MS COM / XPCOM Abstraction Layer - UTF-8 and UTF-16 string classes.
*/
/*
* Copyright (C) 2006-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
namespace com
{
// BSTR representing a null wide char with 32 bits of length prefix (0);
// this will work on Windows as well as other platforms where BSTR does
// not use length prefixes
/* static */
{
/*
* Initialie m_bstr first in case of throws further down in the code, then
* check for empty input (m_bstr == NULL means empty, there are no NULL
* strings).
*/
return;
/*
* Calculate the length and allocate a BSTR string buffer of the right
* size, i.e. optimize heap usage.
*/
if (RT_FAILURE(vrc))
{
/* ASSUME: input is valid Utf-8. Fake out of memory error. */
}
if (RT_UNLIKELY(!m_bstr))
if (RT_FAILURE(vrc))
{
/* This should not happen! */
cleanup();
}
}
/* static */
#if defined(VBOX_WITH_XPCOM)
{
if (RT_UNLIKELY(!*pstr))
}
{
if (RT_UNLIKELY(!*pstr))
return E_OUTOFMEMORY;
return S_OK;
}
#endif
{
if (length())
{
jolt();
}
return *this;
}
{
if (length())
{
jolt();
}
return *this;
}
{
if (length())
{
if (pszName)
{
jolt();
}
else
cleanup();
}
return *this;
}
{
if (length())
{
jolt();
}
return *this;
}
/**
* Internal function used in Utf8Str copy constructors and assignment when
* copying from a UTF-16 string.
*
* As with the RTCString::copyFrom() variants, this unconditionally sets the
* members to a copy of the given other strings and makes no assumptions about
* previous contents. This can therefore be used both in copy constructors,
* when member variables have no defined value, and in assignments after having
* called cleanup().
*
* This variant converts from a UTF-16 string, most probably from
* a Bstr assignment.
*
* @param a_pbstr The source string. The caller guarantees that this
* is valid UTF-16.
*
* @sa RTCString::copyFromN
*/
{
{
RTSTR_MAX, // size_t cwcString: translate entire string
&m_psz, // char **ppsz: output buffer
0, // size_t cch: if 0, func allocates buffer in *ppsz
&m_cch); // size_t *pcch: receives the size of the output string, excluding the terminator.
if (RT_SUCCESS(vrc))
else
{
if ( vrc != VERR_NO_STR_MEMORY
&& vrc != VERR_NO_MEMORY)
{
/* ASSUME: input is valid Utf-16. Fake out of memory error. */
}
m_cch = 0;
m_cbAllocated = 0;
}
}
else
{
m_cch = 0;
m_cbAllocated = 0;
}
}
/**
* A variant of Utf8Str::copyFrom that does not throw any exceptions but returns
* E_OUTOFMEMORY instead.
*
* @param a_pbstr The source string.
* @returns S_OK or E_OUTOFMEMORY.
*/
{
{
RTSTR_MAX, // size_t cwcString: translate entire string
&m_psz, // char **ppsz: output buffer
0, // size_t cch: if 0, func allocates buffer in *ppsz
&m_cch); // size_t *pcch: receives the size of the output string, excluding the terminator.
if (RT_SUCCESS(vrc))
else
{
if ( vrc != VERR_NO_STR_MEMORY
&& vrc != VERR_NO_MEMORY)
{
/* ASSUME: input is valid Utf-16. Fake out of memory error. */
}
m_cch = 0;
m_cbAllocated = 0;
return E_OUTOFMEMORY;
}
}
else
{
m_cch = 0;
m_cbAllocated = 0;
}
return S_OK;
}
/**
* A variant of Utf8Str::copyFromN that does not throw any exceptions but
* returns E_OUTOFMEMORY instead.
*
* @param a_pcszSrc The source string.
* @param a_cchSrc The source string.
* @returns S_OK or E_OUTOFMEMORY.
*
* @remarks This calls cleanup() first, so the caller doesn't have to. (Saves
* code space.)
*/
{
cleanup();
if (a_cchSrc)
{
{
}
else
{
m_cch = 0;
m_cbAllocated = 0;
return E_OUTOFMEMORY;
}
}
else
{
m_cch = 0;
m_cbAllocated = 0;
}
return S_OK;
}
} /* namespace com */