VirtualBox

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

Last change on this file since 80850 was 80850, checked in by vboxsync, 5 years ago

Main/glue: Moved the implementation of cleanup and copyFrom out of the header. Added a helper for combining cleanup and copyFrom that the assignment operators can use.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.5 KB
Line 
1/* $Id: string.cpp 80850 2019-09-17 10:14:53Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer - UTF-8 and UTF-16 string classes.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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#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
188int Bstr::compareUtf8(const char *a_pszRight, CaseSensitivity a_enmCase /*= CaseSensitive*/) const
189{
190 PCRTUTF16 pwszLeft = m_bstr;
191
192 /*
193 * Special case for null/empty strings. Unlike RTUtf16Cmp we
194 * treat null and empty equally.
195 */
196 if (!pwszLeft)
197 return !a_pszRight || *a_pszRight == '\0' ? 0 : -1;
198 if (!a_pszRight)
199 return *pwszLeft == '\0' ? 0 : 1;
200
201 /*
202 * Compare with a UTF-8 string by enumerating them char by char.
203 */
204 for (;;)
205 {
206 RTUNICP ucLeft;
207 int rc = RTUtf16GetCpEx(&pwszLeft, &ucLeft);
208 AssertRCReturn(rc, 1);
209
210 RTUNICP ucRight;
211 rc = RTStrGetCpEx(&a_pszRight, &ucRight);
212 AssertRCReturn(rc, -1);
213 if (ucLeft == ucRight)
214 {
215 if (ucLeft)
216 continue;
217 return 0;
218 }
219
220 if (a_enmCase == CaseInsensitive)
221 {
222 if (RTUniCpToUpper(ucLeft) == RTUniCpToUpper(ucRight))
223 continue;
224 if (RTUniCpToLower(ucLeft) == RTUniCpToLower(ucRight))
225 continue;
226 }
227
228 return ucLeft < ucRight ? -1 : 1;
229 }
230}
231
232
233#ifndef VBOX_WITH_XPCOM
234
235HRESULT Bstr::joltNoThrow(ssize_t cwcNew /* = -1*/)
236{
237 if (m_bstr)
238 {
239 size_t const cwcAlloc = ::SysStringLen(m_bstr);
240 size_t const cwcActual = cwcNew < 0 ? ::RTUtf16Len(m_bstr) : (size_t)cwcNew;
241 Assert(cwcNew < 0 || cwcActual == ::RTUtf16Len(m_bstr));
242 if (cwcActual != cwcAlloc)
243 {
244 Assert(cwcActual <= cwcAlloc);
245 Assert((unsigned int)cwcActual == cwcActual);
246
247 /* Official way: Reallocate the string. We could of course just update the size-prefix if we dared... */
248 if (!::SysReAllocStringLen(&m_bstr, NULL, (unsigned int)cwcActual))
249 {
250 AssertFailed();
251 return E_OUTOFMEMORY;
252 }
253 }
254 }
255 else
256 Assert(cwcNew <= 0);
257 return S_OK;
258}
259
260
261void Bstr::jolt(ssize_t cwcNew /* = -1*/)
262{
263 HRESULT hrc = joltNoThrow(cwcNew);
264 if (hrc != S_OK)
265 throw std::bad_alloc();
266}
267
268#endif /* !VBOX_WITH_XPCOM */
269
270
271HRESULT Bstr::reserveNoThrow(size_t cwcMin, bool fForce /*= false*/) RT_NOEXCEPT
272{
273 /* If not forcing the string to the cwcMin length, check cwcMin against the
274 current string length: */
275 if (!fForce)
276 {
277 size_t cwcCur = m_bstr ? ::SysStringLen(m_bstr) : 0;
278 if (cwcCur >= cwcMin)
279 return S_OK;
280 }
281
282 /* The documentation for SysReAllocStringLen hints about it being allergic
283 to NULL in some way or another, so we call SysAllocStringLen directly
284 when appropriate: */
285 if (m_bstr)
286 AssertReturn(::SysReAllocStringLen(&m_bstr, NULL, (unsigned int)cwcMin) != FALSE, E_OUTOFMEMORY);
287 else if (cwcMin > 0)
288 {
289 m_bstr = ::SysAllocStringLen(NULL, (unsigned int)cwcMin);
290 AssertReturn(m_bstr, E_OUTOFMEMORY);
291 }
292
293 return S_OK;
294}
295
296
297void Bstr::reserve(size_t cwcMin, bool fForce /*= false*/)
298{
299 HRESULT hrc = reserveNoThrow(cwcMin, fForce);
300 if (hrc != S_OK)
301 throw std::bad_alloc();
302}
303
304
305Bstr &Bstr::append(const Bstr &rThat)
306{
307 if (rThat.isNotEmpty())
308 return appendWorkerUtf16(rThat.m_bstr, rThat.length());
309 return *this;
310}
311
312
313HRESULT Bstr::appendNoThrow(const Bstr &rThat) RT_NOEXCEPT
314{
315 if (rThat.isNotEmpty())
316 return appendWorkerUtf16NoThrow(rThat.m_bstr, rThat.length());
317 return S_OK;
318}
319
320
321Bstr &Bstr::append(const RTCString &rThat)
322{
323 if (rThat.isNotEmpty())
324 return appendWorkerUtf8(rThat.c_str(), rThat.length());
325 return *this;
326}
327
328
329HRESULT Bstr::appendNoThrow(const RTCString &rThat) RT_NOEXCEPT
330{
331 if (rThat.isNotEmpty())
332 return appendWorkerUtf8NoThrow(rThat.c_str(), rThat.length());
333 return S_OK;
334}
335
336
337Bstr &Bstr::append(CBSTR pwszSrc)
338{
339 if (pwszSrc && *pwszSrc)
340 return appendWorkerUtf16(pwszSrc, RTUtf16Len(pwszSrc));
341 return *this;
342}
343
344
345HRESULT Bstr::appendNoThrow(CBSTR pwszSrc) RT_NOEXCEPT
346{
347 if (pwszSrc && *pwszSrc)
348 return appendWorkerUtf16NoThrow(pwszSrc, RTUtf16Len(pwszSrc));
349 return S_OK;
350}
351
352
353Bstr &Bstr::append(const char *pszSrc)
354{
355 if (pszSrc && *pszSrc)
356 return appendWorkerUtf8(pszSrc, strlen(pszSrc));
357 return *this;
358}
359
360
361HRESULT Bstr::appendNoThrow(const char *pszSrc) RT_NOEXCEPT
362{
363 if (pszSrc && *pszSrc)
364 return appendWorkerUtf8NoThrow(pszSrc, strlen(pszSrc));
365 return S_OK;
366}
367
368
369Bstr &Bstr::append(const Bstr &rThat, size_t offStart, size_t cwcMax /*= RTSTR_MAX*/)
370{
371 size_t cwcSrc = rThat.length();
372 if (offStart < cwcSrc)
373 return appendWorkerUtf16(rThat.raw() + offStart, RT_MIN(cwcSrc - offStart, cwcMax));
374 return *this;
375}
376
377
378HRESULT Bstr::appendNoThrow(const Bstr &rThat, size_t offStart, size_t cwcMax /*= RTSTR_MAX*/) RT_NOEXCEPT
379{
380 size_t cwcSrc = rThat.length();
381 if (offStart < cwcSrc)
382 return appendWorkerUtf16NoThrow(rThat.raw() + offStart, RT_MIN(cwcSrc - offStart, cwcMax));
383 return S_OK;
384}
385
386
387Bstr &Bstr::append(const RTCString &rThat, size_t offStart, size_t cchMax /*= RTSTR_MAX*/)
388{
389 if (offStart < rThat.length())
390 return appendWorkerUtf8(rThat.c_str() + offStart, RT_MIN(rThat.length() - offStart, cchMax));
391 return *this;
392}
393
394
395HRESULT Bstr::appendNoThrow(const RTCString &rThat, size_t offStart, size_t cchMax /*= RTSTR_MAX*/) RT_NOEXCEPT
396{
397 if (offStart < rThat.length())
398 return appendWorkerUtf8NoThrow(rThat.c_str() + offStart, RT_MIN(rThat.length() - offStart, cchMax));
399 return S_OK;
400}
401
402
403Bstr &Bstr::append(CBSTR pwszThat, size_t cchMax)
404{
405 return appendWorkerUtf16(pwszThat, RTUtf16NLen(pwszThat, cchMax));
406}
407
408
409HRESULT Bstr::appendNoThrow(CBSTR pwszThat, size_t cchMax) RT_NOEXCEPT
410{
411 return appendWorkerUtf16NoThrow(pwszThat, RTUtf16NLen(pwszThat, cchMax));
412}
413
414
415Bstr &Bstr::append(const char *pszThat, size_t cchMax)
416{
417 return appendWorkerUtf8(pszThat, RTStrNLen(pszThat, cchMax));
418}
419
420
421HRESULT Bstr::appendNoThrow(const char *pszThat, size_t cchMax) RT_NOEXCEPT
422{
423 return appendWorkerUtf8NoThrow(pszThat, RTStrNLen(pszThat, cchMax));
424}
425
426
427Bstr &Bstr::append(char ch)
428{
429 AssertMsg(ch > 0 && ch < 127, ("%#x\n", ch));
430 return appendWorkerUtf8(&ch, 1);
431}
432
433
434HRESULT Bstr::appendNoThrow(char ch) RT_NOEXCEPT
435{
436 AssertMsg(ch > 0 && ch < 127, ("%#x\n", ch));
437 return appendWorkerUtf8NoThrow(&ch, 1);
438}
439
440
441Bstr &Bstr::appendCodePoint(RTUNICP uc)
442{
443 RTUTF16 wszTmp[3];
444 PRTUTF16 pwszEnd = RTUtf16PutCp(wszTmp, uc);
445 *pwszEnd = '\0';
446 return appendWorkerUtf16(&wszTmp[0], pwszEnd - &wszTmp[0]);
447}
448
449
450HRESULT Bstr::appendCodePointNoThrow(RTUNICP uc) RT_NOEXCEPT
451{
452 RTUTF16 wszTmp[3];
453 PRTUTF16 pwszEnd = RTUtf16PutCp(wszTmp, uc);
454 *pwszEnd = '\0';
455 return appendWorkerUtf16NoThrow(&wszTmp[0], pwszEnd - &wszTmp[0]);
456}
457
458
459Bstr &Bstr::appendWorkerUtf16(PCRTUTF16 pwszSrc, size_t cwcSrc)
460{
461 size_t cwcOld = length();
462 size_t cwcTotal = cwcOld + cwcSrc;
463 reserve(cwcTotal, true /*fForce*/);
464 if (cwcSrc)
465 memcpy(&m_bstr[cwcOld], pwszSrc, cwcSrc * sizeof(RTUTF16));
466 m_bstr[cwcTotal] = '\0';
467 return *this;
468}
469
470
471HRESULT Bstr::appendWorkerUtf16NoThrow(PCRTUTF16 pwszSrc, size_t cwcSrc) RT_NOEXCEPT
472{
473 size_t cwcOld = length();
474 size_t cwcTotal = cwcOld + cwcSrc;
475 HRESULT hrc = reserveNoThrow(cwcTotal, true /*fForce*/);
476 if (hrc == S_OK)
477 {
478 if (cwcSrc)
479 memcpy(&m_bstr[cwcOld], pwszSrc, cwcSrc * sizeof(RTUTF16));
480 m_bstr[cwcTotal] = '\0';
481 }
482 return hrc;
483}
484
485
486Bstr &Bstr::appendWorkerUtf8(const char *pszSrc, size_t cchSrc)
487{
488 size_t cwcSrc;
489 int rc = RTStrCalcUtf16LenEx(pszSrc, cchSrc, &cwcSrc);
490 AssertRCStmt(rc, throw std::bad_alloc());
491
492 size_t cwcOld = length();
493 size_t cwcTotal = cwcOld + cwcSrc;
494 reserve(cwcTotal, true /*fForce*/);
495 if (cwcSrc)
496 {
497 PRTUTF16 pwszDst = &m_bstr[cwcOld];
498 rc = RTStrToUtf16Ex(pszSrc, cchSrc, &pwszDst, cwcSrc + 1, NULL);
499 AssertRCStmt(rc, throw std::bad_alloc());
500 }
501 m_bstr[cwcTotal] = '\0';
502 return *this;
503}
504
505
506HRESULT Bstr::appendWorkerUtf8NoThrow(const char *pszSrc, size_t cchSrc) RT_NOEXCEPT
507{
508 size_t cwcSrc;
509 int rc = RTStrCalcUtf16LenEx(pszSrc, cchSrc, &cwcSrc);
510 AssertRCStmt(rc, E_INVALIDARG);
511
512 size_t cwcOld = length();
513 size_t cwcTotal = cwcOld + cwcSrc;
514 HRESULT hrc = reserveNoThrow(cwcTotal, true /*fForce*/);
515 AssertReturn(hrc == S_OK, hrc);
516 if (cwcSrc)
517 {
518 PRTUTF16 pwszDst = &m_bstr[cwcOld];
519 rc = RTStrToUtf16Ex(pszSrc, cchSrc, &pwszDst, cwcSrc + 1, NULL);
520 AssertRCStmt(rc, E_INVALIDARG);
521 }
522 m_bstr[cwcTotal] = '\0';
523 return S_OK;
524}
525
526
527Bstr &Bstr::appendPrintf(const char *pszFormat, ...)
528{
529 va_list va;
530 va_start(va, pszFormat);
531 HRESULT hrc = appendPrintfVNoThrow(pszFormat, va);
532 va_end(va);
533 if (hrc != S_OK)
534 throw std::bad_alloc();
535 return *this;
536}
537
538
539HRESULT Bstr::appendPrintfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT
540{
541 va_list va;
542 va_start(va, pszFormat);
543 HRESULT hrc = appendPrintfVNoThrow(pszFormat, va);
544 va_end(va);
545 return hrc;
546}
547
548
549Bstr &Bstr::appendPrintfV(const char *pszFormat, va_list va)
550{
551 HRESULT hrc = appendPrintfVNoThrow(pszFormat, va);
552 if (hrc != S_OK)
553 throw std::bad_alloc();
554 return *this;
555}
556
557
558HRESULT Bstr::appendPrintfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT
559{
560 size_t const cwcOld = length();
561 BSTRNOTHROW Args = { this, cwcOld, cwcOld, S_OK };
562
563 RTStrFormatV(printfOutputCallbackNoThrow, &Args, NULL, NULL, pszFormat, va);
564 if (Args.hrc == S_OK)
565 {
566 Args.hrc = joltNoThrow(Args.offDst);
567 if (Args.hrc == S_OK)
568 return S_OK;
569 }
570
571 if (m_bstr)
572 m_bstr[cwcOld] = '\0';
573 return Args.hrc;
574}
575
576
577Bstr &Bstr::erase(size_t offStart /*= 0*/, size_t cwcLength /*= RTSTR_MAX*/) RT_NOEXCEPT
578{
579 size_t cwc = length();
580 if (offStart < cwc)
581 {
582 if (cwcLength >= cwc - offStart)
583 {
584 if (!offStart)
585 cleanup();
586 else
587 {
588 /* Trail removal, nothing to move. */
589 m_bstr[offStart] = '\0';
590 joltNoThrow(offStart); /* not entirely optimal... */
591 }
592 }
593 else if (cwcLength > 0)
594 {
595 /* Pull up the tail to offStart. */
596 size_t cwcAfter = cwc - offStart - cwcLength;
597 memmove(&m_bstr[offStart], &m_bstr[offStart + cwcLength], cwcAfter * sizeof(*m_bstr));
598 cwc -= cwcLength;
599 m_bstr[cwc] = '\0';
600 joltNoThrow(cwc); /* not entirely optimal... */
601 }
602 }
603 return *this;
604}
605
606
607void Bstr::cleanup()
608{
609 if (m_bstr)
610 {
611 ::SysFreeString(m_bstr);
612 m_bstr = NULL;
613 }
614}
615
616
617void Bstr::copyFrom(const OLECHAR *a_bstrSrc)
618{
619 if (a_bstrSrc && *a_bstrSrc)
620 {
621 m_bstr = ::SysAllocString(a_bstrSrc);
622 if (!m_bstr)
623 throw std::bad_alloc();
624 }
625 else
626 m_bstr = NULL;
627}
628
629
630void Bstr::cleanupAndCopyFrom(const OLECHAR *a_bstrSrc)
631{
632 cleanup();
633 copyFrom(a_bstrSrc);
634}
635
636
637/*********************************************************************************************************************************
638* Utf8Str Implementation *
639*********************************************************************************************************************************/
640
641/* static */
642const Utf8Str Utf8Str::Empty; /* default ctor is OK */
643
644#if defined(VBOX_WITH_XPCOM)
645void Utf8Str::cloneTo(char **pstr) const
646{
647 size_t cb = length() + 1;
648 *pstr = (char *)nsMemory::Alloc(cb);
649 if (RT_LIKELY(*pstr))
650 memcpy(*pstr, c_str(), cb);
651 else
652 throw std::bad_alloc();
653}
654
655HRESULT Utf8Str::cloneToEx(char **pstr) const
656{
657 size_t cb = length() + 1;
658 *pstr = (char *)nsMemory::Alloc(cb);
659 if (RT_LIKELY(*pstr))
660 {
661 memcpy(*pstr, c_str(), cb);
662 return S_OK;
663 }
664 return E_OUTOFMEMORY;
665}
666#endif
667
668Utf8Str& Utf8Str::stripTrailingSlash()
669{
670 if (length())
671 {
672 ::RTPathStripTrailingSlash(m_psz);
673 jolt();
674 }
675 return *this;
676}
677
678Utf8Str& Utf8Str::stripFilename()
679{
680 if (length())
681 {
682 RTPathStripFilename(m_psz);
683 jolt();
684 }
685 return *this;
686}
687
688Utf8Str& Utf8Str::stripPath()
689{
690 if (length())
691 {
692 char *pszName = ::RTPathFilename(m_psz);
693 if (pszName)
694 {
695 size_t cchName = length() - (pszName - m_psz);
696 memmove(m_psz, pszName, cchName + 1);
697 jolt();
698 }
699 else
700 cleanup();
701 }
702 return *this;
703}
704
705Utf8Str& Utf8Str::stripSuffix()
706{
707 if (length())
708 {
709 RTPathStripSuffix(m_psz);
710 jolt();
711 }
712 return *this;
713}
714
715size_t Utf8Str::parseKeyValue(Utf8Str &a_rKey, Utf8Str &a_rValue, size_t a_offStart /* = 0*/,
716 const Utf8Str &a_rPairSeparator /*= ","*/, const Utf8Str &a_rKeyValueSeparator /*= "="*/) const
717{
718 /* Find the end of the next pair, skipping empty pairs.
719 Note! The skipping allows us to pass the return value of a parseKeyValue()
720 call as offStart to the next call. */
721 size_t offEnd;
722 while ( a_offStart == (offEnd = find(&a_rPairSeparator, a_offStart))
723 && offEnd != npos)
724 a_offStart++;
725
726 /* Look for a key/value separator before the end of the pair.
727 ASSUMES npos value returned by find when the substring is not found is
728 really high. */
729 size_t offKeyValueSep = find(&a_rKeyValueSeparator, a_offStart);
730 if (offKeyValueSep < offEnd)
731 {
732 a_rKey = substr(a_offStart, offKeyValueSep - a_offStart);
733 if (offEnd == npos)
734 offEnd = m_cch; /* No confusing npos when returning strings. */
735 a_rValue = substr(offKeyValueSep + 1, offEnd - offKeyValueSep - 1);
736 }
737 else
738 {
739 a_rKey.setNull();
740 a_rValue.setNull();
741 }
742
743 return offEnd;
744}
745
746/**
747 * Internal function used in Utf8Str copy constructors and assignment when
748 * copying from a UTF-16 string.
749 *
750 * As with the RTCString::copyFrom() variants, this unconditionally sets the
751 * members to a copy of the given other strings and makes no assumptions about
752 * previous contents. This can therefore be used both in copy constructors,
753 * when member variables have no defined value, and in assignments after having
754 * called cleanup().
755 *
756 * This variant converts from a UTF-16 string, most probably from
757 * a Bstr assignment.
758 *
759 * @param a_pbstr The source string. The caller guarantees that this
760 * is valid UTF-16.
761 * @param a_cwcMax The number of characters to be copied. If set to RTSTR_MAX,
762 * the entire string will be copied.
763 *
764 * @sa RTCString::copyFromN
765 */
766void Utf8Str::copyFrom(CBSTR a_pbstr, size_t a_cwcMax)
767{
768 if (a_pbstr && *a_pbstr)
769 {
770 int vrc = RTUtf16ToUtf8Ex((PCRTUTF16)a_pbstr,
771 a_cwcMax, // size_t cwcString: translate entire string
772 &m_psz, // char **ppsz: output buffer
773 0, // size_t cch: if 0, func allocates buffer in *ppsz
774 &m_cch); // size_t *pcch: receives the size of the output string, excluding the terminator.
775 if (RT_SUCCESS(vrc))
776 m_cbAllocated = m_cch + 1;
777 else
778 {
779 if ( vrc != VERR_NO_STR_MEMORY
780 && vrc != VERR_NO_MEMORY)
781 {
782 /* ASSUME: input is valid Utf-16. Fake out of memory error. */
783 AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTUtf16Len(a_pbstr) * sizeof(RTUTF16), a_pbstr));
784 }
785
786 m_cch = 0;
787 m_cbAllocated = 0;
788 m_psz = NULL;
789
790 throw std::bad_alloc();
791 }
792 }
793 else
794 {
795 m_cch = 0;
796 m_cbAllocated = 0;
797 m_psz = NULL;
798 }
799}
800
801/**
802 * A variant of Utf8Str::copyFrom that does not throw any exceptions but returns
803 * E_OUTOFMEMORY instead.
804 *
805 * @param a_pbstr The source string.
806 * @returns S_OK or E_OUTOFMEMORY.
807 */
808HRESULT Utf8Str::copyFromEx(CBSTR a_pbstr)
809{
810 if (a_pbstr && *a_pbstr)
811 {
812 int vrc = RTUtf16ToUtf8Ex((PCRTUTF16)a_pbstr,
813 RTSTR_MAX, // size_t cwcString: translate entire string
814 &m_psz, // char **ppsz: output buffer
815 0, // size_t cch: if 0, func allocates buffer in *ppsz
816 &m_cch); // size_t *pcch: receives the size of the output string, excluding the terminator.
817 if (RT_SUCCESS(vrc))
818 m_cbAllocated = m_cch + 1;
819 else
820 {
821 if ( vrc != VERR_NO_STR_MEMORY
822 && vrc != VERR_NO_MEMORY)
823 {
824 /* ASSUME: input is valid Utf-16. Fake out of memory error. */
825 AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTUtf16Len(a_pbstr) * sizeof(RTUTF16), a_pbstr));
826 }
827
828 m_cch = 0;
829 m_cbAllocated = 0;
830 m_psz = NULL;
831
832 return E_OUTOFMEMORY;
833 }
834 }
835 else
836 {
837 m_cch = 0;
838 m_cbAllocated = 0;
839 m_psz = NULL;
840 }
841 return S_OK;
842}
843
844
845/**
846 * A variant of Utf8Str::copyFromN that does not throw any exceptions but
847 * returns E_OUTOFMEMORY instead.
848 *
849 * @param a_pcszSrc The source string.
850 * @param a_offSrc Start offset to copy from.
851 * @param a_cchSrc How much to copy
852 * @returns S_OK or E_OUTOFMEMORY.
853 *
854 * @remarks This calls cleanup() first, so the caller doesn't have to. (Saves
855 * code space.)
856 */
857HRESULT Utf8Str::copyFromExNComRC(const char *a_pcszSrc, size_t a_offSrc, size_t a_cchSrc)
858{
859 Assert(!a_cchSrc || !m_psz || (uintptr_t)&a_pcszSrc[a_offSrc] - (uintptr_t)m_psz >= (uintptr_t)m_cbAllocated);
860 cleanup();
861 if (a_cchSrc)
862 {
863 m_psz = RTStrAlloc(a_cchSrc + 1);
864 if (RT_LIKELY(m_psz))
865 {
866 m_cch = a_cchSrc;
867 m_cbAllocated = a_cchSrc + 1;
868 memcpy(m_psz, a_pcszSrc + a_offSrc, a_cchSrc);
869 m_psz[a_cchSrc] = '\0';
870 }
871 else
872 {
873 m_cch = 0;
874 m_cbAllocated = 0;
875 return E_OUTOFMEMORY;
876 }
877 }
878 else
879 {
880 m_cch = 0;
881 m_cbAllocated = 0;
882 m_psz = NULL;
883 }
884 return S_OK;
885}
886
887} /* 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