VirtualBox

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

Last change on this file since 22875 was 21588, checked in by vboxsync, 15 years ago

Main: move vbox-independent OVF reader code to separate file; move some more string implementation from com::UTF8Str to iprt::MiniString

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1/* $Id: string.cpp 21588 2009-07-14 16:22:06Z 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!",
98 n, data.pos));
99 NOREF (n);
100
101 // finalize formatting
102 data.cache [data.pos] = 0;
103 (*static_cast <Utf8Str *> (this)) = data.cache;
104 ::RTMemTmpFree (data.cache);
105}
106
107// static
108DECLCALLBACK(size_t) Utf8StrFmt::strOutput (void *pvArg, const char *pachChars,
109 size_t cbChars)
110{
111 Assert (pvArg);
112 FormatData &data = *(FormatData *) pvArg;
113
114 if (!(pachChars == NULL && cbChars == 0))
115 {
116 Assert (pachChars);
117
118 // append to cache (always assume an extra byte for a terminating zero)
119 size_t needed = cbChars + 1;
120 if (data.pos + needed > data.size)
121 {
122 data.size += FormatData::CacheIncrement;
123 if (needed >= FormatData::CacheIncrement)
124 data.size += needed;
125 data.cache = (char *) ::RTMemRealloc (data.cache, data.size);
126 }
127 strncpy (data.cache + data.pos, pachChars, cbChars);
128 data.pos += cbChars;
129 }
130
131 return cbChars;
132}
133
134
135} /* 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