VirtualBox

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

Last change on this file since 26600 was 26186, checked in by vboxsync, 15 years ago

Main: coding style fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line 
1/* $Id: string.cpp 26186 2010-02-03 13:07:12Z vboxsync $ */
2
3/** @file
4 *
5 * MS COM / XPCOM Abstraction Layer:
6 * Smart string classes definition
7 */
8
9/*
10 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
21 * Clara, CA 95054 USA or visit http://www.sun.com if you need
22 * additional information or have any questions.
23 */
24
25#include "VBox/com/string.h"
26
27#include <iprt/err.h>
28#include <iprt/path.h>
29
30namespace com
31{
32
33/* static */
34const Bstr Bstr::Null; /* default ctor is OK */
35
36/* static */
37const Utf8Str Utf8Str::Null; /* default ctor is OK */
38
39Utf8Str& Utf8Str::toLower()
40{
41 if (length())
42 ::RTStrToLower(m_psz);
43 return *this;
44}
45
46Utf8Str& Utf8Str::toUpper()
47{
48 if (length())
49 ::RTStrToUpper(m_psz);
50 return *this;
51}
52
53void Utf8Str::stripTrailingSlash()
54{
55 RTPathStripTrailingSlash(m_psz);
56 jolt();
57}
58
59void Utf8Str::stripFilename()
60{
61 RTPathStripFilename(m_psz);
62 jolt();
63}
64
65void Utf8Str::stripExt()
66{
67 RTPathStripExt(m_psz);
68 jolt();
69}
70
71struct FormatData
72{
73 static const size_t CacheIncrement = 256;
74 size_t size;
75 size_t pos;
76 char *cache;
77};
78
79void Utf8StrFmt::init(const char *format, va_list args)
80{
81 if (!format)
82 return;
83
84 // assume an extra byte for a terminating zero
85 size_t fmtlen = strlen(format) + 1;
86
87 FormatData data;
88 data.size = FormatData::CacheIncrement;
89 if (fmtlen >= FormatData::CacheIncrement)
90 data.size += fmtlen;
91 data.pos = 0;
92 data.cache = (char*)::RTMemTmpAllocZ(data.size);
93
94 size_t n = ::RTStrFormatV(strOutput, &data, NULL, NULL, format, args);
95
96 AssertMsg(n == data.pos,
97 ("The number of bytes formatted doesn't match: %d and %d!", n, data.pos));
98 NOREF(n);
99
100 // finalize formatting
101 data.cache[data.pos] = 0;
102 (*static_cast<Utf8Str*>(this)) = data.cache;
103 ::RTMemTmpFree(data.cache);
104}
105
106// static
107DECLCALLBACK(size_t) Utf8StrFmt::strOutput(void *pvArg, const char *pachChars,
108 size_t cbChars)
109{
110 Assert(pvArg);
111 FormatData &data = *(FormatData *) pvArg;
112
113 if (!(pachChars == NULL && cbChars == 0))
114 {
115 Assert(pachChars);
116
117 // append to cache (always assume an extra byte for a terminating zero)
118 size_t needed = cbChars + 1;
119 if (data.pos + needed > data.size)
120 {
121 data.size += FormatData::CacheIncrement;
122 if (needed >= FormatData::CacheIncrement)
123 data.size += needed;
124 data.cache = (char*)::RTMemRealloc(data.cache, data.size);
125 }
126 strncpy(data.cache + data.pos, pachChars, cbChars);
127 data.pos += cbChars;
128 }
129
130 return cbChars;
131}
132
133
134} /* 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