VirtualBox

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

Last change on this file since 31751 was 31281, checked in by vboxsync, 14 years ago

Main: one function instead of two for finding DVD and floppy images

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/* $Id: string.cpp 31281 2010-08-02 10:35:19Z vboxsync $ */
2
3/** @file
4 *
5 * MS COM / XPCOM Abstraction Layer:
6 * UTF-8 and UTF-16 string classes
7 */
8
9/*
10 * Copyright (C) 2006-2007 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 */
20
21#include "VBox/com/string.h"
22
23#include <iprt/err.h>
24#include <iprt/path.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/* static */
39const Utf8Str Utf8Str::Empty; /* default ctor is OK */
40
41#if defined (VBOX_WITH_XPCOM)
42void Utf8Str::cloneTo(char **pstr) const
43{
44 size_t cb = length() + 1;
45 *pstr = (char*)nsMemory::Alloc(cb);
46 if (!*pstr)
47 throw std::bad_alloc();
48 memcpy(*pstr, c_str(), cb);
49}
50#endif
51
52Utf8Str& Utf8Str::toLower()
53{
54 if (length())
55 ::RTStrToLower(m_psz);
56 return *this;
57}
58
59Utf8Str& Utf8Str::toUpper()
60{
61 if (length())
62 ::RTStrToUpper(m_psz);
63 return *this;
64}
65
66void Utf8Str::stripTrailingSlash()
67{
68 RTPathStripTrailingSlash(m_psz);
69 jolt();
70}
71
72void Utf8Str::stripFilename()
73{
74 RTPathStripFilename(m_psz);
75 jolt();
76}
77
78void Utf8Str::stripExt()
79{
80 RTPathStripExt(m_psz);
81 jolt();
82}
83
84/**
85 * Internal function used in Utf8Str copy constructors and assignment when
86 * copying from a UTF-16 string.
87 *
88 * As with the iprt::ministring::copyFrom() variants, this unconditionally
89 * sets the members to a copy of the given other strings and makes
90 * no assumptions about previous contents. This can therefore be used
91 * both in copy constructors, when member variables have no defined
92 * value, and in assignments after having called cleanup().
93 *
94 * This variant converts from a UTF-16 string, most probably from
95 * a Bstr assignment.
96 *
97 * @param s
98 */
99void Utf8Str::copyFrom(CBSTR s)
100{
101 if (s && *s)
102 {
103 int vrc = RTUtf16ToUtf8Ex((PRTUTF16)s, // PCRTUTF16 pwszString
104 RTSTR_MAX, // size_t cwcString: translate entire string
105 &m_psz, // char **ppsz: output buffer
106 0, // size_t cch: if 0, func allocates buffer in *ppsz
107 &m_cbLength); // size_t *pcch: receives the size of the output string, excluding the terminator.
108 if (RT_FAILURE(vrc))
109 {
110 if ( vrc == VERR_NO_STR_MEMORY
111 || vrc == VERR_NO_MEMORY
112 )
113 throw std::bad_alloc();
114
115 // @todo what do we do with bad input strings? throw also? for now just keep an empty string
116 m_cbLength = 0;
117 m_cbAllocated = 0;
118 m_psz = NULL;
119 }
120 else
121 m_cbAllocated = m_cbLength + 1;
122 }
123 else
124 {
125 m_cbLength = 0;
126 m_cbAllocated = 0;
127 m_psz = NULL;
128 }
129}
130
131void Utf8StrFmt::init(const char *format, va_list args)
132{
133 if (!format || !*format)
134 {
135 m_cbLength = 0;
136 m_cbAllocated = 0;
137 m_psz = NULL;
138 }
139 else
140 {
141 m_cbLength = RTStrAPrintfV(&m_psz, format, args);
142 m_cbAllocated = m_cbLength + 1;
143 }
144}
145
146} /* 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