VirtualBox

source: vbox/trunk/include/VBox/com/string.h@ 85308

Last change on this file since 85308 was 85308, checked in by vboxsync, 4 years ago

VBox/com/string.h: Some more Bstr assignEx methods. (forgot the header) bugref:9790

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 49.8 KB
Line 
1/* $Id: string.h 85308 2020-07-13 12:38:55Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer - Smart string classes declaration.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef VBOX_INCLUDED_com_string_h
28#define VBOX_INCLUDED_com_string_h
29#ifndef RT_WITHOUT_PRAGMA_ONCE
30# pragma once
31#endif
32
33/* Make sure all the stdint.h macros are included - must come first! */
34#ifndef __STDC_LIMIT_MACROS
35# define __STDC_LIMIT_MACROS
36#endif
37#ifndef __STDC_CONSTANT_MACROS
38# define __STDC_CONSTANT_MACROS
39#endif
40
41#if defined(VBOX_WITH_XPCOM)
42# include <nsMemory.h>
43#endif
44
45#include "VBox/com/defs.h"
46#include "VBox/com/assert.h"
47
48#include <iprt/mem.h>
49#include <iprt/utf16.h>
50#include <iprt/cpp/ministring.h>
51
52
53/** @defgroup grp_com_str Smart String Classes
54 * @ingroup grp_com
55 * @{
56 */
57
58namespace com
59{
60
61class Utf8Str;
62
63// global constant in glue/string.cpp that represents an empty BSTR
64extern const BSTR g_bstrEmpty;
65
66/**
67 * String class used universally in Main for COM-style Utf-16 strings.
68 *
69 * Unfortunately COM on Windows uses UTF-16 everywhere, requiring conversions
70 * back and forth since most of VirtualBox and our libraries use UTF-8.
71 *
72 * To make things more obscure, on Windows, a COM-style BSTR is not just a
73 * pointer to a null-terminated wide character array, but the four bytes (32
74 * bits) BEFORE the memory that the pointer points to are a length DWORD. One
75 * must therefore avoid pointer arithmetic and always use SysAllocString and
76 * the like to deal with BSTR pointers, which manage that DWORD correctly.
77 *
78 * For platforms other than Windows, we provide our own versions of the Sys*
79 * functions in Main/xpcom/helpers.cpp which do NOT use length prefixes though
80 * to be compatible with how XPCOM allocates string parameters to public
81 * functions.
82 *
83 * The Bstr class hides all this handling behind a std::string-like interface
84 * and also provides automatic conversions to RTCString and Utf8Str instances.
85 *
86 * The one advantage of using the SysString* routines is that this makes it
87 * possible to use it as a type of member variables of COM/XPCOM components and
88 * pass their values to callers through component methods' output parameters
89 * using the #cloneTo() operation. Also, the class can adopt (take ownership
90 * of) string buffers returned in output parameters of COM methods using the
91 * #asOutParam() operation and correctly free them afterwards.
92 *
93 * Starting with VirtualBox 3.2, like Utf8Str, Bstr no longer differentiates
94 * between NULL strings and empty strings. In other words, Bstr("") and
95 * Bstr(NULL) behave the same. In both cases, Bstr allocates no memory,
96 * reports a zero length and zero allocated bytes for both, and returns an
97 * empty C wide string from raw().
98 *
99 * @note All Bstr methods ASSUMES valid UTF-16 or UTF-8 input strings.
100 * The VirtualBox policy in this regard is to validate strings coming
101 * from external sources before passing them to Bstr or Utf8Str.
102 */
103class Bstr
104{
105public:
106
107 Bstr()
108 : m_bstr(NULL)
109 { }
110
111 Bstr(const Bstr &that)
112 {
113 copyFrom((const OLECHAR *)that.m_bstr);
114 }
115
116 Bstr(CBSTR that)
117 {
118 copyFrom((const OLECHAR *)that);
119 }
120
121#if defined(VBOX_WITH_XPCOM)
122 Bstr(const wchar_t *that)
123 {
124 AssertCompile(sizeof(wchar_t) == sizeof(OLECHAR));
125 copyFrom((const OLECHAR *)that);
126 }
127#endif
128
129 Bstr(const RTCString &that)
130 {
131 copyFrom(that.c_str());
132 }
133
134 Bstr(const char *that)
135 {
136 copyFrom(that);
137 }
138
139 Bstr(const char *a_pThat, size_t a_cchMax)
140 {
141 copyFromN(a_pThat, a_cchMax);
142 }
143
144 ~Bstr()
145 {
146 setNull();
147 }
148
149 Bstr &operator=(const Bstr &that)
150 {
151 cleanupAndCopyFrom((const OLECHAR *)that.m_bstr);
152 return *this;
153 }
154
155 Bstr &operator=(CBSTR that)
156 {
157 cleanupAndCopyFrom((const OLECHAR *)that);
158 return *this;
159 }
160
161#if defined(VBOX_WITH_XPCOM)
162 Bstr &operator=(const wchar_t *that)
163 {
164 cleanupAndCopyFrom((const OLECHAR *)that);
165 return *this;
166 }
167#endif
168
169 Bstr &setNull()
170 {
171 cleanup();
172 return *this;
173 }
174
175 /**
176 * Extended assignment method that returns a COM status code instead of an
177 * exception on failure.
178 *
179 * @returns S_OK or E_OUTOFMEMORY.
180 * @param a_rSrcStr The source string
181 */
182 HRESULT assignEx(const Bstr &a_rSrcStr) RT_NOEXCEPT
183 {
184 return cleanupAndCopyFromEx((const OLECHAR *)a_rSrcStr.m_bstr);
185 }
186
187 /**
188 * Extended assignment method that returns a COM status code instead of an
189 * exception on failure.
190 *
191 * @returns S_OK or E_OUTOFMEMORY.
192 * @param a_pSrcStr The source string
193 */
194 HRESULT assignEx(CBSTR a_pSrcStr) RT_NOEXCEPT
195 {
196 return cleanupAndCopyFromEx((const OLECHAR *)a_pSrcStr);
197 }
198
199 /**
200 * Assign the value of a RTCString/Utf8Str string, no exceptions.
201 *
202 * @returns S_OK or E_OUTOFMEMORY.
203 * @param a_rSrcStr The source string
204 */
205 HRESULT assignEx(RTCString const &a_rSrcStr) RT_NOEXCEPT
206 {
207 return cleanupAndCopyFromNoThrow(a_rSrcStr.c_str(), a_rSrcStr.length());
208 }
209
210 /**
211 * Assign the value of a RTCString/Utf8Str substring, no exceptions.
212 *
213 * @returns S_OK, E_OUTOFMEMORY or E_INVALIDARG.
214 * @param a_rSrcStr The source string
215 * @param a_offSrc The character (byte) offset of the substring.
216 * @param a_cchSrc The number of characters (bytes) to copy from the source
217 * string.
218 */
219 HRESULT assignEx(RTCString const &a_rSrcStr, size_t a_offSrc, size_t a_cchSrc) RT_NOEXCEPT
220 {
221 size_t const cchTmp = a_rSrcStr.length();
222 if ( a_offSrc + a_cchSrc < cchTmp
223 && a_offSrc < cchTmp)
224 return cleanupAndCopyFromNoThrow(a_rSrcStr.c_str() + a_offSrc, a_cchSrc);
225 return E_INVALIDARG;
226 }
227
228 /**
229 * Assign the value of a zero terminated UTF-8 string, no exceptions.
230 *
231 * @returns S_OK or E_OUTOFMEMORY.
232 * @param a_pszSrcStr The source string.
233 */
234 HRESULT assignEx(const char *a_pszSrcStr) RT_NOEXCEPT
235 {
236 return cleanupAndCopyFromNoThrow(a_pszSrcStr, RTSTR_MAX);
237 }
238
239 /**
240 * Assign the value of a UTF-8 substring, no exceptions.
241 *
242 * @returns S_OK or E_OUTOFMEMORY.
243 * @param a_pszSrcStr The source string.
244 * @param a_cchSrc The number of characters (bytes) to copy from the source
245 * string.
246 */
247 HRESULT assignEx(const char *a_pszSrcStr, size_t a_cchSrc) RT_NOEXCEPT
248 {
249 return cleanupAndCopyFromNoThrow(a_pszSrcStr, a_cchSrc);
250 }
251
252#ifdef _MSC_VER
253# if _MSC_VER >= 1400
254 RTMEMEF_NEW_AND_DELETE_OPERATORS();
255# endif
256#else
257 RTMEMEF_NEW_AND_DELETE_OPERATORS();
258#endif
259
260 /** Case sensitivity selector. */
261 enum CaseSensitivity
262 {
263 CaseSensitive,
264 CaseInsensitive
265 };
266
267 /**
268 * Compares the member string to str.
269 * @param str
270 * @param cs Whether comparison should be case-sensitive.
271 * @return
272 */
273 int compare(CBSTR str, CaseSensitivity cs = CaseSensitive) const
274 {
275 if (cs == CaseSensitive)
276 return ::RTUtf16Cmp((PRTUTF16)m_bstr, (PRTUTF16)str);
277 return ::RTUtf16LocaleICmp((PRTUTF16)m_bstr, (PRTUTF16)str);
278 }
279
280 int compare(BSTR str, CaseSensitivity cs = CaseSensitive) const
281 {
282 return compare((CBSTR)str, cs);
283 }
284
285 int compare(const Bstr &that, CaseSensitivity cs = CaseSensitive) const
286 {
287 return compare(that.m_bstr, cs);
288 }
289
290 bool operator==(const Bstr &that) const { return !compare(that.m_bstr); }
291 bool operator==(CBSTR that) const { return !compare(that); }
292 bool operator==(BSTR that) const { return !compare(that); }
293 bool operator!=(const Bstr &that) const { return !!compare(that.m_bstr); }
294 bool operator!=(CBSTR that) const { return !!compare(that); }
295 bool operator!=(BSTR that) const { return !!compare(that); }
296 bool operator<(const Bstr &that) const { return compare(that.m_bstr) < 0; }
297 bool operator<(CBSTR that) const { return compare(that) < 0; }
298 bool operator<(BSTR that) const { return compare(that) < 0; }
299 bool operator<=(const Bstr &that) const { return compare(that.m_bstr) <= 0; }
300 bool operator<=(CBSTR that) const { return compare(that) <= 0; }
301 bool operator<=(BSTR that) const { return compare(that) <= 0; }
302 bool operator>(const Bstr &that) const { return compare(that.m_bstr) > 0; }
303 bool operator>(CBSTR that) const { return compare(that) > 0; }
304 bool operator>(BSTR that) const { return compare(that) > 0; }
305 bool operator>=(const Bstr &that) const { return compare(that.m_bstr) >= 0; }
306 bool operator>=(CBSTR that) const { return compare(that) >= 0; }
307 bool operator>=(BSTR that) const { return compare(that) >= 0; }
308
309 /**
310 * Compares this string to an UTF-8 C style string.
311 *
312 * @retval 0 if equal
313 * @retval -1 if this string is smaller than the UTF-8 one.
314 * @retval 1 if the UTF-8 string is smaller than this.
315 *
316 * @param a_pszRight The string to compare with.
317 * @param a_enmCase Whether comparison should be case-sensitive.
318 */
319 int compareUtf8(const char *a_pszRight, CaseSensitivity a_enmCase = CaseSensitive) const;
320
321 /** Java style compare method.
322 * @returns true if @a a_pszRight equals this string.
323 * @param a_pszRight The (UTF-8) string to compare with. */
324 bool equals(const char *a_pszRight) const { return compareUtf8(a_pszRight, CaseSensitive) == 0; }
325
326 /** Java style case-insensitive compare method.
327 * @returns true if @a a_pszRight equals this string.
328 * @param a_pszRight The (UTF-8) string to compare with. */
329 bool equalsIgnoreCase(const char *a_pszRight) const { return compareUtf8(a_pszRight, CaseInsensitive) == 0; }
330
331 /** Java style compare method.
332 * @returns true if @a a_rThat equals this string.
333 * @param a_rThat The other Bstr instance to compare with. */
334 bool equals(const Bstr &a_rThat) const { return compare(a_rThat.m_bstr, CaseSensitive) == 0; }
335 /** Java style case-insensitive compare method.
336 * @returns true if @a a_rThat equals this string.
337 * @param a_rThat The other Bstr instance to compare with. */
338 bool equalsIgnoreCase(const Bstr &a_rThat) const { return compare(a_rThat.m_bstr, CaseInsensitive) == 0; }
339
340 /** Java style compare method.
341 * @returns true if @a a_pThat equals this string.
342 * @param a_pThat The native const BSTR to compare with. */
343 bool equals(CBSTR a_pThat) const { return compare(a_pThat, CaseSensitive) == 0; }
344 /** Java style case-insensitive compare method.
345 * @returns true if @a a_pThat equals this string.
346 * @param a_pThat The native const BSTR to compare with. */
347 bool equalsIgnoreCase(CBSTR a_pThat) const { return compare(a_pThat, CaseInsensitive) == 0; }
348
349 /** Java style compare method.
350 * @returns true if @a a_pThat equals this string.
351 * @param a_pThat The native BSTR to compare with. */
352 bool equals(BSTR a_pThat) const { return compare(a_pThat, CaseSensitive) == 0; }
353 /** Java style case-insensitive compare method.
354 * @returns true if @a a_pThat equals this string.
355 * @param a_pThat The native BSTR to compare with. */
356 bool equalsIgnoreCase(BSTR a_pThat) const { return compare(a_pThat, CaseInsensitive) == 0; }
357
358 /**
359 * Returns true if the member string has no length.
360 * This is true for instances created from both NULL and "" input strings.
361 *
362 * @note Always use this method to check if an instance is empty. Do not
363 * use length() because that may need to run through the entire string
364 * (Bstr does not cache string lengths).
365 */
366 bool isEmpty() const { return m_bstr == NULL || *m_bstr == 0; }
367
368 /**
369 * Returns true if the member string has a length of one or more.
370 *
371 * @returns true if not empty, false if empty (NULL or "").
372 */
373 bool isNotEmpty() const { return m_bstr != NULL && *m_bstr != 0; }
374
375 size_t length() const { return isEmpty() ? 0 : ::RTUtf16Len((PRTUTF16)m_bstr); }
376
377 /**
378 * Assigns the output of the string format operation (RTStrPrintf).
379 *
380 * @param pszFormat Pointer to the format string,
381 * @see pg_rt_str_format.
382 * @param ... Ellipsis containing the arguments specified by
383 * the format string.
384 *
385 * @throws std::bad_alloc On allocation error. Object state is undefined.
386 *
387 * @returns Reference to the object.
388 */
389 Bstr &printf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
390
391 /**
392 * Assigns the output of the string format operation (RTStrPrintf).
393 *
394 * @param pszFormat Pointer to the format string,
395 * @see pg_rt_str_format.
396 * @param ... Ellipsis containing the arguments specified by
397 * the format string.
398 *
399 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
400 */
401 HRESULT printfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 2);
402
403 /**
404 * Assigns the output of the string format operation (RTStrPrintfV).
405 *
406 * @param pszFormat Pointer to the format string,
407 * @see pg_rt_str_format.
408 * @param va Argument vector containing the arguments
409 * specified by the format string.
410 *
411 * @throws std::bad_alloc On allocation error. Object state is undefined.
412 *
413 * @returns Reference to the object.
414 */
415 Bstr &printfV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
416
417 /**
418 * Assigns the output of the string format operation (RTStrPrintfV).
419 *
420 * @param pszFormat Pointer to the format string,
421 * @see pg_rt_str_format.
422 * @param va Argument vector containing the arguments
423 * specified by the format string.
424 *
425 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
426 */
427 HRESULT printfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 0);
428
429 /** @name Append methods and operators
430 * @{ */
431
432 /**
433 * Appends the string @a that to @a rThat.
434 *
435 * @param rThat The string to append.
436 * @throws std::bad_alloc On allocation error. The object is left unchanged.
437 * @returns Reference to the object.
438 */
439 Bstr &append(const Bstr &rThat);
440
441 /**
442 * Appends the string @a that to @a rThat.
443 *
444 * @param rThat The string to append.
445 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
446 */
447 HRESULT appendNoThrow(const Bstr &rThat) RT_NOEXCEPT;
448
449 /**
450 * Appends the UTF-8 string @a that to @a rThat.
451 *
452 * @param rThat The string to append.
453 * @throws std::bad_alloc On allocation error. The object is left unchanged.
454 * @returns Reference to the object.
455 */
456 Bstr &append(const RTCString &rThat);
457
458 /**
459 * Appends the UTF-8 string @a that to @a rThat.
460 *
461 * @param rThat The string to append.
462 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
463 */
464 HRESULT appendNoThrow(const RTCString &rThat) RT_NOEXCEPT;
465
466 /**
467 * Appends the UTF-16 string @a pszSrc to @a this.
468 *
469 * @param pwszSrc The C-style UTF-16 string to append.
470 * @throws std::bad_alloc On allocation error. The object is left unchanged.
471 * @returns Reference to the object.
472 */
473 Bstr &append(CBSTR pwszSrc);
474
475 /**
476 * Appends the UTF-16 string @a pszSrc to @a this.
477 *
478 * @param pwszSrc The C-style UTF-16 string to append.
479 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
480 */
481 HRESULT appendNoThrow(CBSTR pwszSrc) RT_NOEXCEPT;
482
483 /**
484 * Appends the UTF-8 string @a pszSrc to @a this.
485 *
486 * @param pszSrc The C-style string to append.
487 * @throws std::bad_alloc On allocation error. The object is left unchanged.
488 * @returns Reference to the object.
489 */
490 Bstr &append(const char *pszSrc);
491
492 /**
493 * Appends the UTF-8 string @a pszSrc to @a this.
494 *
495 * @param pszSrc The C-style string to append.
496 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
497 */
498 HRESULT appendNoThrow(const char *pszSrc) RT_NOEXCEPT;
499
500 /**
501 * Appends the a substring from @a rThat to @a this.
502 *
503 * @param rThat The string to append a substring from.
504 * @param offStart The start of the substring to append (UTF-16
505 * offset, not codepoint).
506 * @param cwcMax The maximum number of UTF-16 units to append.
507 * @throws std::bad_alloc On allocation error. The object is left unchanged.
508 * @returns Reference to the object.
509 */
510 Bstr &append(const Bstr &rThat, size_t offStart, size_t cwcMax = RTSTR_MAX);
511
512 /**
513 * Appends the a substring from @a rThat to @a this.
514 *
515 * @param rThat The string to append a substring from.
516 * @param offStart The start of the substring to append (UTF-16
517 * offset, not codepoint).
518 * @param cwcMax The maximum number of UTF-16 units to append.
519 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
520 */
521 HRESULT appendNoThrow(const Bstr &rThat, size_t offStart, size_t cwcMax = RTSTR_MAX) RT_NOEXCEPT;
522
523 /**
524 * Appends the a substring from UTF-8 @a rThat to @a this.
525 *
526 * @param rThat The string to append a substring from.
527 * @param offStart The start of the substring to append (byte offset,
528 * not codepoint).
529 * @param cchMax The maximum number of bytes to append.
530 * @throws std::bad_alloc On allocation error. The object is left unchanged.
531 * @returns Reference to the object.
532 */
533 Bstr &append(const RTCString &rThat, size_t offStart, size_t cchMax = RTSTR_MAX);
534
535 /**
536 * Appends the a substring from UTF-8 @a rThat to @a this.
537 *
538 * @param rThat The string to append a substring from.
539 * @param offStart The start of the substring to append (byte offset,
540 * not codepoint).
541 * @param cchMax The maximum number of bytes to append.
542 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
543 */
544 HRESULT appendNoThrow(const RTCString &rThat, size_t offStart, size_t cchMax = RTSTR_MAX) RT_NOEXCEPT;
545
546 /**
547 * Appends the first @a cchMax chars from UTF-16 string @a pszThat to @a this.
548 *
549 * @param pwszThat The C-style UTF-16 string to append.
550 * @param cchMax The maximum number of bytes to append.
551 * @throws std::bad_alloc On allocation error. The object is left unchanged.
552 * @returns Reference to the object.
553 */
554 Bstr &append(CBSTR pwszThat, size_t cchMax);
555
556 /**
557 * Appends the first @a cchMax chars from UTF-16 string @a pszThat to @a this.
558 *
559 * @param pwszThat The C-style UTF-16 string to append.
560 * @param cchMax The maximum number of bytes to append.
561 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
562 */
563 HRESULT appendNoThrow(CBSTR pwszThat, size_t cchMax) RT_NOEXCEPT;
564
565 /**
566 * Appends the first @a cchMax chars from string @a pszThat to @a this.
567 *
568 * @param pszThat The C-style string to append.
569 * @param cchMax The maximum number of bytes to append.
570 * @throws std::bad_alloc On allocation error. The object is left unchanged.
571 * @returns Reference to the object.
572 */
573 Bstr &append(const char *pszThat, size_t cchMax);
574
575 /**
576 * Appends the first @a cchMax chars from string @a pszThat to @a this.
577 *
578 * @param pszThat The C-style string to append.
579 * @param cchMax The maximum number of bytes to append.
580 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
581 */
582 HRESULT appendNoThrow(const char *pszThat, size_t cchMax) RT_NOEXCEPT;
583
584 /**
585 * Appends the given character to @a this.
586 *
587 * @param ch The character to append.
588 * @throws std::bad_alloc On allocation error. The object is left unchanged.
589 * @returns Reference to the object.
590 */
591 Bstr &append(char ch);
592
593 /**
594 * Appends the given character to @a this.
595 *
596 * @param ch The character to append.
597 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
598 */
599 HRESULT appendNoThrow(char ch) RT_NOEXCEPT;
600
601 /**
602 * Appends the given unicode code point to @a this.
603 *
604 * @param uc The unicode code point to append.
605 * @throws std::bad_alloc On allocation error. The object is left unchanged.
606 * @returns Reference to the object.
607 */
608 Bstr &appendCodePoint(RTUNICP uc);
609
610 /**
611 * Appends the given unicode code point to @a this.
612 *
613 * @param uc The unicode code point to append.
614 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
615 */
616 HRESULT appendCodePointNoThrow(RTUNICP uc) RT_NOEXCEPT;
617
618 /**
619 * Appends the output of the string format operation (RTStrPrintf).
620 *
621 * @param pszFormat Pointer to the format string,
622 * @see pg_rt_str_format.
623 * @param ... Ellipsis containing the arguments specified by
624 * the format string.
625 *
626 * @throws std::bad_alloc On allocation error. Object state is undefined.
627 *
628 * @returns Reference to the object.
629 */
630 Bstr &appendPrintf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
631
632 /**
633 * Appends the output of the string format operation (RTStrPrintf).
634 *
635 * @param pszFormat Pointer to the format string,
636 * @see pg_rt_str_format.
637 * @param ... Ellipsis containing the arguments specified by
638 * the format string.
639 *
640 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
641 */
642 HRESULT appendPrintfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 2);
643
644 /**
645 * Appends the output of the string format operation (RTStrPrintfV).
646 *
647 * @param pszFormat Pointer to the format string,
648 * @see pg_rt_str_format.
649 * @param va Argument vector containing the arguments
650 * specified by the format string.
651 *
652 * @throws std::bad_alloc On allocation error. Object state is undefined.
653 *
654 * @returns Reference to the object.
655 */
656 Bstr &appendPrintfV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
657
658 /**
659 * Appends the output of the string format operation (RTStrPrintfV).
660 *
661 * @param pszFormat Pointer to the format string,
662 * @see pg_rt_str_format.
663 * @param va Argument vector containing the arguments
664 * specified by the format string.
665 *
666 * @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
667 */
668 HRESULT appendPrintfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 0);
669
670 /**
671 * Shortcut to append(), Bstr variant.
672 *
673 * @param rThat The string to append.
674 * @returns Reference to the object.
675 */
676 Bstr &operator+=(const Bstr &rThat)
677 {
678 return append(rThat);
679 }
680
681 /**
682 * Shortcut to append(), RTCString variant.
683 *
684 * @param rThat The string to append.
685 * @returns Reference to the object.
686 */
687 Bstr &operator+=(const RTCString &rThat)
688 {
689 return append(rThat);
690 }
691
692 /**
693 * Shortcut to append(), CBSTR variant.
694 *
695 * @param pwszThat The C-style string to append.
696 * @returns Reference to the object.
697 */
698 Bstr &operator+=(CBSTR pwszThat)
699 {
700 return append(pwszThat);
701 }
702
703 /**
704 * Shortcut to append(), const char * variant.
705 *
706 * @param pszThat The C-style string to append.
707 * @returns Reference to the object.
708 */
709 Bstr &operator+=(const char *pszThat)
710 {
711 return append(pszThat);
712 }
713
714 /**
715 * Shortcut to append(), char variant.
716 *
717 * @param ch The character to append.
718 *
719 * @returns Reference to the object.
720 */
721 Bstr &operator+=(char ch)
722 {
723 return append(ch);
724 }
725
726 /** @} */
727
728 /**
729 * Erases a sequence from the string.
730 *
731 * @returns Reference to the object.
732 * @param offStart Where in @a this string to start erasing (UTF-16
733 * units, not codepoints).
734 * @param cwcLength How much following @a offStart to erase (UTF-16
735 * units, not codepoints).
736 */
737 Bstr &erase(size_t offStart = 0, size_t cwcLength = RTSTR_MAX) RT_NOEXCEPT;
738
739
740 /** @name BASE64 related methods
741 * @{ */
742 /**
743 * Encodes the given data as BASE64.
744 *
745 * @returns S_OK or E_OUTOFMEMORY.
746 * @param pvData Pointer to the data to encode.
747 * @param cbData Number of bytes to encode.
748 * @param fLineBreaks Whether to add line breaks (true) or just encode it
749 * as a continuous string.
750 * @sa RTBase64EncodeUtf16
751 */
752 HRESULT base64Encode(const void *pvData, size_t cbData, bool fLineBreaks = false);
753
754 /**
755 * Decodes the string as BASE64.
756 *
757 * @returns IPRT status code, see RTBase64DecodeUtf16Ex.
758 * @param pvData Where to return the decoded bytes.
759 * @param cbData Size of the @a pvData return buffer.
760 * @param pcbActual Where to return number of bytes actually decoded.
761 * This is optional and if not specified, the request
762 * will fail unless @a cbData matches the data size
763 * exactly.
764 * @param ppwszEnd Where to return pointer to the first non-base64
765 * character following the encoded data. This is
766 * optional and if NULL, the request will fail if there
767 * are anything trailing the encoded bytes in the
768 * string.
769 * @sa base64DecodedSize, RTBase64DecodeUtf16
770 */
771 int base64Decode(void *pvData, size_t cbData, size_t *pcbActual = NULL, PRTUTF16 *ppwszEnd = NULL);
772
773 /**
774 * Determins the size of the BASE64 encoded data in the string.
775 *
776 * @returns The length in bytes. -1 if the encoding is bad.
777 *
778 * @param ppwszEnd If not NULL, this will point to the first char
779 * following the Base64 encoded text block. If
780 * NULL the entire string is assumed to be Base64.
781 * @sa base64Decode, RTBase64DecodedUtf16Size
782 */
783 ssize_t base64DecodedSize(PRTUTF16 *ppwszEnd = NULL);
784 /** @} */
785
786#if defined(VBOX_WITH_XPCOM)
787 /**
788 * Returns a pointer to the raw member UTF-16 string. If the member string is empty,
789 * returns a pointer to a global variable containing an empty BSTR with a proper zero
790 * length prefix so that Windows is happy.
791 */
792 CBSTR raw() const
793 {
794 if (m_bstr)
795 return m_bstr;
796
797 return g_bstrEmpty;
798 }
799#else
800 /**
801 * Windows-only hack, as the automatically generated headers use BSTR.
802 * So if we don't want to cast like crazy we have to be more loose than
803 * on XPCOM.
804 *
805 * Returns a pointer to the raw member UTF-16 string. If the member string is empty,
806 * returns a pointer to a global variable containing an empty BSTR with a proper zero
807 * length prefix so that Windows is happy.
808 */
809 BSTR raw() const
810 {
811 if (m_bstr)
812 return m_bstr;
813
814 return g_bstrEmpty;
815 }
816#endif
817
818 /**
819 * Returns a non-const raw pointer that allows modifying the string directly.
820 *
821 * @note As opposed to raw(), this DOES return NULL if the member string is
822 * empty because we cannot return a mutable pointer to the global variable
823 * with the empty string.
824 *
825 * @note If modifying the string size (only shrinking it is allows), #jolt() or
826 * #joltNoThrow() must be called!
827 *
828 * @note Do not modify memory beyond the #length() of the string!
829 *
830 * @sa joltNoThrow(), mutalbleRaw(), reserve(), reserveNoThrow()
831 */
832 BSTR mutableRaw() { return m_bstr; }
833
834 /**
835 * Correct the embedded length after using mutableRaw().
836 *
837 * This is needed on COM (Windows) to update the embedded string length. It is
838 * a stub on hosts using XPCOM.
839 *
840 * @param cwcNew The new string length, if handy, otherwise a negative
841 * number.
842 * @sa joltNoThrow(), mutalbleRaw(), reserve(), reserveNoThrow()
843 */
844#ifndef VBOX_WITH_XPCOM
845 void jolt(ssize_t cwcNew = -1);
846#else
847 void jolt(ssize_t cwcNew = -1)
848 {
849 Assert(cwcNew < 0 || (cwcNew == 0 && !m_bstr) || m_bstr[cwcNew] == '\0'); RT_NOREF(cwcNew);
850 }
851#endif
852
853 /**
854 * Correct the embedded length after using mutableRaw().
855 *
856 * This is needed on COM (Windows) to update the embedded string length. It is
857 * a stub on hosts using XPCOM.
858 *
859 * @returns S_OK on success, E_OUTOFMEMORY if shrinking the string failed.
860 * @param cwcNew The new string length, if handy, otherwise a negative
861 * number.
862 * @sa jolt(), mutalbleRaw(), reserve(), reserveNoThrow()
863 */
864#ifndef VBOX_WITH_XPCOM
865 HRESULT joltNoThrow(ssize_t cwcNew = -1) RT_NOEXCEPT;
866#else
867 HRESULT joltNoThrow(ssize_t cwcNew = -1) RT_NOEXCEPT
868 {
869 Assert(cwcNew < 0 || (cwcNew == 0 && !m_bstr) || m_bstr[cwcNew] == '\0'); RT_NOREF(cwcNew);
870 return S_OK;
871 }
872#endif
873
874 /**
875 * Make sure at that least @a cwc of buffer space is reserved.
876 *
877 * Requests that the contained memory buffer have at least cb bytes allocated.
878 * This may expand or shrink the string's storage, but will never truncate the
879 * contained string. In other words, cb will be ignored if it's smaller than
880 * length() + 1.
881 *
882 * @param cwcMin The new minimum string length that the can be stored. This
883 * does not include the terminator.
884 * @param fForce Force this size.
885 *
886 * @throws std::bad_alloc On allocation error. The object is left unchanged.
887 */
888 void reserve(size_t cwcMin, bool fForce = false);
889
890 /**
891 * A C like version of the #reserve() method, i.e. return code instead of throw.
892 *
893 * @returns S_OK or E_OUTOFMEMORY.
894 * @param cwcMin The new minimum string length that the can be stored. This
895 * does not include the terminator.
896 * @param fForce Force this size.
897 */
898 HRESULT reserveNoThrow(size_t cwcMin, bool fForce = false) RT_NOEXCEPT;
899
900 /**
901 * Intended to assign copies of instances to |BSTR| out parameters from
902 * within the interface method. Transfers the ownership of the duplicated
903 * string to the caller.
904 *
905 * If the member string is empty, this allocates an empty BSTR in *pstr
906 * (i.e. makes it point to a new buffer with a null byte).
907 *
908 * @deprecated Use cloneToEx instead to avoid throwing exceptions.
909 */
910 void cloneTo(BSTR *pstr) const
911 {
912 if (pstr)
913 {
914 *pstr = ::SysAllocString((const OLECHAR *)raw()); // raw() returns a pointer to "" if empty
915#ifdef RT_EXCEPTIONS_ENABLED
916 if (!*pstr)
917 throw std::bad_alloc();
918#endif
919 }
920 }
921
922 /**
923 * A version of cloneTo that does not throw any out of memory exceptions, but
924 * returns E_OUTOFMEMORY intead.
925 * @returns S_OK or E_OUTOFMEMORY.
926 */
927 HRESULT cloneToEx(BSTR *pstr) const
928 {
929 if (!pstr)
930 return S_OK;
931 *pstr = ::SysAllocString((const OLECHAR *)raw()); // raw() returns a pointer to "" if empty
932 return pstr ? S_OK : E_OUTOFMEMORY;
933 }
934
935 /**
936 * Intended to assign instances to |BSTR| out parameters from within the
937 * interface method. Transfers the ownership of the original string to the
938 * caller and resets the instance to null.
939 *
940 * As opposed to cloneTo(), this method doesn't create a copy of the
941 * string.
942 *
943 * If the member string is empty, this allocates an empty BSTR in *pstr
944 * (i.e. makes it point to a new buffer with a null byte).
945 *
946 * @param pbstrDst The BSTR variable to detach the string to.
947 *
948 * @throws std::bad_alloc if we failed to allocate a new empty string.
949 */
950 void detachTo(BSTR *pbstrDst)
951 {
952 if (m_bstr)
953 {
954 *pbstrDst = m_bstr;
955 m_bstr = NULL;
956 }
957 else
958 {
959 // allocate null BSTR
960 *pbstrDst = ::SysAllocString((const OLECHAR *)g_bstrEmpty);
961#ifdef RT_EXCEPTIONS_ENABLED
962 if (!*pbstrDst)
963 throw std::bad_alloc();
964#endif
965 }
966 }
967
968 /**
969 * A version of detachTo that does not throw exceptions on out-of-memory
970 * conditions, but instead returns E_OUTOFMEMORY.
971 *
972 * @param pbstrDst The BSTR variable to detach the string to.
973 * @returns S_OK or E_OUTOFMEMORY.
974 */
975 HRESULT detachToEx(BSTR *pbstrDst)
976 {
977 if (m_bstr)
978 {
979 *pbstrDst = m_bstr;
980 m_bstr = NULL;
981 }
982 else
983 {
984 // allocate null BSTR
985 *pbstrDst = ::SysAllocString((const OLECHAR *)g_bstrEmpty);
986 if (!*pbstrDst)
987 return E_OUTOFMEMORY;
988 }
989 return S_OK;
990 }
991
992 /**
993 * Intended to pass instances as |BSTR| out parameters to methods.
994 * Takes the ownership of the returned data.
995 */
996 BSTR *asOutParam()
997 {
998 cleanup();
999 return &m_bstr;
1000 }
1001
1002 /**
1003 * Static immutable empty-string object. May be used for comparison purposes.
1004 */
1005 static const Bstr Empty;
1006
1007protected:
1008
1009 void cleanup();
1010
1011 /**
1012 * Protected internal helper to copy a string. This ignores the previous object
1013 * state, so either call this from a constructor or call cleanup() first.
1014 *
1015 * This variant copies from a zero-terminated UTF-16 string (which need not
1016 * be a BSTR, i.e. need not have a length prefix).
1017 *
1018 * If the source is empty, this sets the member string to NULL.
1019 *
1020 * @param a_bstrSrc The source string. The caller guarantees
1021 * that this is valid UTF-16.
1022 *
1023 * @throws std::bad_alloc - the object is representing an empty string.
1024 */
1025 void copyFrom(const OLECHAR *a_bstrSrc);
1026
1027 /** cleanup() + copyFrom() - for assignment operators. */
1028 void cleanupAndCopyFrom(const OLECHAR *a_bstrSrc);
1029
1030 /**
1031 * Protected internal helper to copy a string, implying cleanup().
1032 *
1033 * This variant copies from a zero-terminated UTF-16 string (which need not be a
1034 * BSTR, i.e. need not have a length prefix).
1035 *
1036 * If the source is empty, this sets the member string to NULL.
1037 *
1038 * @param a_bstrSrc The source string. The caller guarantees
1039 * that this is valid UTF-16.
1040 * @returns S_OK or E_OUTOFMEMORY
1041 */
1042 HRESULT cleanupAndCopyFromEx(const OLECHAR *a_bstrSrc) RT_NOEXCEPT;
1043
1044 /**
1045 * Protected internal helper to copy a string. This ignores the previous object
1046 * state, so either call this from a constructor or call cleanup() first.
1047 *
1048 * This variant copies and converts from a zero-terminated UTF-8 string.
1049 *
1050 * If the source is empty, this sets the member string to NULL.
1051 *
1052 * @param a_pszSrc The source string. The caller guarantees
1053 * that this is valid UTF-8.
1054 *
1055 * @throws std::bad_alloc - the object is representing an empty string.
1056 */
1057 void copyFrom(const char *a_pszSrc)
1058 {
1059 copyFromN(a_pszSrc, RTSTR_MAX);
1060 }
1061
1062 /**
1063 * Variant of copyFrom for sub-string constructors.
1064 *
1065 * @param a_pszSrc The source string. The caller guarantees
1066 * that this is valid UTF-8.
1067 * @param a_cchSrc The maximum number of chars (not codepoints) to
1068 * copy. If you pass RTSTR_MAX it'll be exactly
1069 * like copyFrom().
1070 *
1071 * @throws std::bad_alloc - the object is representing an empty string.
1072 */
1073 void copyFromN(const char *a_pszSrc, size_t a_cchSrc);
1074
1075 /** cleanup() + non-throwing copyFromN(). */
1076 HRESULT cleanupAndCopyFromNoThrow(const char *a_pszSrc, size_t a_cchMax) RT_NOEXCEPT;
1077
1078 Bstr &appendWorkerUtf16(PCRTUTF16 pwszSrc, size_t cwcSrc);
1079 Bstr &appendWorkerUtf8(const char *pszSrc, size_t cchSrc);
1080 HRESULT appendWorkerUtf16NoThrow(PCRTUTF16 pwszSrc, size_t cwcSrc) RT_NOEXCEPT;
1081 HRESULT appendWorkerUtf8NoThrow(const char *pszSrc, size_t cchSrc) RT_NOEXCEPT;
1082
1083 static DECLCALLBACK(size_t) printfOutputCallbackNoThrow(void *pvArg, const char *pachChars, size_t cbChars) RT_NOEXCEPT;
1084
1085 BSTR m_bstr;
1086
1087 friend class Utf8Str; /* to access our raw_copy() */
1088};
1089
1090/* symmetric compare operators */
1091inline bool operator==(CBSTR l, const Bstr &r) { return r.operator==(l); }
1092inline bool operator!=(CBSTR l, const Bstr &r) { return r.operator!=(l); }
1093inline bool operator==(BSTR l, const Bstr &r) { return r.operator==(l); }
1094inline bool operator!=(BSTR l, const Bstr &r) { return r.operator!=(l); }
1095
1096
1097
1098
1099/**
1100 * String class used universally in Main for UTF-8 strings.
1101 *
1102 * This is based on RTCString, to which some functionality has been
1103 * moved. Here we keep things that are specific to Main, such as conversions
1104 * with UTF-16 strings (Bstr).
1105 *
1106 * Like RTCString, Utf8Str does not differentiate between NULL strings
1107 * and empty strings. In other words, Utf8Str("") and Utf8Str(NULL) behave the
1108 * same. In both cases, RTCString allocates no memory, reports
1109 * a zero length and zero allocated bytes for both, and returns an empty
1110 * C string from c_str().
1111 *
1112 * @note All Utf8Str methods ASSUMES valid UTF-8 or UTF-16 input strings.
1113 * The VirtualBox policy in this regard is to validate strings coming
1114 * from external sources before passing them to Utf8Str or Bstr.
1115 */
1116class Utf8Str : public RTCString
1117{
1118public:
1119
1120 Utf8Str() {}
1121
1122 Utf8Str(const RTCString &that)
1123 : RTCString(that)
1124 {}
1125
1126 Utf8Str(const char *that)
1127 : RTCString(that)
1128 {}
1129
1130 Utf8Str(const Bstr &that)
1131 {
1132 copyFrom(that.raw());
1133 }
1134
1135 Utf8Str(CBSTR that, size_t a_cwcSize = RTSTR_MAX)
1136 {
1137 copyFrom(that, a_cwcSize);
1138 }
1139
1140 Utf8Str(const char *a_pszSrc, size_t a_cchSrc)
1141 : RTCString(a_pszSrc, a_cchSrc)
1142 {
1143 }
1144
1145 /**
1146 * Constructs a new string given the format string and the list of the
1147 * arguments for the format string.
1148 *
1149 * @param a_pszFormat Pointer to the format string (UTF-8),
1150 * @see pg_rt_str_format.
1151 * @param a_va Argument vector containing the arguments
1152 * specified by the format string.
1153 * @sa RTCString::printfV
1154 */
1155 Utf8Str(const char *a_pszFormat, va_list a_va) RT_IPRT_FORMAT_ATTR(1, 0)
1156 : RTCString(a_pszFormat, a_va)
1157 {
1158 }
1159
1160 Utf8Str& operator=(const RTCString &that)
1161 {
1162 RTCString::operator=(that);
1163 return *this;
1164 }
1165
1166 Utf8Str& operator=(const char *that)
1167 {
1168 RTCString::operator=(that);
1169 return *this;
1170 }
1171
1172 Utf8Str& operator=(const Bstr &that)
1173 {
1174 cleanup();
1175 copyFrom(that.raw());
1176 return *this;
1177 }
1178
1179 Utf8Str& operator=(CBSTR that)
1180 {
1181 cleanup();
1182 copyFrom(that);
1183 return *this;
1184 }
1185
1186 /**
1187 * Extended assignment method that returns a COM status code instead of an
1188 * exception on failure.
1189 *
1190 * @returns S_OK or E_OUTOFMEMORY.
1191 * @param a_rSrcStr The source string
1192 */
1193 HRESULT assignEx(Utf8Str const &a_rSrcStr)
1194 {
1195 return copyFromExNComRC(a_rSrcStr.m_psz, 0, a_rSrcStr.m_cch);
1196 }
1197
1198 /**
1199 * Extended assignment method that returns a COM status code instead of an
1200 * exception on failure.
1201 *
1202 * @returns S_OK, E_OUTOFMEMORY or E_INVALIDARG.
1203 * @param a_rSrcStr The source string
1204 * @param a_offSrc The character (byte) offset of the substring.
1205 * @param a_cchSrc The number of characters (bytes) to copy from the source
1206 * string.
1207 */
1208 HRESULT assignEx(Utf8Str const &a_rSrcStr, size_t a_offSrc, size_t a_cchSrc)
1209 {
1210 if ( a_offSrc + a_cchSrc > a_rSrcStr.m_cch
1211 || a_offSrc > a_rSrcStr.m_cch)
1212 return E_INVALIDARG;
1213 return copyFromExNComRC(a_rSrcStr.m_psz, a_offSrc, a_cchSrc);
1214 }
1215
1216 /**
1217 * Extended assignment method that returns a COM status code instead of an
1218 * exception on failure.
1219 *
1220 * @returns S_OK or E_OUTOFMEMORY.
1221 * @param a_pcszSrc The source string
1222 */
1223 HRESULT assignEx(const char *a_pcszSrc)
1224 {
1225 return copyFromExNComRC(a_pcszSrc, 0, a_pcszSrc ? strlen(a_pcszSrc) : 0);
1226 }
1227
1228 /**
1229 * Extended assignment method that returns a COM status code instead of an
1230 * exception on failure.
1231 *
1232 * @returns S_OK or E_OUTOFMEMORY.
1233 * @param a_pcszSrc The source string
1234 * @param a_cchSrc The number of characters (bytes) to copy from the source
1235 * string.
1236 */
1237 HRESULT assignEx(const char *a_pcszSrc, size_t a_cchSrc)
1238 {
1239 return copyFromExNComRC(a_pcszSrc, 0, a_cchSrc);
1240 }
1241
1242 RTMEMEF_NEW_AND_DELETE_OPERATORS();
1243
1244#if defined(VBOX_WITH_XPCOM)
1245 /**
1246 * Intended to assign instances to |char *| out parameters from within the
1247 * interface method. Transfers the ownership of the duplicated string to the
1248 * caller.
1249 *
1250 * This allocates a single 0 byte in the target if the member string is empty.
1251 *
1252 * This uses XPCOM memory allocation and thus only works on XPCOM. MSCOM doesn't
1253 * like char* strings anyway.
1254 */
1255 void cloneTo(char **pstr) const;
1256
1257 /**
1258 * A version of cloneTo that does not throw allocation errors but returns
1259 * E_OUTOFMEMORY instead.
1260 * @returns S_OK or E_OUTOFMEMORY (COM status codes).
1261 */
1262 HRESULT cloneToEx(char **pstr) const;
1263#endif
1264
1265 /**
1266 * Intended to assign instances to |BSTR| out parameters from within the
1267 * interface method. Transfers the ownership of the duplicated string to the
1268 * caller.
1269 */
1270 void cloneTo(BSTR *pstr) const
1271 {
1272 if (pstr)
1273 {
1274 Bstr bstr(*this);
1275 bstr.cloneTo(pstr);
1276 }
1277 }
1278
1279 /**
1280 * A version of cloneTo that does not throw allocation errors but returns
1281 * E_OUTOFMEMORY instead.
1282 *
1283 * @param pbstr Where to store a clone of the string.
1284 * @returns S_OK or E_OUTOFMEMORY (COM status codes).
1285 */
1286 HRESULT cloneToEx(BSTR *pbstr) const
1287 {
1288 if (!pbstr)
1289 return S_OK;
1290 Bstr bstr(*this);
1291 return bstr.detachToEx(pbstr);
1292 }
1293
1294 /**
1295 * Safe assignment from BSTR.
1296 *
1297 * @param pbstrSrc The source string.
1298 * @returns S_OK or E_OUTOFMEMORY (COM status codes).
1299 */
1300 HRESULT cloneEx(CBSTR pbstrSrc)
1301 {
1302 cleanup();
1303 return copyFromEx(pbstrSrc);
1304 }
1305
1306 /**
1307 * Removes a trailing slash from the member string, if present.
1308 * Calls RTPathStripTrailingSlash() without having to mess with mutableRaw().
1309 */
1310 Utf8Str& stripTrailingSlash();
1311
1312 /**
1313 * Removes a trailing filename from the member string, if present.
1314 * Calls RTPathStripFilename() without having to mess with mutableRaw().
1315 */
1316 Utf8Str& stripFilename();
1317
1318 /**
1319 * Removes the path component from the member string, if present.
1320 * Calls RTPathFilename() without having to mess with mutableRaw().
1321 */
1322 Utf8Str& stripPath();
1323
1324 /**
1325 * Removes a trailing file name suffix from the member string, if present.
1326 * Calls RTPathStripSuffix() without having to mess with mutableRaw().
1327 */
1328 Utf8Str& stripSuffix();
1329
1330 /**
1331 * Parses key=value pairs.
1332 *
1333 * @returns offset of the @a a_rPairSeparator following the returned value.
1334 * @retval npos is returned if there are no more key/value pairs.
1335 *
1336 * @param a_rKey Reference to variable that should receive
1337 * the key substring. This is set to null if
1338 * no key/value found. (It's also possible the
1339 * key is an empty string, so be careful.)
1340 * @param a_rValue Reference to variable that should receive
1341 * the value substring. This is set to null if
1342 * no key/value found. (It's also possible the
1343 * value is an empty string, so be careful.)
1344 * @param a_offStart The offset to start searching from. This is
1345 * typically 0 for the first call, and the
1346 * return value of the previous call for the
1347 * subsequent ones.
1348 * @param a_rPairSeparator The pair separator string. If this is an
1349 * empty string, the whole string will be
1350 * considered as a single key/value pair.
1351 * @param a_rKeyValueSeparator The key/value separator string.
1352 */
1353 size_t parseKeyValue(Utf8Str &a_rKey, Utf8Str &a_rValue, size_t a_offStart = 0,
1354 const Utf8Str &a_rPairSeparator = ",", const Utf8Str &a_rKeyValueSeparator = "=") const;
1355
1356 /**
1357 * Static immutable empty-string object. May be used for comparison purposes.
1358 */
1359 static const Utf8Str Empty;
1360protected:
1361
1362 void copyFrom(CBSTR a_pbstr, size_t a_cwcMax = RTSTR_MAX);
1363 HRESULT copyFromEx(CBSTR a_pbstr);
1364 HRESULT copyFromExNComRC(const char *a_pcszSrc, size_t a_offSrc, size_t a_cchSrc);
1365
1366 friend class Bstr; /* to access our raw_copy() */
1367};
1368
1369/**
1370 * Class with RTCString::printf as constructor for your convenience.
1371 *
1372 * Constructing a Utf8Str string object from a format string and a variable
1373 * number of arguments can easily be confused with the other Utf8Str
1374 * constructures, thus this child class.
1375 *
1376 * The usage of this class is like the following:
1377 * @code
1378 Utf8StrFmt strName("program name = %s", argv[0]);
1379 @endcode
1380 */
1381class Utf8StrFmt : public Utf8Str
1382{
1383public:
1384
1385 /**
1386 * Constructs a new string given the format string and the list of the
1387 * arguments for the format string.
1388 *
1389 * @param a_pszFormat Pointer to the format string (UTF-8),
1390 * @see pg_rt_str_format.
1391 * @param ... Ellipsis containing the arguments specified by
1392 * the format string.
1393 */
1394 explicit Utf8StrFmt(const char *a_pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2)
1395 {
1396 va_list va;
1397 va_start(va, a_pszFormat);
1398 printfV(a_pszFormat, va);
1399 va_end(va);
1400 }
1401
1402 RTMEMEF_NEW_AND_DELETE_OPERATORS();
1403
1404protected:
1405 Utf8StrFmt()
1406 { }
1407
1408private:
1409};
1410
1411/**
1412 * Class with Bstr::printf as constructor for your convenience.
1413 */
1414class BstrFmt : public Bstr
1415{
1416public:
1417
1418 /**
1419 * Constructs a new string given the format string and the list of the
1420 * arguments for the format string.
1421 *
1422 * @param a_pszFormat printf-like format string (in UTF-8 encoding), see
1423 * iprt/string.h for details.
1424 * @param ... List of the arguments for the format string.
1425 */
1426 explicit BstrFmt(const char *a_pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2)
1427 {
1428 va_list va;
1429 va_start(va, a_pszFormat);
1430 printfV(a_pszFormat, va);
1431 va_end(va);
1432 }
1433
1434 RTMEMEF_NEW_AND_DELETE_OPERATORS();
1435
1436protected:
1437 BstrFmt()
1438 { }
1439};
1440
1441/**
1442 * Class with Bstr::printfV as constructor for your convenience.
1443 */
1444class BstrFmtVA : public Bstr
1445{
1446public:
1447
1448 /**
1449 * Constructs a new string given the format string and the list of the
1450 * arguments for the format string.
1451 *
1452 * @param a_pszFormat printf-like format string (in UTF-8 encoding), see
1453 * iprt/string.h for details.
1454 * @param a_va List of arguments for the format string
1455 */
1456 BstrFmtVA(const char *a_pszFormat, va_list a_va) RT_IPRT_FORMAT_ATTR(1, 0)
1457 {
1458 printfV(a_pszFormat, a_va);
1459 }
1460
1461 RTMEMEF_NEW_AND_DELETE_OPERATORS();
1462
1463protected:
1464 BstrFmtVA()
1465 { }
1466};
1467
1468} /* namespace com */
1469
1470/** @} */
1471
1472#endif /* !VBOX_INCLUDED_com_string_h */
1473
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