1 | /* $Id: string.cpp 36530 2011-04-04 13:56:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MS COM / XPCOM Abstraction Layer - UTF-8 and UTF-16 string classes.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2011 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "VBox/com/string.h"
|
---|
19 |
|
---|
20 | #include <iprt/err.h>
|
---|
21 | #include <iprt/path.h>
|
---|
22 | #include <iprt/log.h>
|
---|
23 |
|
---|
24 | namespace com
|
---|
25 | {
|
---|
26 |
|
---|
27 | // BSTR representing a null wide char with 32 bits of length prefix (0);
|
---|
28 | // this will work on Windows as well as other platforms where BSTR does
|
---|
29 | // not use length prefixes
|
---|
30 | const OLECHAR g_achEmptyBstr[3] = { 0, 0, 0 };
|
---|
31 | const BSTR g_bstrEmpty = (BSTR)&g_achEmptyBstr[2];
|
---|
32 |
|
---|
33 | /* static */
|
---|
34 | const Bstr Bstr::Empty; /* default ctor is OK */
|
---|
35 |
|
---|
36 | void Bstr::copyFromN(const char *a_pszSrc, size_t a_cchMax)
|
---|
37 | {
|
---|
38 | /*
|
---|
39 | * Initialie m_bstr first in case of throws further down in the code, then
|
---|
40 | * check for empty input (m_bstr == NULL means empty, there are no NULL
|
---|
41 | * strings).
|
---|
42 | */
|
---|
43 | m_bstr = NULL;
|
---|
44 | if (!a_cchMax || !a_pszSrc || !*a_pszSrc)
|
---|
45 | return;
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * Calculate the length and allocate a BSTR string buffer of the right
|
---|
49 | * size, i.e. optimize heap usage.
|
---|
50 | */
|
---|
51 | size_t cwc;
|
---|
52 | int vrc = ::RTStrCalcUtf16LenEx(a_pszSrc, a_cchMax, &cwc);
|
---|
53 | if (RT_FAILURE(vrc))
|
---|
54 | {
|
---|
55 | /* ASSUME: input is valid Utf-8. Fake out of memory error. */
|
---|
56 | AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTStrNLen(a_pszSrc, a_cchMax), a_pszSrc));
|
---|
57 | throw std::bad_alloc();
|
---|
58 | }
|
---|
59 |
|
---|
60 | m_bstr = ::SysAllocStringByteLen(NULL, cwc * sizeof(OLECHAR));
|
---|
61 | if (RT_UNLIKELY(!m_bstr))
|
---|
62 | throw std::bad_alloc();
|
---|
63 |
|
---|
64 | PRTUTF16 pwsz = (PRTUTF16)m_bstr;
|
---|
65 | vrc = ::RTStrToUtf16Ex(a_pszSrc, a_cchMax, &pwsz, cwc + 1, NULL);
|
---|
66 | if (RT_FAILURE(vrc))
|
---|
67 | {
|
---|
68 | /* This should not happen! */
|
---|
69 | AssertRC(vrc);
|
---|
70 | cleanup();
|
---|
71 | throw std::bad_alloc();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /* static */
|
---|
77 | const Utf8Str Utf8Str::Empty; /* default ctor is OK */
|
---|
78 |
|
---|
79 | #if defined(VBOX_WITH_XPCOM)
|
---|
80 | void Utf8Str::cloneTo(char **pstr) const
|
---|
81 | {
|
---|
82 | size_t cb = length() + 1;
|
---|
83 | *pstr = (char*)nsMemory::Alloc(cb);
|
---|
84 | if (RT_UNLIKELY(!*pstr))
|
---|
85 | throw std::bad_alloc();
|
---|
86 | memcpy(*pstr, c_str(), cb);
|
---|
87 | }
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | Utf8Str& Utf8Str::stripTrailingSlash()
|
---|
91 | {
|
---|
92 | if (length())
|
---|
93 | {
|
---|
94 | ::RTPathStripTrailingSlash(m_psz);
|
---|
95 | jolt();
|
---|
96 | }
|
---|
97 | return *this;
|
---|
98 | }
|
---|
99 |
|
---|
100 | Utf8Str& Utf8Str::stripFilename()
|
---|
101 | {
|
---|
102 | if (length())
|
---|
103 | {
|
---|
104 | RTPathStripFilename(m_psz);
|
---|
105 | jolt();
|
---|
106 | }
|
---|
107 | return *this;
|
---|
108 | }
|
---|
109 |
|
---|
110 | Utf8Str& Utf8Str::stripPath()
|
---|
111 | {
|
---|
112 | if (length())
|
---|
113 | {
|
---|
114 | char *pszName = ::RTPathFilename(m_psz);
|
---|
115 | if (pszName)
|
---|
116 | {
|
---|
117 | size_t cchName = length() - (pszName - m_psz);
|
---|
118 | memmove(m_psz, pszName, cchName + 1);
|
---|
119 | jolt();
|
---|
120 | }
|
---|
121 | else
|
---|
122 | cleanup();
|
---|
123 | }
|
---|
124 | return *this;
|
---|
125 | }
|
---|
126 |
|
---|
127 | Utf8Str& Utf8Str::stripExt()
|
---|
128 | {
|
---|
129 | if (length())
|
---|
130 | {
|
---|
131 | RTPathStripExt(m_psz);
|
---|
132 | jolt();
|
---|
133 | }
|
---|
134 | return *this;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Internal function used in Utf8Str copy constructors and assignment when
|
---|
139 | * copying from a UTF-16 string.
|
---|
140 | *
|
---|
141 | * As with the RTCString::copyFrom() variants, this unconditionally sets the
|
---|
142 | * members to a copy of the given other strings and makes no assumptions about
|
---|
143 | * previous contents. This can therefore be used both in copy constructors,
|
---|
144 | * when member variables have no defined value, and in assignments after having
|
---|
145 | * called cleanup().
|
---|
146 | *
|
---|
147 | * This variant converts from a UTF-16 string, most probably from
|
---|
148 | * a Bstr assignment.
|
---|
149 | *
|
---|
150 | * @param a_pbstr The source string. The caller guarantees that this
|
---|
151 | * is valid UTF-16.
|
---|
152 | *
|
---|
153 | * @sa RTCString::copyFromN
|
---|
154 | */
|
---|
155 | void Utf8Str::copyFrom(CBSTR a_pbstr)
|
---|
156 | {
|
---|
157 | if (a_pbstr && *a_pbstr)
|
---|
158 | {
|
---|
159 | int vrc = RTUtf16ToUtf8Ex((PCRTUTF16)a_pbstr,
|
---|
160 | RTSTR_MAX, // size_t cwcString: translate entire string
|
---|
161 | &m_psz, // char **ppsz: output buffer
|
---|
162 | 0, // size_t cch: if 0, func allocates buffer in *ppsz
|
---|
163 | &m_cch); // size_t *pcch: receives the size of the output string, excluding the terminator.
|
---|
164 | if (RT_SUCCESS(vrc))
|
---|
165 | m_cbAllocated = m_cch + 1;
|
---|
166 | else
|
---|
167 | {
|
---|
168 | if ( vrc != VERR_NO_STR_MEMORY
|
---|
169 | && vrc != VERR_NO_MEMORY)
|
---|
170 | {
|
---|
171 | /* ASSUME: input is valid Utf-16. Fake out of memory error. */
|
---|
172 | AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTUtf16Len(a_pbstr) * sizeof(RTUTF16), a_pbstr));
|
---|
173 | }
|
---|
174 |
|
---|
175 | m_cch = 0;
|
---|
176 | m_cbAllocated = 0;
|
---|
177 | m_psz = NULL;
|
---|
178 |
|
---|
179 | throw std::bad_alloc();
|
---|
180 | }
|
---|
181 | }
|
---|
182 | else
|
---|
183 | {
|
---|
184 | m_cch = 0;
|
---|
185 | m_cbAllocated = 0;
|
---|
186 | m_psz = NULL;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | } /* namespace com */
|
---|