VirtualBox

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

Last change on this file since 16649 was 16495, checked in by vboxsync, 16 years ago

OVF: VBoxManage import implementation (to be continued), back-end fixes, documentation updates

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1/* $Id: string.cpp 16495 2009-02-03 21:20:36Z 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
29namespace com
30{
31
32/* static */
33const Bstr Bstr::Null; /* default ctor is OK */
34
35/* static */
36const Utf8Str Utf8Str::Null; /* default ctor is OK */
37
38const size_t Utf8Str::npos = (size_t)-1;
39
40Utf8Str Utf8Str::substr(size_t pos /*= 0*/, size_t n /*= npos*/) const
41{
42 Utf8Str ret;
43
44 if (n)
45 {
46 const char *psz;
47
48 if ((psz = c_str()))
49 {
50 RTUNICP cp;
51
52 // walk the UTF-8 characters until where the caller wants to start
53 size_t i = pos;
54 while (*psz && i--)
55 if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
56 return ret; // return empty string on bad encoding
57
58 const char *pFirst = psz;
59
60 if (n == npos)
61 // all the rest:
62 ret = pFirst;
63 else
64 {
65 i = n;
66 while (*psz && i--)
67 if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
68 return ret; // return empty string on bad encoding
69
70 size_t cbCopy = psz - pFirst;
71 ret.alloc(cbCopy + 1);
72 memcpy(ret.str, pFirst, cbCopy);
73 ret.str[cbCopy] = '\0';
74 }
75 }
76 }
77
78 return ret;
79}
80
81int Utf8Str::toInt(uint64_t &i) const
82{
83 return RTStrToUInt64Ex(str, NULL, 0, &i);
84}
85
86int Utf8Str::toInt(uint32_t &i) const
87{
88 return RTStrToUInt32Ex(str, NULL, 0, &i);
89}
90
91struct FormatData
92{
93 static const size_t CacheIncrement = 256;
94 size_t size;
95 size_t pos;
96 char *cache;
97};
98
99void Utf8StrFmt::init (const char *format, va_list args)
100{
101 if (!format)
102 return;
103
104 // assume an extra byte for a terminating zero
105 size_t fmtlen = strlen (format) + 1;
106
107 FormatData data;
108 data.size = FormatData::CacheIncrement;
109 if (fmtlen >= FormatData::CacheIncrement)
110 data.size += fmtlen;
111 data.pos = 0;
112 data.cache = (char *) ::RTMemTmpAllocZ (data.size);
113
114 size_t n = ::RTStrFormatV (strOutput, &data, NULL, NULL, format, args);
115
116 AssertMsg (n == data.pos,
117 ("The number of bytes formatted doesn't match: %d and %d!",
118 n, data.pos));
119 NOREF (n);
120
121 // finalize formatting
122 data.cache [data.pos] = 0;
123 (*static_cast <Utf8Str *> (this)) = data.cache;
124 ::RTMemTmpFree (data.cache);
125}
126
127// static
128DECLCALLBACK(size_t) Utf8StrFmt::strOutput (void *pvArg, const char *pachChars,
129 size_t cbChars)
130{
131 Assert (pvArg);
132 FormatData &data = *(FormatData *) pvArg;
133
134 if (!(pachChars == NULL && cbChars == 0))
135 {
136 Assert (pachChars);
137
138 // append to cache (always assume an extra byte for a terminating zero)
139 size_t needed = cbChars + 1;
140 if (data.pos + needed > data.size)
141 {
142 data.size += FormatData::CacheIncrement;
143 if (needed >= FormatData::CacheIncrement)
144 data.size += needed;
145 data.cache = (char *) ::RTMemRealloc (data.cache, data.size);
146 }
147 strncpy (data.cache + data.pos, pachChars, cbChars);
148 data.pos += cbChars;
149 }
150
151 return cbChars;
152}
153
154
155} /* 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