1 | /* $Id: string.h 40875 2012-04-11 17:51:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MS COM / XPCOM Abstraction Layer - Smart string classes declaration.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2011 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * 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_com_string_h
|
---|
28 | #define ___VBox_com_string_h
|
---|
29 |
|
---|
30 | /* Make sure all the stdint.h macros are included - must come first! */
|
---|
31 | #ifndef __STDC_LIMIT_MACROS
|
---|
32 | # define __STDC_LIMIT_MACROS
|
---|
33 | #endif
|
---|
34 | #ifndef __STDC_CONSTANT_MACROS
|
---|
35 | # define __STDC_CONSTANT_MACROS
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #if defined(VBOX_WITH_XPCOM)
|
---|
39 | # include <nsMemory.h>
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include "VBox/com/defs.h"
|
---|
43 | #include "VBox/com/assert.h"
|
---|
44 |
|
---|
45 | #include <iprt/mem.h>
|
---|
46 | #include <iprt/cpp/ministring.h>
|
---|
47 |
|
---|
48 | namespace com
|
---|
49 | {
|
---|
50 |
|
---|
51 | class Utf8Str;
|
---|
52 |
|
---|
53 | // global constant in glue/string.cpp that represents an empty BSTR
|
---|
54 | extern const BSTR g_bstrEmpty;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * String class used universally in Main for COM-style Utf-16 strings.
|
---|
58 | *
|
---|
59 | * Unfortunately COM on Windows uses UTF-16 everywhere, requiring conversions
|
---|
60 | * back and forth since most of VirtualBox and our libraries use UTF-8.
|
---|
61 | *
|
---|
62 | * To make things more obscure, on Windows, a COM-style BSTR is not just a
|
---|
63 | * pointer to a null-terminated wide character array, but the four bytes (32
|
---|
64 | * bits) BEFORE the memory that the pointer points to are a length DWORD. One
|
---|
65 | * must therefore avoid pointer arithmetic and always use SysAllocString and
|
---|
66 | * the like to deal with BSTR pointers, which manage that DWORD correctly.
|
---|
67 | *
|
---|
68 | * For platforms other than Windows, we provide our own versions of the Sys*
|
---|
69 | * functions in Main/xpcom/helpers.cpp which do NOT use length prefixes though
|
---|
70 | * to be compatible with how XPCOM allocates string parameters to public
|
---|
71 | * functions.
|
---|
72 | *
|
---|
73 | * The Bstr class hides all this handling behind a std::string-like interface
|
---|
74 | * and also provides automatic conversions to RTCString and Utf8Str instances.
|
---|
75 | *
|
---|
76 | * The one advantage of using the SysString* routines is that this makes it
|
---|
77 | * possible to use it as a type of member variables of COM/XPCOM components and
|
---|
78 | * pass their values to callers through component methods' output parameters
|
---|
79 | * using the #cloneTo() operation. Also, the class can adopt (take ownership
|
---|
80 | * of) string buffers returned in output parameters of COM methods using the
|
---|
81 | * #asOutParam() operation and correctly free them afterwards.
|
---|
82 | *
|
---|
83 | * Starting with VirtualBox 3.2, like Utf8Str, Bstr no longer differentiates
|
---|
84 | * between NULL strings and empty strings. In other words, Bstr("") and
|
---|
85 | * Bstr(NULL) behave the same. In both cases, Bstr allocates no memory,
|
---|
86 | * reports a zero length and zero allocated bytes for both, and returns an
|
---|
87 | * empty C wide string from raw().
|
---|
88 | *
|
---|
89 | * @note All Bstr methods ASSUMES valid UTF-16 or UTF-8 input strings.
|
---|
90 | * The VirtualBox policy in this regard is to validate strings coming
|
---|
91 | * from external sources before passing them to Bstr or Utf8Str.
|
---|
92 | */
|
---|
93 | class Bstr
|
---|
94 | {
|
---|
95 | public:
|
---|
96 |
|
---|
97 | Bstr()
|
---|
98 | : m_bstr(NULL)
|
---|
99 | { }
|
---|
100 |
|
---|
101 | Bstr(const Bstr &that)
|
---|
102 | {
|
---|
103 | copyFrom((const OLECHAR *)that.m_bstr);
|
---|
104 | }
|
---|
105 |
|
---|
106 | Bstr(CBSTR that)
|
---|
107 | {
|
---|
108 | copyFrom((const OLECHAR *)that);
|
---|
109 | }
|
---|
110 |
|
---|
111 | #if defined(VBOX_WITH_XPCOM)
|
---|
112 | Bstr(const wchar_t *that)
|
---|
113 | {
|
---|
114 | AssertCompile(sizeof(wchar_t) == sizeof(OLECHAR));
|
---|
115 | copyFrom((const OLECHAR *)that);
|
---|
116 | }
|
---|
117 | #endif
|
---|
118 |
|
---|
119 | Bstr(const RTCString &that)
|
---|
120 | {
|
---|
121 | copyFrom(that.c_str());
|
---|
122 | }
|
---|
123 |
|
---|
124 | Bstr(const char *that)
|
---|
125 | {
|
---|
126 | copyFrom(that);
|
---|
127 | }
|
---|
128 |
|
---|
129 | Bstr(const char *a_pThat, size_t a_cchMax)
|
---|
130 | {
|
---|
131 | copyFromN(a_pThat, a_cchMax);
|
---|
132 | }
|
---|
133 |
|
---|
134 | ~Bstr()
|
---|
135 | {
|
---|
136 | setNull();
|
---|
137 | }
|
---|
138 |
|
---|
139 | Bstr& operator=(const Bstr &that)
|
---|
140 | {
|
---|
141 | cleanup();
|
---|
142 | copyFrom((const OLECHAR *)that.m_bstr);
|
---|
143 | return *this;
|
---|
144 | }
|
---|
145 |
|
---|
146 | Bstr& operator=(CBSTR that)
|
---|
147 | {
|
---|
148 | cleanup();
|
---|
149 | copyFrom((const OLECHAR *)that);
|
---|
150 | return *this;
|
---|
151 | }
|
---|
152 |
|
---|
153 | #if defined(VBOX_WITH_XPCOM)
|
---|
154 | Bstr& operator=(const wchar_t *that)
|
---|
155 | {
|
---|
156 | cleanup();
|
---|
157 | copyFrom((const OLECHAR *)that);
|
---|
158 | return *this;
|
---|
159 | }
|
---|
160 | #endif
|
---|
161 |
|
---|
162 | Bstr& setNull()
|
---|
163 | {
|
---|
164 | cleanup();
|
---|
165 | return *this;
|
---|
166 | }
|
---|
167 |
|
---|
168 | #ifdef _MSC_VER
|
---|
169 | # if _MSC_VER >= 1400
|
---|
170 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
171 | # endif
|
---|
172 | #else
|
---|
173 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
174 | #endif
|
---|
175 |
|
---|
176 | /** Case sensitivity selector. */
|
---|
177 | enum CaseSensitivity
|
---|
178 | {
|
---|
179 | CaseSensitive,
|
---|
180 | CaseInsensitive
|
---|
181 | };
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Compares the member string to str.
|
---|
185 | * @param str
|
---|
186 | * @param cs Whether comparison should be case-sensitive.
|
---|
187 | * @return
|
---|
188 | */
|
---|
189 | int compare(CBSTR str, CaseSensitivity cs = CaseSensitive) const
|
---|
190 | {
|
---|
191 | if (cs == CaseSensitive)
|
---|
192 | return ::RTUtf16Cmp((PRTUTF16)m_bstr, (PRTUTF16)str);
|
---|
193 | return ::RTUtf16LocaleICmp((PRTUTF16)m_bstr, (PRTUTF16)str);
|
---|
194 | }
|
---|
195 |
|
---|
196 | int compare(BSTR str, CaseSensitivity cs = CaseSensitive) const
|
---|
197 | {
|
---|
198 | return compare((CBSTR)str, cs);
|
---|
199 | }
|
---|
200 |
|
---|
201 | int compare(const Bstr &that, CaseSensitivity cs = CaseSensitive) const
|
---|
202 | {
|
---|
203 | return compare(that.m_bstr, cs);
|
---|
204 | }
|
---|
205 |
|
---|
206 | bool operator==(const Bstr &that) const { return !compare(that.m_bstr); }
|
---|
207 | bool operator!=(const Bstr &that) const { return !!compare(that.m_bstr); }
|
---|
208 | bool operator==(CBSTR that) const { return !compare(that); }
|
---|
209 | bool operator==(BSTR that) const { return !compare(that); }
|
---|
210 |
|
---|
211 | bool operator!=(CBSTR that) const { return !!compare(that); }
|
---|
212 | bool operator!=(BSTR that) const { return !!compare(that); }
|
---|
213 | bool operator<(const Bstr &that) const { return compare(that.m_bstr) < 0; }
|
---|
214 | bool operator<(CBSTR that) const { return compare(that) < 0; }
|
---|
215 | bool operator<(BSTR that) const { return compare(that) < 0; }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Returns true if the member string has no length.
|
---|
219 | * This is true for instances created from both NULL and "" input strings.
|
---|
220 | *
|
---|
221 | * @note Always use this method to check if an instance is empty. Do not
|
---|
222 | * use length() because that may need to run through the entire string
|
---|
223 | * (Bstr does not cache string lengths).
|
---|
224 | */
|
---|
225 | bool isEmpty() const { return m_bstr == NULL || *m_bstr == 0; }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Returns true if the member string has a length of one or more.
|
---|
229 | *
|
---|
230 | * @returns true if not empty, false if empty (NULL or "").
|
---|
231 | */
|
---|
232 | bool isNotEmpty() const { return m_bstr != NULL && *m_bstr != 0; }
|
---|
233 |
|
---|
234 | size_t length() const { return isEmpty() ? 0 : ::RTUtf16Len((PRTUTF16)m_bstr); }
|
---|
235 |
|
---|
236 | #if defined(VBOX_WITH_XPCOM)
|
---|
237 | /**
|
---|
238 | * Returns a pointer to the raw member UTF-16 string. If the member string is empty,
|
---|
239 | * returns a pointer to a global variable containing an empty BSTR with a proper zero
|
---|
240 | * length prefix so that Windows is happy.
|
---|
241 | */
|
---|
242 | CBSTR raw() const
|
---|
243 | {
|
---|
244 | if (m_bstr)
|
---|
245 | return m_bstr;
|
---|
246 |
|
---|
247 | return g_bstrEmpty;
|
---|
248 | }
|
---|
249 | #else
|
---|
250 | /**
|
---|
251 | * Windows-only hack, as the automatically generated headers use BSTR.
|
---|
252 | * So if we don't want to cast like crazy we have to be more loose than
|
---|
253 | * on XPCOM.
|
---|
254 | *
|
---|
255 | * Returns a pointer to the raw member UTF-16 string. If the member string is empty,
|
---|
256 | * returns a pointer to a global variable containing an empty BSTR with a proper zero
|
---|
257 | * length prefix so that Windows is happy.
|
---|
258 | */
|
---|
259 | BSTR raw() const
|
---|
260 | {
|
---|
261 | if (m_bstr)
|
---|
262 | return m_bstr;
|
---|
263 |
|
---|
264 | return g_bstrEmpty;
|
---|
265 | }
|
---|
266 | #endif
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Returns a non-const raw pointer that allows to modify the string directly.
|
---|
270 | * As opposed to raw(), this DOES return NULL if the member string is empty
|
---|
271 | * because we cannot return a mutable pointer to the global variable with the
|
---|
272 | * empty string.
|
---|
273 | *
|
---|
274 | * @warning
|
---|
275 | * Be sure not to modify data beyond the allocated memory! The
|
---|
276 | * guaranteed size of the allocated memory is at least #length()
|
---|
277 | * bytes after creation and after every assignment operation.
|
---|
278 | */
|
---|
279 | BSTR mutableRaw() { return m_bstr; }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Intended to assign copies of instances to |BSTR| out parameters from
|
---|
283 | * within the interface method. Transfers the ownership of the duplicated
|
---|
284 | * string to the caller.
|
---|
285 | *
|
---|
286 | * If the member string is empty, this allocates an empty BSTR in *pstr
|
---|
287 | * (i.e. makes it point to a new buffer with a null byte).
|
---|
288 | *
|
---|
289 | * @deprecated Use cloneToEx instead to avoid throwing exceptions.
|
---|
290 | */
|
---|
291 | void cloneTo(BSTR *pstr) const
|
---|
292 | {
|
---|
293 | if (pstr)
|
---|
294 | {
|
---|
295 | *pstr = ::SysAllocString((const OLECHAR *)raw()); // raw() returns a pointer to "" if empty
|
---|
296 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
297 | if (!*pstr)
|
---|
298 | throw std::bad_alloc();
|
---|
299 | #endif
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * A version of cloneTo that does not throw any out of memory exceptions, but
|
---|
305 | * returns E_OUTOFMEMORY intead.
|
---|
306 | * @returns S_OK or E_OUTOFMEMORY.
|
---|
307 | */
|
---|
308 | HRESULT cloneToEx(BSTR *pstr) const
|
---|
309 | {
|
---|
310 | if (!pstr)
|
---|
311 | return S_OK;
|
---|
312 | *pstr = ::SysAllocString((const OLECHAR *)raw()); // raw() returns a pointer to "" if empty
|
---|
313 | return pstr ? S_OK : E_OUTOFMEMORY;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Intended to assign instances to |BSTR| out parameters from within the
|
---|
318 | * interface method. Transfers the ownership of the original string to the
|
---|
319 | * caller and resets the instance to null.
|
---|
320 | *
|
---|
321 | * As opposed to cloneTo(), this method doesn't create a copy of the
|
---|
322 | * string.
|
---|
323 | *
|
---|
324 | * If the member string is empty, this allocates an empty BSTR in *pstr
|
---|
325 | * (i.e. makes it point to a new buffer with a null byte).
|
---|
326 | *
|
---|
327 | * @param pbstrDst The BSTR variable to detach the string to.
|
---|
328 | *
|
---|
329 | * @throws std::bad_alloc if we failed to allocate a new empty string.
|
---|
330 | */
|
---|
331 | void detachTo(BSTR *pbstrDst)
|
---|
332 | {
|
---|
333 | if (m_bstr)
|
---|
334 | {
|
---|
335 | *pbstrDst = m_bstr;
|
---|
336 | m_bstr = NULL;
|
---|
337 | }
|
---|
338 | else
|
---|
339 | {
|
---|
340 | // allocate null BSTR
|
---|
341 | *pbstrDst = ::SysAllocString((const OLECHAR *)g_bstrEmpty);
|
---|
342 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
343 | if (!*pbstrDst)
|
---|
344 | throw std::bad_alloc();
|
---|
345 | #endif
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * A version of detachTo that does not throw exceptions on out-of-memory
|
---|
351 | * conditions, but instead returns E_OUTOFMEMORY.
|
---|
352 | *
|
---|
353 | * @param pbstrDst The BSTR variable to detach the string to.
|
---|
354 | * @returns S_OK or E_OUTOFMEMORY.
|
---|
355 | */
|
---|
356 | HRESULT detachToEx(BSTR *pbstrDst)
|
---|
357 | {
|
---|
358 | if (m_bstr)
|
---|
359 | {
|
---|
360 | *pbstrDst = m_bstr;
|
---|
361 | m_bstr = NULL;
|
---|
362 | }
|
---|
363 | else
|
---|
364 | {
|
---|
365 | // allocate null BSTR
|
---|
366 | *pbstrDst = ::SysAllocString((const OLECHAR *)g_bstrEmpty);
|
---|
367 | if (!*pbstrDst)
|
---|
368 | return E_OUTOFMEMORY;
|
---|
369 | }
|
---|
370 | return S_OK;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Intended to pass instances as |BSTR| out parameters to methods.
|
---|
375 | * Takes the ownership of the returned data.
|
---|
376 | */
|
---|
377 | BSTR *asOutParam()
|
---|
378 | {
|
---|
379 | cleanup();
|
---|
380 | return &m_bstr;
|
---|
381 | }
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Static immutable empty-string object. May be used for comparison purposes.
|
---|
385 | */
|
---|
386 | static const Bstr Empty;
|
---|
387 |
|
---|
388 | protected:
|
---|
389 |
|
---|
390 | void cleanup()
|
---|
391 | {
|
---|
392 | if (m_bstr)
|
---|
393 | {
|
---|
394 | ::SysFreeString(m_bstr);
|
---|
395 | m_bstr = NULL;
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | /**
|
---|
400 | * Protected internal helper to copy a string. This ignores the previous object
|
---|
401 | * state, so either call this from a constructor or call cleanup() first.
|
---|
402 | *
|
---|
403 | * This variant copies from a zero-terminated UTF-16 string (which need not
|
---|
404 | * be a BSTR, i.e. need not have a length prefix).
|
---|
405 | *
|
---|
406 | * If the source is empty, this sets the member string to NULL.
|
---|
407 | *
|
---|
408 | * @param a_bstrSrc The source string. The caller guarantees
|
---|
409 | * that this is valid UTF-16.
|
---|
410 | *
|
---|
411 | * @throws std::bad_alloc - the object is representing an empty string.
|
---|
412 | */
|
---|
413 | void copyFrom(const OLECHAR *a_bstrSrc)
|
---|
414 | {
|
---|
415 | if (a_bstrSrc && *a_bstrSrc)
|
---|
416 | {
|
---|
417 | m_bstr = ::SysAllocString(a_bstrSrc);
|
---|
418 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
419 | if (!m_bstr)
|
---|
420 | throw std::bad_alloc();
|
---|
421 | #endif
|
---|
422 | }
|
---|
423 | else
|
---|
424 | m_bstr = NULL;
|
---|
425 | }
|
---|
426 |
|
---|
427 | /**
|
---|
428 | * Protected internal helper to copy a string. This ignores the previous object
|
---|
429 | * state, so either call this from a constructor or call cleanup() first.
|
---|
430 | *
|
---|
431 | * This variant copies and converts from a zero-terminated UTF-8 string.
|
---|
432 | *
|
---|
433 | * If the source is empty, this sets the member string to NULL.
|
---|
434 | *
|
---|
435 | * @param a_pszSrc The source string. The caller guarantees
|
---|
436 | * that this is valid UTF-8.
|
---|
437 | *
|
---|
438 | * @throws std::bad_alloc - the object is representing an empty string.
|
---|
439 | */
|
---|
440 | void copyFrom(const char *a_pszSrc)
|
---|
441 | {
|
---|
442 | copyFromN(a_pszSrc, RTSTR_MAX);
|
---|
443 | }
|
---|
444 |
|
---|
445 | /**
|
---|
446 | * Variant of copyFrom for sub-string constructors.
|
---|
447 | *
|
---|
448 | * @param a_pszSrc The source string. The caller guarantees
|
---|
449 | * that this is valid UTF-8.
|
---|
450 | * @param a_cchMax The maximum number of chars (not
|
---|
451 | * codepoints) to copy. If you pass RTSTR_MAX
|
---|
452 | * it'll be exactly like copyFrom().
|
---|
453 | *
|
---|
454 | * @throws std::bad_alloc - the object is representing an empty string.
|
---|
455 | */
|
---|
456 | void copyFromN(const char *a_pszSrc, size_t a_cchSrc);
|
---|
457 |
|
---|
458 | BSTR m_bstr;
|
---|
459 |
|
---|
460 | friend class Utf8Str; /* to access our raw_copy() */
|
---|
461 | };
|
---|
462 |
|
---|
463 | /* symmetric compare operators */
|
---|
464 | inline bool operator==(CBSTR l, const Bstr &r) { return r.operator==(l); }
|
---|
465 | inline bool operator!=(CBSTR l, const Bstr &r) { return r.operator!=(l); }
|
---|
466 | inline bool operator==(BSTR l, const Bstr &r) { return r.operator==(l); }
|
---|
467 | inline bool operator!=(BSTR l, const Bstr &r) { return r.operator!=(l); }
|
---|
468 |
|
---|
469 |
|
---|
470 |
|
---|
471 |
|
---|
472 | /**
|
---|
473 | * String class used universally in Main for UTF-8 strings.
|
---|
474 | *
|
---|
475 | * This is based on RTCString, to which some functionality has been
|
---|
476 | * moved. Here we keep things that are specific to Main, such as conversions
|
---|
477 | * with UTF-16 strings (Bstr).
|
---|
478 | *
|
---|
479 | * Like RTCString, Utf8Str does not differentiate between NULL strings
|
---|
480 | * and empty strings. In other words, Utf8Str("") and Utf8Str(NULL) behave the
|
---|
481 | * same. In both cases, RTCString allocates no memory, reports
|
---|
482 | * a zero length and zero allocated bytes for both, and returns an empty
|
---|
483 | * C string from c_str().
|
---|
484 | *
|
---|
485 | * @note All Utf8Str methods ASSUMES valid UTF-8 or UTF-16 input strings.
|
---|
486 | * The VirtualBox policy in this regard is to validate strings coming
|
---|
487 | * from external sources before passing them to Utf8Str or Bstr.
|
---|
488 | */
|
---|
489 | class Utf8Str : public RTCString
|
---|
490 | {
|
---|
491 | public:
|
---|
492 |
|
---|
493 | Utf8Str() {}
|
---|
494 |
|
---|
495 | Utf8Str(const RTCString &that)
|
---|
496 | : RTCString(that)
|
---|
497 | {}
|
---|
498 |
|
---|
499 | Utf8Str(const char *that)
|
---|
500 | : RTCString(that)
|
---|
501 | {}
|
---|
502 |
|
---|
503 | Utf8Str(const Bstr &that)
|
---|
504 | {
|
---|
505 | copyFrom(that.raw());
|
---|
506 | }
|
---|
507 |
|
---|
508 | Utf8Str(CBSTR that)
|
---|
509 | {
|
---|
510 | copyFrom(that);
|
---|
511 | }
|
---|
512 |
|
---|
513 | /**
|
---|
514 | * Constructs a new string given the format string and the list of the
|
---|
515 | * arguments for the format string.
|
---|
516 | *
|
---|
517 | * @param a_pszFormat Pointer to the format string (UTF-8),
|
---|
518 | * @see pg_rt_str_format.
|
---|
519 | * @param a_va Argument vector containing the arguments
|
---|
520 | * specified by the format string.
|
---|
521 | * @sa RTCString::printfV
|
---|
522 | */
|
---|
523 | Utf8Str(const char *a_pszFormat, va_list a_va)
|
---|
524 | : RTCString(a_pszFormat, a_va)
|
---|
525 | {
|
---|
526 | }
|
---|
527 |
|
---|
528 | Utf8Str& operator=(const RTCString &that)
|
---|
529 | {
|
---|
530 | RTCString::operator=(that);
|
---|
531 | return *this;
|
---|
532 | }
|
---|
533 |
|
---|
534 | Utf8Str& operator=(const char *that)
|
---|
535 | {
|
---|
536 | RTCString::operator=(that);
|
---|
537 | return *this;
|
---|
538 | }
|
---|
539 |
|
---|
540 | Utf8Str& operator=(const Bstr &that)
|
---|
541 | {
|
---|
542 | cleanup();
|
---|
543 | copyFrom(that.raw());
|
---|
544 | return *this;
|
---|
545 | }
|
---|
546 |
|
---|
547 | Utf8Str& operator=(CBSTR that)
|
---|
548 | {
|
---|
549 | cleanup();
|
---|
550 | copyFrom(that);
|
---|
551 | return *this;
|
---|
552 | }
|
---|
553 |
|
---|
554 | bool operator<(const RTCString &that) const { return RTCString::operator<(that); }
|
---|
555 |
|
---|
556 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
557 |
|
---|
558 | #if defined(VBOX_WITH_XPCOM)
|
---|
559 | /**
|
---|
560 | * Intended to assign instances to |char *| out parameters from within the
|
---|
561 | * interface method. Transfers the ownership of the duplicated string to the
|
---|
562 | * caller.
|
---|
563 | *
|
---|
564 | * This allocates a single 0 byte in the target if the member string is empty.
|
---|
565 | *
|
---|
566 | * This uses XPCOM memory allocation and thus only works on XPCOM. MSCOM doesn't
|
---|
567 | * like char* strings anyway.
|
---|
568 | */
|
---|
569 | void cloneTo(char **pstr) const;
|
---|
570 |
|
---|
571 | /**
|
---|
572 | * A version of cloneTo that does not throw allocation errors but returns
|
---|
573 | * E_OUTOFMEMORY instead.
|
---|
574 | * @returns S_OK or E_OUTOFMEMORY (COM status codes).
|
---|
575 | */
|
---|
576 | HRESULT cloneToEx(char **pstr) const;
|
---|
577 | #endif
|
---|
578 |
|
---|
579 | /**
|
---|
580 | * Intended to assign instances to |BSTR| out parameters from within the
|
---|
581 | * interface method. Transfers the ownership of the duplicated string to the
|
---|
582 | * caller.
|
---|
583 | */
|
---|
584 | void cloneTo(BSTR *pstr) const
|
---|
585 | {
|
---|
586 | if (pstr)
|
---|
587 | {
|
---|
588 | Bstr bstr(*this);
|
---|
589 | bstr.cloneTo(pstr);
|
---|
590 | }
|
---|
591 | }
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * A version of cloneTo that does not throw allocation errors but returns
|
---|
595 | * E_OUTOFMEMORY instead.
|
---|
596 | *
|
---|
597 | * @param pbstr Where to store a clone of the string.
|
---|
598 | * @returns S_OK or E_OUTOFMEMORY (COM status codes).
|
---|
599 | */
|
---|
600 | HRESULT cloneToEx(BSTR *pbstr) const
|
---|
601 | {
|
---|
602 | if (!pbstr)
|
---|
603 | return S_OK;
|
---|
604 | Bstr bstr(*this);
|
---|
605 | return bstr.detachToEx(pbstr);
|
---|
606 | }
|
---|
607 |
|
---|
608 | /**
|
---|
609 | * Safe assignment from BSTR.
|
---|
610 | *
|
---|
611 | * @param pbstrSrc The source string.
|
---|
612 | * @returns S_OK or E_OUTOFMEMORY (COM status codes).
|
---|
613 | */
|
---|
614 | HRESULT cloneEx(CBSTR pbstrSrc)
|
---|
615 | {
|
---|
616 | cleanup();
|
---|
617 | return copyFromEx(pbstrSrc);
|
---|
618 | }
|
---|
619 |
|
---|
620 | /**
|
---|
621 | * Removes a trailing slash from the member string, if present.
|
---|
622 | * Calls RTPathStripTrailingSlash() without having to mess with mutableRaw().
|
---|
623 | */
|
---|
624 | Utf8Str& stripTrailingSlash();
|
---|
625 |
|
---|
626 | /**
|
---|
627 | * Removes a trailing filename from the member string, if present.
|
---|
628 | * Calls RTPathStripFilename() without having to mess with mutableRaw().
|
---|
629 | */
|
---|
630 | Utf8Str& stripFilename();
|
---|
631 |
|
---|
632 | /**
|
---|
633 | * Removes the path component from the member string, if present.
|
---|
634 | * Calls RTPathFilename() without having to mess with mutableRaw().
|
---|
635 | */
|
---|
636 | Utf8Str& stripPath();
|
---|
637 |
|
---|
638 | /**
|
---|
639 | * Removes a trailing file name extension from the member string, if present.
|
---|
640 | * Calls RTPathStripExt() without having to mess with mutableRaw().
|
---|
641 | */
|
---|
642 | Utf8Str& stripExt();
|
---|
643 |
|
---|
644 | /**
|
---|
645 | * Static immutable empty-string object. May be used for comparison purposes.
|
---|
646 | */
|
---|
647 | static const Utf8Str Empty;
|
---|
648 | protected:
|
---|
649 |
|
---|
650 | void copyFrom(CBSTR a_pbstr);
|
---|
651 | HRESULT copyFromEx(CBSTR a_pbstr);
|
---|
652 |
|
---|
653 | friend class Bstr; /* to access our raw_copy() */
|
---|
654 | };
|
---|
655 |
|
---|
656 | /**
|
---|
657 | * Class with RTCString::printf as constructor for your convenience.
|
---|
658 | *
|
---|
659 | * Constructing a Utf8Str string object from a format string and a variable
|
---|
660 | * number of arguments can easily be confused with the other Utf8Str
|
---|
661 | * constructures, thus this child class.
|
---|
662 | *
|
---|
663 | * The usage of this class is like the following:
|
---|
664 | * @code
|
---|
665 | Utf8StrFmt strName("program name = %s", argv[0]);
|
---|
666 | @endcode
|
---|
667 | */
|
---|
668 | class Utf8StrFmt : public Utf8Str
|
---|
669 | {
|
---|
670 | public:
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * Constructs a new string given the format string and the list of the
|
---|
674 | * arguments for the format string.
|
---|
675 | *
|
---|
676 | * @param a_pszFormat Pointer to the format string (UTF-8),
|
---|
677 | * @see pg_rt_str_format.
|
---|
678 | * @param ... Ellipsis containing the arguments specified by
|
---|
679 | * the format string.
|
---|
680 | */
|
---|
681 | explicit Utf8StrFmt(const char *a_pszFormat, ...)
|
---|
682 | {
|
---|
683 | va_list va;
|
---|
684 | va_start(va, a_pszFormat);
|
---|
685 | printfV(a_pszFormat, va);
|
---|
686 | va_end(va);
|
---|
687 | }
|
---|
688 |
|
---|
689 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
690 |
|
---|
691 | protected:
|
---|
692 | Utf8StrFmt()
|
---|
693 | { }
|
---|
694 |
|
---|
695 | private:
|
---|
696 | };
|
---|
697 |
|
---|
698 | /**
|
---|
699 | * The BstrFmt class is a shortcut to <tt>Bstr(Utf8StrFmt(...))</tt>.
|
---|
700 | */
|
---|
701 | class BstrFmt : public Bstr
|
---|
702 | {
|
---|
703 | public:
|
---|
704 |
|
---|
705 | /**
|
---|
706 | * Constructs a new string given the format string and the list of the
|
---|
707 | * arguments for the format string.
|
---|
708 | *
|
---|
709 | * @param aFormat printf-like format string (in UTF-8 encoding).
|
---|
710 | * @param ... List of the arguments for the format string.
|
---|
711 | */
|
---|
712 | explicit BstrFmt(const char *aFormat, ...)
|
---|
713 | {
|
---|
714 | va_list args;
|
---|
715 | va_start(args, aFormat);
|
---|
716 | copyFrom(Utf8Str(aFormat, args).c_str());
|
---|
717 | va_end(args);
|
---|
718 | }
|
---|
719 |
|
---|
720 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
721 | };
|
---|
722 |
|
---|
723 | /**
|
---|
724 | * The BstrFmtVA class is a shortcut to <tt>Bstr(Utf8Str(format,va))</tt>.
|
---|
725 | */
|
---|
726 | class BstrFmtVA : public Bstr
|
---|
727 | {
|
---|
728 | public:
|
---|
729 |
|
---|
730 | /**
|
---|
731 | * Constructs a new string given the format string and the list of the
|
---|
732 | * arguments for the format string.
|
---|
733 | *
|
---|
734 | * @param aFormat printf-like format string (in UTF-8 encoding).
|
---|
735 | * @param aArgs List of arguments for the format string
|
---|
736 | */
|
---|
737 | BstrFmtVA(const char *aFormat, va_list aArgs)
|
---|
738 | {
|
---|
739 | copyFrom(Utf8Str(aFormat, aArgs).c_str());
|
---|
740 | }
|
---|
741 |
|
---|
742 | RTMEMEF_NEW_AND_DELETE_OPERATORS();
|
---|
743 | };
|
---|
744 |
|
---|
745 | } /* namespace com */
|
---|
746 |
|
---|
747 | #endif /* !___VBox_com_string_h */
|
---|
748 |
|
---|