VirtualBox

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

Last change on this file since 28851 was 28800, checked in by vboxsync, 14 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
Line 
1/* $Id: string.cpp 28800 2010-04-27 08:22:32Z 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::Null; /* default ctor is OK */
37
38/* static */
39const Utf8Str Utf8Str::Null; /* 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
84struct FormatData
85{
86 static const size_t CacheIncrement = 256;
87 size_t size;
88 size_t pos;
89 char *cache;
90};
91
92void Utf8StrFmt::init(const char *format, va_list args)
93{
94 if (!format)
95 return;
96
97 // assume an extra byte for a terminating zero
98 size_t fmtlen = strlen(format) + 1;
99
100 FormatData data;
101 data.size = FormatData::CacheIncrement;
102 if (fmtlen >= FormatData::CacheIncrement)
103 data.size += fmtlen;
104 data.pos = 0;
105 data.cache = (char*)::RTMemTmpAllocZ(data.size);
106
107 size_t n = ::RTStrFormatV(strOutput, &data, NULL, NULL, format, args);
108
109 AssertMsg(n == data.pos,
110 ("The number of bytes formatted doesn't match: %d and %d!", n, data.pos));
111 NOREF(n);
112
113 // finalize formatting
114 data.cache[data.pos] = 0;
115 (*static_cast<Utf8Str*>(this)) = data.cache;
116 ::RTMemTmpFree(data.cache);
117}
118
119// static
120DECLCALLBACK(size_t) Utf8StrFmt::strOutput(void *pvArg, const char *pachChars,
121 size_t cbChars)
122{
123 Assert(pvArg);
124 FormatData &data = *(FormatData *) pvArg;
125
126 if (!(pachChars == NULL && cbChars == 0))
127 {
128 Assert(pachChars);
129
130 // append to cache (always assume an extra byte for a terminating zero)
131 size_t needed = cbChars + 1;
132 if (data.pos + needed > data.size)
133 {
134 data.size += FormatData::CacheIncrement;
135 if (needed >= FormatData::CacheIncrement)
136 data.size += needed;
137 data.cache = (char*)::RTMemRealloc(data.cache, data.size);
138 }
139 strncpy(data.cache + data.pos, pachChars, cbChars);
140 data.pos += cbChars;
141 }
142
143 return cbChars;
144}
145
146
147} /* 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