VirtualBox

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

Last change on this file since 95353 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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

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