VirtualBox

source: vbox/trunk/src/VBox/Main/glue/string.cpp@ 95969

最後變更 在這個檔案從95969是 95956,由 vboxsync 提交於 3 年 前

Main/glue/string.cpp: Made the code compile w/o exceptions. It is used by VBoxCredPros in the GAs, which in no-CRT mode have exceptions disabled. bugref:10261

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 26.6 KB
 
1/* $Id: string.cpp 95956 2022-07-29 20:54:43Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer - UTF-8 and UTF-16 string classes.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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/log.h>
22#include <iprt/path.h>
23#include <iprt/string.h>
24#include <iprt/uni.h>
25
26namespace com
27{
28
29// BSTR representing a null wide char with 32 bits of length prefix (0);
30// this will work on Windows as well as other platforms where BSTR does
31// not use length prefixes
32const OLECHAR g_achEmptyBstr[3] = { 0, 0, 0 };
33const BSTR g_bstrEmpty = (BSTR)&g_achEmptyBstr[2];
34
35/* static */
36const Bstr Bstr::Empty; /* default ctor is OK */
37
38
39Bstr &Bstr::printf(const char *pszFormat, ...)
40{
41 va_list va;
42 va_start(va, pszFormat);
43 HRESULT hrc = printfVNoThrow(pszFormat, va);
44 va_end(va);
45#ifdef RT_EXCEPTIONS_ENABLED
46 if (hrc == S_OK)
47 { /* likely */ }
48 else
49 throw std::bad_alloc();
50#else
51 Assert(hrc == S_OK); RT_NOREF(hrc);
52#endif
53 return *this;
54}
55
56HRESULT Bstr::printfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT
57{
58 va_list va;
59 va_start(va, pszFormat);
60 HRESULT hrc = printfVNoThrow(pszFormat, va);
61 va_end(va);
62 return hrc;
63}
64
65
66Bstr &Bstr::printfV(const char *pszFormat, va_list va)
67{
68 HRESULT hrc = printfVNoThrow(pszFormat, va);
69#ifdef RT_EXCEPTIONS_ENABLED
70 if (hrc == S_OK)
71 { /* likely */ }
72 else
73 throw std::bad_alloc();
74#else
75 Assert(hrc == S_OK); RT_NOREF(hrc);
76#endif
77 return *this;
78}
79
80struct BSTRNOTHROW
81{
82 Bstr *pThis;
83 size_t cwcAlloc;
84 size_t offDst;
85 HRESULT hrc;
86};
87
88/**
89 * Callback used with RTStrFormatV by Bstr::printfVNoThrow.
90 *
91 * @returns The number of bytes added (not used).
92 *
93 * @param pvArg Pointer to a BSTRNOTHROW structure.
94 * @param pachChars The characters to append.
95 * @param cbChars The number of characters. 0 on the final callback.
96 */
97/*static*/ DECLCALLBACK(size_t)
98Bstr::printfOutputCallbackNoThrow(void *pvArg, const char *pachChars, size_t cbChars) RT_NOEXCEPT
99{
100 BSTRNOTHROW *pArgs = (BSTRNOTHROW *)pvArg;
101 if (cbChars)
102 {
103 size_t cwcAppend;
104 int rc = ::RTStrCalcUtf16LenEx(pachChars, cbChars, &cwcAppend);
105 AssertRCReturnStmt(rc, pArgs->hrc = E_UNEXPECTED, 0);
106
107 /*
108 * Ensure we've got sufficient memory.
109 */
110 Bstr *pThis = pArgs->pThis;
111 size_t const cwcBoth = pArgs->offDst + cwcAppend;
112 if (cwcBoth >= pArgs->cwcAlloc)
113 {
114 if (pArgs->hrc == S_OK)
115 {
116 /* Double the buffer size, if it's less that _1M. Align sizes like
117 for append. */
118 size_t cwcAlloc = RT_ALIGN_Z(pArgs->cwcAlloc, 128);
119 cwcAlloc += RT_MIN(cwcAlloc, _1M);
120 if (cwcAlloc <= cwcBoth)
121 cwcAlloc = RT_ALIGN_Z(cwcBoth + 1, 512);
122 pArgs->hrc = pThis->reserveNoThrow(cwcAlloc, true /*fForce*/);
123 AssertMsgReturn(pArgs->hrc == S_OK, ("cwcAlloc=%#zx\n", cwcAlloc), 0);
124 pArgs->cwcAlloc = cwcAlloc;
125 }
126 else
127 return 0;
128 }
129
130 /*
131 * Do the conversion.
132 */
133 PRTUTF16 pwszDst = pThis->m_bstr + pArgs->offDst;
134 Assert(pArgs->cwcAlloc > pArgs->offDst);
135 rc = ::RTStrToUtf16Ex(pachChars, cbChars, &pwszDst, pArgs->cwcAlloc - pArgs->offDst, &cwcAppend);
136 AssertRCReturnStmt(rc, pArgs->hrc = E_UNEXPECTED, 0);
137 pArgs->offDst += cwcAppend;
138 }
139 return cbChars;
140}
141
142HRESULT Bstr::printfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT
143{
144 cleanup();
145
146 BSTRNOTHROW Args = { this, 0, 0, S_OK };
147 RTStrFormatV(printfOutputCallbackNoThrow, &Args, NULL, NULL, pszFormat, va);
148 if (Args.hrc == S_OK)
149 {
150 Args.hrc = joltNoThrow(Args.offDst);
151 if (Args.hrc == S_OK)
152 return S_OK;
153 }
154
155 cleanup();
156 return Args.hrc;
157}
158
159void Bstr::copyFromN(const char *a_pszSrc, size_t a_cchMax)
160{
161 /*
162 * Initialize m_bstr first in case of throws further down in the code, then
163 * check for empty input (m_bstr == NULL means empty, there are no NULL
164 * strings).
165 */
166 m_bstr = NULL;
167 if (!a_cchMax || !a_pszSrc || !*a_pszSrc)
168 return;
169
170 /*
171 * Calculate the length and allocate a BSTR string buffer of the right
172 * size, i.e. optimize heap usage.
173 */
174 size_t cwc;
175 int vrc = ::RTStrCalcUtf16LenEx(a_pszSrc, a_cchMax, &cwc);
176 if (RT_SUCCESS(vrc))
177 {
178 m_bstr = ::SysAllocStringByteLen(NULL, (unsigned)(cwc * sizeof(OLECHAR)));
179 if (RT_LIKELY(m_bstr))
180 {
181 PRTUTF16 pwsz = (PRTUTF16)m_bstr;
182 vrc = ::RTStrToUtf16Ex(a_pszSrc, a_cchMax, &pwsz, cwc + 1, NULL);
183 if (RT_SUCCESS(vrc))
184 return;
185
186 /* This should not happen! */
187 AssertRC(vrc);
188 cleanup();
189 }
190 }
191 else /* ASSUME: input is valid Utf-8. Fake out of memory error. */
192 AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTStrNLen(a_pszSrc, a_cchMax), a_pszSrc));
193#ifdef RT_EXCEPTIONS_ENABLED
194 throw std::bad_alloc();
195#endif
196}
197
198HRESULT Bstr::cleanupAndCopyFromNoThrow(const char *a_pszSrc, size_t a_cchMax) RT_NOEXCEPT
199{
200 /*
201 * Check for empty input (m_bstr == NULL means empty, there are no NULL strings).
202 */
203 cleanup();
204 if (!a_cchMax || !a_pszSrc || !*a_pszSrc)
205 return S_OK;
206
207 /*
208 * Calculate the length and allocate a BSTR string buffer of the right
209 * size, i.e. optimize heap usage.
210 */
211 HRESULT hrc;
212 size_t cwc;
213 int vrc = ::RTStrCalcUtf16LenEx(a_pszSrc, a_cchMax, &cwc);
214 if (RT_SUCCESS(vrc))
215 {
216 m_bstr = ::SysAllocStringByteLen(NULL, (unsigned)(cwc * sizeof(OLECHAR)));
217 if (RT_LIKELY(m_bstr))
218 {
219 PRTUTF16 pwsz = (PRTUTF16)m_bstr;
220 vrc = ::RTStrToUtf16Ex(a_pszSrc, a_cchMax, &pwsz, cwc + 1, NULL);
221 if (RT_SUCCESS(vrc))
222 return S_OK;
223
224 /* This should not happen! */
225 AssertRC(vrc);
226 cleanup();
227 hrc = E_UNEXPECTED;
228 }
229 else
230 hrc = E_OUTOFMEMORY;
231 }
232 else
233 {
234 /* Unexpected: Invalid UTF-8 input. */
235 AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTStrNLen(a_pszSrc, a_cchMax), a_pszSrc));
236 hrc = E_UNEXPECTED;
237 }
238 return hrc;
239}
240
241
242int Bstr::compareUtf8(const char *a_pszRight, CaseSensitivity a_enmCase /*= CaseSensitive*/) const
243{
244 PCRTUTF16 pwszLeft = m_bstr;
245
246 /*
247 * Special case for null/empty strings. Unlike RTUtf16Cmp we
248 * treat null and empty equally.
249 */
250 if (!pwszLeft)
251 return !a_pszRight || *a_pszRight == '\0' ? 0 : -1;
252 if (!a_pszRight)
253 return *pwszLeft == '\0' ? 0 : 1;
254
255 /*
256 * Compare with a UTF-8 string by enumerating them char by char.
257 */
258 for (;;)
259 {
260 RTUNICP ucLeft;
261 int rc = RTUtf16GetCpEx(&pwszLeft, &ucLeft);
262 AssertRCReturn(rc, 1);
263
264 RTUNICP ucRight;
265 rc = RTStrGetCpEx(&a_pszRight, &ucRight);
266 AssertRCReturn(rc, -1);
267 if (ucLeft == ucRight)
268 {
269 if (ucLeft)
270 continue;
271 return 0;
272 }
273
274 if (a_enmCase == CaseInsensitive)
275 {
276 if (RTUniCpToUpper(ucLeft) == RTUniCpToUpper(ucRight))
277 continue;
278 if (RTUniCpToLower(ucLeft) == RTUniCpToLower(ucRight))
279 continue;
280 }
281
282 return ucLeft < ucRight ? -1 : 1;
283 }
284}
285
286
287bool Bstr::startsWith(Bstr const &a_rStart) const
288{
289 return RTUtf16NCmp(m_bstr, a_rStart.m_bstr, a_rStart.length()) == 0;
290}
291
292
293bool Bstr::startsWith(RTCString const &a_rStart) const
294{
295 return RTUtf16NCmpUtf8(m_bstr, a_rStart.c_str(), RTSTR_MAX, a_rStart.length()) == 0;
296}
297
298
299bool Bstr::startsWith(const char *a_pszStart) const
300{
301 return RTUtf16NCmpUtf8(m_bstr, a_pszStart, RTSTR_MAX, strlen(a_pszStart)) == 0;
302}
303
304
305#ifndef VBOX_WITH_XPCOM
306
307HRESULT Bstr::joltNoThrow(ssize_t cwcNew /* = -1*/) RT_NOEXCEPT
308{
309 if (m_bstr)
310 {
311 size_t const cwcAlloc = ::SysStringLen(m_bstr);
312 size_t const cwcActual = cwcNew < 0 ? ::RTUtf16Len(m_bstr) : (size_t)cwcNew;
313 Assert(cwcNew < 0 || cwcActual == ::RTUtf16Len(m_bstr));
314 if (cwcActual != cwcAlloc)
315 {
316 Assert(cwcActual <= cwcAlloc);
317 Assert((unsigned int)cwcActual == cwcActual);
318
319 /* Official way: Reallocate the string. We could of course just update the size-prefix if we dared... */
320 if (!::SysReAllocStringLen(&m_bstr, NULL, (unsigned int)cwcActual))
321 {
322 AssertFailed();
323 return E_OUTOFMEMORY;
324 }
325 }
326 }
327 else
328 Assert(cwcNew <= 0);
329 return S_OK;
330}
331
332
333void Bstr::jolt(ssize_t cwcNew /* = -1*/)
334{
335 HRESULT hrc = joltNoThrow(cwcNew);
336# ifdef RT_EXCEPTIONS_ENABLED
337 if (hrc != S_OK)
338 throw std::bad_alloc();
339# else
340 Assert(hrc == S_OK); RT_NOREF(hrc);
341# endif
342}
343
344#endif /* !VBOX_WITH_XPCOM */
345
346
347HRESULT Bstr::reserveNoThrow(size_t cwcMin, bool fForce /*= false*/) RT_NOEXCEPT
348{
349 /* If not forcing the string to the cwcMin length, check cwcMin against the
350 current string length: */
351 if (!fForce)
352 {
353 size_t cwcCur = m_bstr ? ::SysStringLen(m_bstr) : 0;
354 if (cwcCur >= cwcMin)
355 return S_OK;
356 }
357
358 /* The documentation for SysReAllocStringLen hints about it being allergic
359 to NULL in some way or another, so we call SysAllocStringLen directly
360 when appropriate: */
361 if (m_bstr)
362 AssertReturn(::SysReAllocStringLen(&m_bstr, NULL, (unsigned int)cwcMin) != FALSE, E_OUTOFMEMORY);
363 else if (cwcMin > 0)
364 {
365 m_bstr = ::SysAllocStringLen(NULL, (unsigned int)cwcMin);
366 AssertReturn(m_bstr, E_OUTOFMEMORY);
367 }
368
369 return S_OK;
370}
371
372
373void Bstr::reserve(size_t cwcMin, bool fForce /*= false*/)
374{
375 HRESULT hrc = reserveNoThrow(cwcMin, fForce);
376#ifdef RT_EXCEPTIONS_ENABLED
377 if (hrc != S_OK)
378 throw std::bad_alloc();
379#else
380 Assert(hrc == S_OK); RT_NOREF(hrc);
381#endif
382}
383
384
385Bstr &Bstr::append(const Bstr &rThat)
386{
387 if (rThat.isNotEmpty())
388 return appendWorkerUtf16(rThat.m_bstr, rThat.length());
389 return *this;
390}
391
392
393HRESULT Bstr::appendNoThrow(const Bstr &rThat) RT_NOEXCEPT
394{
395 if (rThat.isNotEmpty())
396 return appendWorkerUtf16NoThrow(rThat.m_bstr, rThat.length());
397 return S_OK;
398}
399
400
401Bstr &Bstr::append(const RTCString &rThat)
402{
403 if (rThat.isNotEmpty())
404 return appendWorkerUtf8(rThat.c_str(), rThat.length());
405 return *this;
406}
407
408
409HRESULT Bstr::appendNoThrow(const RTCString &rThat) RT_NOEXCEPT
410{
411 if (rThat.isNotEmpty())
412 return appendWorkerUtf8NoThrow(rThat.c_str(), rThat.length());
413 return S_OK;
414}
415
416
417Bstr &Bstr::append(CBSTR pwszSrc)
418{
419 if (pwszSrc && *pwszSrc)
420 return appendWorkerUtf16(pwszSrc, RTUtf16Len(pwszSrc));
421 return *this;
422}
423
424
425HRESULT Bstr::appendNoThrow(CBSTR pwszSrc) RT_NOEXCEPT
426{
427 if (pwszSrc && *pwszSrc)
428 return appendWorkerUtf16NoThrow(pwszSrc, RTUtf16Len(pwszSrc));
429 return S_OK;
430}
431
432
433Bstr &Bstr::append(const char *pszSrc)
434{
435 if (pszSrc && *pszSrc)
436 return appendWorkerUtf8(pszSrc, strlen(pszSrc));
437 return *this;
438}
439
440
441HRESULT Bstr::appendNoThrow(const char *pszSrc) RT_NOEXCEPT
442{
443 if (pszSrc && *pszSrc)
444 return appendWorkerUtf8NoThrow(pszSrc, strlen(pszSrc));
445 return S_OK;
446}
447
448
449Bstr &Bstr::append(const Bstr &rThat, size_t offStart, size_t cwcMax /*= RTSTR_MAX*/)
450{
451 size_t cwcSrc = rThat.length();
452 if (offStart < cwcSrc)
453 return appendWorkerUtf16(rThat.raw() + offStart, RT_MIN(cwcSrc - offStart, cwcMax));
454 return *this;
455}
456
457
458HRESULT Bstr::appendNoThrow(const Bstr &rThat, size_t offStart, size_t cwcMax /*= RTSTR_MAX*/) RT_NOEXCEPT
459{
460 size_t cwcSrc = rThat.length();
461 if (offStart < cwcSrc)
462 return appendWorkerUtf16NoThrow(rThat.raw() + offStart, RT_MIN(cwcSrc - offStart, cwcMax));
463 return S_OK;
464}
465
466
467Bstr &Bstr::append(const RTCString &rThat, size_t offStart, size_t cchMax /*= RTSTR_MAX*/)
468{
469 if (offStart < rThat.length())
470 return appendWorkerUtf8(rThat.c_str() + offStart, RT_MIN(rThat.length() - offStart, cchMax));
471 return *this;
472}
473
474
475HRESULT Bstr::appendNoThrow(const RTCString &rThat, size_t offStart, size_t cchMax /*= RTSTR_MAX*/) RT_NOEXCEPT
476{
477 if (offStart < rThat.length())
478 return appendWorkerUtf8NoThrow(rThat.c_str() + offStart, RT_MIN(rThat.length() - offStart, cchMax));
479 return S_OK;
480}
481
482
483Bstr &Bstr::append(CBSTR pwszThat, size_t cchMax)
484{
485 return appendWorkerUtf16(pwszThat, RTUtf16NLen(pwszThat, cchMax));
486}
487
488
489HRESULT Bstr::appendNoThrow(CBSTR pwszThat, size_t cchMax) RT_NOEXCEPT
490{
491 return appendWorkerUtf16NoThrow(pwszThat, RTUtf16NLen(pwszThat, cchMax));
492}
493
494
495Bstr &Bstr::append(const char *pszThat, size_t cchMax)
496{
497 return appendWorkerUtf8(pszThat, RTStrNLen(pszThat, cchMax));
498}
499
500
501HRESULT Bstr::appendNoThrow(const char *pszThat, size_t cchMax) RT_NOEXCEPT
502{
503 return appendWorkerUtf8NoThrow(pszThat, RTStrNLen(pszThat, cchMax));
504}
505
506
507Bstr &Bstr::append(char ch)
508{
509 AssertMsg(ch > 0 && ch < 127, ("%#x\n", ch));
510 return appendWorkerUtf8(&ch, 1);
511}
512
513
514HRESULT Bstr::appendNoThrow(char ch) RT_NOEXCEPT
515{
516 AssertMsg(ch > 0 && ch < 127, ("%#x\n", ch));
517 return appendWorkerUtf8NoThrow(&ch, 1);
518}
519
520
521Bstr &Bstr::appendCodePoint(RTUNICP uc)
522{
523 RTUTF16 wszTmp[3];
524 PRTUTF16 pwszEnd = RTUtf16PutCp(wszTmp, uc);
525 *pwszEnd = '\0';
526 return appendWorkerUtf16(&wszTmp[0], pwszEnd - &wszTmp[0]);
527}
528
529
530HRESULT Bstr::appendCodePointNoThrow(RTUNICP uc) RT_NOEXCEPT
531{
532 RTUTF16 wszTmp[3];
533 PRTUTF16 pwszEnd = RTUtf16PutCp(wszTmp, uc);
534 *pwszEnd = '\0';
535 return appendWorkerUtf16NoThrow(&wszTmp[0], pwszEnd - &wszTmp[0]);
536}
537
538
539Bstr &Bstr::appendWorkerUtf16(PCRTUTF16 pwszSrc, size_t cwcSrc)
540{
541 size_t cwcOld = length();
542 size_t cwcTotal = cwcOld + cwcSrc;
543 reserve(cwcTotal, true /*fForce*/);
544 if (cwcSrc)
545 memcpy(&m_bstr[cwcOld], pwszSrc, cwcSrc * sizeof(RTUTF16));
546 m_bstr[cwcTotal] = '\0';
547 return *this;
548}
549
550
551HRESULT Bstr::appendWorkerUtf16NoThrow(PCRTUTF16 pwszSrc, size_t cwcSrc) RT_NOEXCEPT
552{
553 size_t cwcOld = length();
554 size_t cwcTotal = cwcOld + cwcSrc;
555 HRESULT hrc = reserveNoThrow(cwcTotal, true /*fForce*/);
556 if (hrc == S_OK)
557 {
558 if (cwcSrc)
559 memcpy(&m_bstr[cwcOld], pwszSrc, cwcSrc * sizeof(RTUTF16));
560 m_bstr[cwcTotal] = '\0';
561 }
562 return hrc;
563}
564
565
566Bstr &Bstr::appendWorkerUtf8(const char *pszSrc, size_t cchSrc)
567{
568 size_t cwcSrc;
569 int rc = RTStrCalcUtf16LenEx(pszSrc, cchSrc, &cwcSrc);
570#ifdef RT_EXCEPTIONS_ENABLED
571 AssertRCStmt(rc, throw std::bad_alloc());
572#else
573 AssertRCReturn(rc, *this);
574#endif
575
576 size_t cwcOld = length();
577 size_t cwcTotal = cwcOld + cwcSrc;
578 reserve(cwcTotal, true /*fForce*/);
579 if (cwcSrc)
580 {
581 PRTUTF16 pwszDst = &m_bstr[cwcOld];
582 rc = RTStrToUtf16Ex(pszSrc, cchSrc, &pwszDst, cwcSrc + 1, NULL);
583#ifdef RT_EXCEPTIONS_ENABLED
584 AssertRCStmt(rc, throw std::bad_alloc());
585#else
586 AssertRC(rc);
587#endif
588 }
589 m_bstr[cwcTotal] = '\0';
590 return *this;
591}
592
593
594HRESULT Bstr::appendWorkerUtf8NoThrow(const char *pszSrc, size_t cchSrc) RT_NOEXCEPT
595{
596 size_t cwcSrc;
597 int rc = RTStrCalcUtf16LenEx(pszSrc, cchSrc, &cwcSrc);
598 AssertRCStmt(rc, E_INVALIDARG);
599
600 size_t cwcOld = length();
601 size_t cwcTotal = cwcOld + cwcSrc;
602 HRESULT hrc = reserveNoThrow(cwcTotal, true /*fForce*/);
603 AssertReturn(hrc == S_OK, hrc);
604 if (cwcSrc)
605 {
606 PRTUTF16 pwszDst = &m_bstr[cwcOld];
607 rc = RTStrToUtf16Ex(pszSrc, cchSrc, &pwszDst, cwcSrc + 1, NULL);
608 AssertRCStmt(rc, E_INVALIDARG);
609 }
610 m_bstr[cwcTotal] = '\0';
611 return S_OK;
612}
613
614
615Bstr &Bstr::appendPrintf(const char *pszFormat, ...)
616{
617 va_list va;
618 va_start(va, pszFormat);
619 HRESULT hrc = appendPrintfVNoThrow(pszFormat, va);
620 va_end(va);
621#ifdef RT_EXCEPTIONS_ENABLED
622 if (hrc != S_OK)
623 throw std::bad_alloc();
624#else
625 Assert(hrc == S_OK); RT_NOREF(hrc);
626#endif
627 return *this;
628}
629
630
631HRESULT Bstr::appendPrintfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT
632{
633 va_list va;
634 va_start(va, pszFormat);
635 HRESULT hrc = appendPrintfVNoThrow(pszFormat, va);
636 va_end(va);
637 return hrc;
638}
639
640
641Bstr &Bstr::appendPrintfV(const char *pszFormat, va_list va)
642{
643 HRESULT hrc = appendPrintfVNoThrow(pszFormat, va);
644#ifdef RT_EXCEPTIONS_ENABLED
645 if (hrc != S_OK)
646 throw std::bad_alloc();
647#else
648 Assert(hrc == S_OK); RT_NOREF(hrc);
649#endif
650 return *this;
651}
652
653
654HRESULT Bstr::appendPrintfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT
655{
656 size_t const cwcOld = length();
657 BSTRNOTHROW Args = { this, cwcOld, cwcOld, S_OK };
658
659 RTStrFormatV(printfOutputCallbackNoThrow, &Args, NULL, NULL, pszFormat, va);
660 if (Args.hrc == S_OK)
661 {
662 Args.hrc = joltNoThrow(Args.offDst);
663 if (Args.hrc == S_OK)
664 return S_OK;
665 }
666
667 if (m_bstr)
668 m_bstr[cwcOld] = '\0';
669 return Args.hrc;
670}
671
672
673Bstr &Bstr::erase(size_t offStart /*= 0*/, size_t cwcLength /*= RTSTR_MAX*/) RT_NOEXCEPT
674{
675 size_t cwc = length();
676 if (offStart < cwc)
677 {
678 if (cwcLength >= cwc - offStart)
679 {
680 if (!offStart)
681 cleanup();
682 else
683 {
684 /* Trail removal, nothing to move. */
685 m_bstr[offStart] = '\0';
686 joltNoThrow(offStart); /* not entirely optimal... */
687 }
688 }
689 else if (cwcLength > 0)
690 {
691 /* Pull up the tail to offStart. */
692 size_t cwcAfter = cwc - offStart - cwcLength;
693 memmove(&m_bstr[offStart], &m_bstr[offStart + cwcLength], cwcAfter * sizeof(*m_bstr));
694 cwc -= cwcLength;
695 m_bstr[cwc] = '\0';
696 joltNoThrow(cwc); /* not entirely optimal... */
697 }
698 }
699 return *this;
700}
701
702
703void Bstr::cleanup()
704{
705 if (m_bstr)
706 {
707 ::SysFreeString(m_bstr);
708 m_bstr = NULL;
709 }
710}
711
712
713void Bstr::copyFrom(const OLECHAR *a_bstrSrc)
714{
715 if (a_bstrSrc && *a_bstrSrc)
716 {
717 m_bstr = ::SysAllocString(a_bstrSrc);
718#ifdef RT_EXCEPTIONS_ENABLED
719 if (RT_LIKELY(m_bstr))
720 { /* likely */ }
721 else
722 throw std::bad_alloc();
723#else
724 Assert(m_bstr);
725#endif
726 }
727 else
728 m_bstr = NULL;
729}
730
731
732void Bstr::cleanupAndCopyFrom(const OLECHAR *a_bstrSrc)
733{
734 cleanup();
735 copyFrom(a_bstrSrc);
736}
737
738
739HRESULT Bstr::cleanupAndCopyFromEx(const OLECHAR *a_bstrSrc) RT_NOEXCEPT
740{
741 cleanup();
742
743 if (a_bstrSrc && *a_bstrSrc)
744 {
745 m_bstr = ::SysAllocString(a_bstrSrc);
746 if (RT_LIKELY(m_bstr))
747 { /* likely */ }
748 else
749 return E_OUTOFMEMORY;
750 }
751 else
752 m_bstr = NULL;
753 return S_OK;
754}
755
756
757
758/*********************************************************************************************************************************
759* Utf8Str Implementation *
760*********************************************************************************************************************************/
761
762/* static */
763const Utf8Str Utf8Str::Empty; /* default ctor is OK */
764
765#if defined(VBOX_WITH_XPCOM)
766void Utf8Str::cloneTo(char **pstr) const
767{
768 size_t cb = length() + 1;
769 *pstr = (char *)nsMemory::Alloc(cb);
770 if (RT_LIKELY(*pstr))
771 memcpy(*pstr, c_str(), cb);
772 else
773#ifdef RT_EXCEPTIONS_ENABLED
774 throw std::bad_alloc();
775#else
776 AssertFailed();
777#endif
778}
779
780HRESULT Utf8Str::cloneToEx(char **pstr) const
781{
782 size_t cb = length() + 1;
783 *pstr = (char *)nsMemory::Alloc(cb);
784 if (RT_LIKELY(*pstr))
785 {
786 memcpy(*pstr, c_str(), cb);
787 return S_OK;
788 }
789 return E_OUTOFMEMORY;
790}
791#endif
792
793HRESULT Utf8Str::cloneToEx(BSTR *pbstr) const RT_NOEXCEPT
794{
795 if (!pbstr)
796 return S_OK;
797 Bstr bstr;
798 HRESULT hrc = bstr.assignEx(*this);
799 if (SUCCEEDED(hrc))
800 hrc = bstr.detachToEx(pbstr);
801 return hrc;
802}
803
804Utf8Str& Utf8Str::stripTrailingSlash()
805{
806 if (length())
807 {
808 ::RTPathStripTrailingSlash(m_psz);
809 jolt();
810 }
811 return *this;
812}
813
814Utf8Str& Utf8Str::stripFilename()
815{
816 if (length())
817 {
818 RTPathStripFilename(m_psz);
819 jolt();
820 }
821 return *this;
822}
823
824Utf8Str& Utf8Str::stripPath()
825{
826 if (length())
827 {
828 char *pszName = ::RTPathFilename(m_psz);
829 if (pszName)
830 {
831 size_t cchName = length() - (pszName - m_psz);
832 memmove(m_psz, pszName, cchName + 1);
833 jolt();
834 }
835 else
836 cleanup();
837 }
838 return *this;
839}
840
841Utf8Str& Utf8Str::stripSuffix()
842{
843 if (length())
844 {
845 RTPathStripSuffix(m_psz);
846 jolt();
847 }
848 return *this;
849}
850
851size_t Utf8Str::parseKeyValue(Utf8Str &a_rKey, Utf8Str &a_rValue, size_t a_offStart /* = 0*/,
852 const Utf8Str &a_rPairSeparator /*= ","*/, const Utf8Str &a_rKeyValueSeparator /*= "="*/) const
853{
854 /* Find the end of the next pair, skipping empty pairs.
855 Note! The skipping allows us to pass the return value of a parseKeyValue()
856 call as offStart to the next call. */
857 size_t offEnd;
858 while ( a_offStart == (offEnd = find(&a_rPairSeparator, a_offStart))
859 && offEnd != npos)
860 a_offStart++;
861
862 /* Look for a key/value separator before the end of the pair.
863 ASSUMES npos value returned by find when the substring is not found is
864 really high. */
865 size_t offKeyValueSep = find(&a_rKeyValueSeparator, a_offStart);
866 if (offKeyValueSep < offEnd)
867 {
868 a_rKey = substr(a_offStart, offKeyValueSep - a_offStart);
869 if (offEnd == npos)
870 offEnd = m_cch; /* No confusing npos when returning strings. */
871 a_rValue = substr(offKeyValueSep + 1, offEnd - offKeyValueSep - 1);
872 }
873 else
874 {
875 a_rKey.setNull();
876 a_rValue.setNull();
877 }
878
879 return offEnd;
880}
881
882/**
883 * Internal function used in Utf8Str copy constructors and assignment when
884 * copying from a UTF-16 string.
885 *
886 * As with the RTCString::copyFrom() variants, this unconditionally sets the
887 * members to a copy of the given other strings and makes no assumptions about
888 * previous contents. This can therefore be used both in copy constructors,
889 * when member variables have no defined value, and in assignments after having
890 * called cleanup().
891 *
892 * This variant converts from a UTF-16 string, most probably from
893 * a Bstr assignment.
894 *
895 * @param a_pbstr The source string. The caller guarantees that this
896 * is valid UTF-16.
897 * @param a_cwcMax The number of characters to be copied. If set to RTSTR_MAX,
898 * the entire string will be copied.
899 *
900 * @sa RTCString::copyFromN
901 */
902void Utf8Str::copyFrom(CBSTR a_pbstr, size_t a_cwcMax)
903{
904 if (a_pbstr && *a_pbstr)
905 {
906 int vrc = RTUtf16ToUtf8Ex((PCRTUTF16)a_pbstr,
907 a_cwcMax, // size_t cwcString: translate entire string
908 &m_psz, // char **ppsz: output buffer
909 0, // size_t cch: if 0, func allocates buffer in *ppsz
910 &m_cch); // size_t *pcch: receives the size of the output string, excluding the terminator.
911 if (RT_SUCCESS(vrc))
912 m_cbAllocated = m_cch + 1;
913 else
914 {
915 if ( vrc != VERR_NO_STR_MEMORY
916 && vrc != VERR_NO_MEMORY)
917 {
918 /* ASSUME: input is valid Utf-16. Fake out of memory error. */
919 AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTUtf16Len(a_pbstr) * sizeof(RTUTF16), a_pbstr));
920 }
921
922 m_cch = 0;
923 m_cbAllocated = 0;
924 m_psz = NULL;
925
926#ifdef RT_EXCEPTIONS_ENABLED
927 throw std::bad_alloc();
928#else
929 AssertFailed();
930#endif
931 }
932 }
933 else
934 {
935 m_cch = 0;
936 m_cbAllocated = 0;
937 m_psz = NULL;
938 }
939}
940
941/**
942 * A variant of Utf8Str::copyFrom that does not throw any exceptions but returns
943 * E_OUTOFMEMORY instead.
944 *
945 * @param a_pbstr The source string.
946 * @returns S_OK or E_OUTOFMEMORY.
947 */
948HRESULT Utf8Str::copyFromEx(CBSTR a_pbstr)
949{
950 if (a_pbstr && *a_pbstr)
951 {
952 int vrc = RTUtf16ToUtf8Ex((PCRTUTF16)a_pbstr,
953 RTSTR_MAX, // size_t cwcString: translate entire string
954 &m_psz, // char **ppsz: output buffer
955 0, // size_t cch: if 0, func allocates buffer in *ppsz
956 &m_cch); // size_t *pcch: receives the size of the output string, excluding the terminator.
957 if (RT_SUCCESS(vrc))
958 m_cbAllocated = m_cch + 1;
959 else
960 {
961 if ( vrc != VERR_NO_STR_MEMORY
962 && vrc != VERR_NO_MEMORY)
963 {
964 /* ASSUME: input is valid Utf-16. Fake out of memory error. */
965 AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTUtf16Len(a_pbstr) * sizeof(RTUTF16), a_pbstr));
966 }
967
968 m_cch = 0;
969 m_cbAllocated = 0;
970 m_psz = NULL;
971
972 return E_OUTOFMEMORY;
973 }
974 }
975 else
976 {
977 m_cch = 0;
978 m_cbAllocated = 0;
979 m_psz = NULL;
980 }
981 return S_OK;
982}
983
984
985/**
986 * A variant of Utf8Str::copyFromN that does not throw any exceptions but
987 * returns E_OUTOFMEMORY instead.
988 *
989 * @param a_pcszSrc The source string.
990 * @param a_offSrc Start offset to copy from.
991 * @param a_cchSrc How much to copy
992 * @returns S_OK or E_OUTOFMEMORY.
993 *
994 * @remarks This calls cleanup() first, so the caller doesn't have to. (Saves
995 * code space.)
996 */
997HRESULT Utf8Str::copyFromExNComRC(const char *a_pcszSrc, size_t a_offSrc, size_t a_cchSrc)
998{
999 Assert(!a_cchSrc || !m_psz || (uintptr_t)&a_pcszSrc[a_offSrc] - (uintptr_t)m_psz >= (uintptr_t)m_cbAllocated);
1000 cleanup();
1001 if (a_cchSrc)
1002 {
1003 m_psz = RTStrAlloc(a_cchSrc + 1);
1004 if (RT_LIKELY(m_psz))
1005 {
1006 m_cch = a_cchSrc;
1007 m_cbAllocated = a_cchSrc + 1;
1008 memcpy(m_psz, a_pcszSrc + a_offSrc, a_cchSrc);
1009 m_psz[a_cchSrc] = '\0';
1010 }
1011 else
1012 {
1013 m_cch = 0;
1014 m_cbAllocated = 0;
1015 return E_OUTOFMEMORY;
1016 }
1017 }
1018 else
1019 {
1020 m_cch = 0;
1021 m_cbAllocated = 0;
1022 m_psz = NULL;
1023 }
1024 return S_OK;
1025}
1026
1027} /* namespace com */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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