VirtualBox

Changeset 34846 in vbox for trunk/src/VBox/Main/glue


Ignore:
Timestamp:
Dec 8, 2010 5:50:25 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
68659
Message:

COM glue: Added a substring constructor to the Bstr class (with optimized memory usage compared to the others).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/glue/string.cpp

    r34837 r34846  
    3636const Bstr Bstr::Empty; /* default ctor is OK */
    3737
     38void Bstr::copyFromN(const char *a_pszSrc, size_t a_cchMax)
     39{
     40    /*
     41     * Initialie m_bstr first in case of throws further down in the code, then
     42     * check for empty input (m_bstr == NULL means empty, there are no NULL
     43     * strings).
     44     */
     45    m_bstr = NULL;
     46    if (!a_cchMax || !a_pszSrc || !*a_pszSrc)
     47        return;
     48
     49    /*
     50     * Calculate the length and allocate a BSTR string buffer of the right
     51     * size, i.e. optimize heap usage.
     52     */
     53    size_t cwc;
     54    int vrc = ::RTStrCalcUtf16LenEx(a_pszSrc, a_cchMax, &cwc);
     55    AssertRCReturnVoid(vrc); /* throw instead? */
     56
     57    m_bstr = ::SysAllocStringByteLen(NULL, cwc * sizeof(OLECHAR));
     58    if (m_bstr)
     59    {
     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        }
     68    }
     69    else
     70        throw std::bad_alloc();
     71}
     72
     73
    3874/* static */
    3975const Utf8Str Utf8Str::Empty; /* default ctor is OK */
    4076
    41 #if defined (VBOX_WITH_XPCOM)
     77#if defined(VBOX_WITH_XPCOM)
    4278void Utf8Str::cloneTo(char **pstr) const
    4379{
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette