VirtualBox

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

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

COM: teach Utf8Str substr() like std::string

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
1/* $Id: string.cpp 16324 2009-01-28 18:06:59Z 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 = c_str();
47 RTUNICP cp;
48
49 // walk the UTF-8 characters until where the caller wants to start
50 size_t i = pos;
51 while (i--)
52 RTStrGetCpEx(&psz, &cp);
53
54 const char *pFirst = psz;
55
56 if (n == npos)
57 // all the rest:
58 ret = pFirst;
59 else
60 {
61 i = n;
62 while (i--)
63 RTStrGetCpEx(&psz, &cp);
64
65 size_t len = psz - pFirst;
66 char *psz = (char*)RTMemAlloc(len + 1);
67 memcpy(psz, pFirst, len);
68 psz[len] = '\0';
69 ret = psz;
70 RTMemFree(psz);
71 }
72 }
73
74 return ret;
75}
76
77struct FormatData
78{
79 static const size_t CacheIncrement = 256;
80 size_t size;
81 size_t pos;
82 char *cache;
83};
84
85void Utf8StrFmt::init (const char *format, va_list args)
86{
87 if (!format)
88 return;
89
90 // assume an extra byte for a terminating zero
91 size_t fmtlen = strlen (format) + 1;
92
93 FormatData data;
94 data.size = FormatData::CacheIncrement;
95 if (fmtlen >= FormatData::CacheIncrement)
96 data.size += fmtlen;
97 data.pos = 0;
98 data.cache = (char *) ::RTMemTmpAllocZ (data.size);
99
100 size_t n = ::RTStrFormatV (strOutput, &data, NULL, NULL, format, args);
101
102 AssertMsg (n == data.pos,
103 ("The number of bytes formatted doesn't match: %d and %d!",
104 n, data.pos));
105 NOREF (n);
106
107 // finalize formatting
108 data.cache [data.pos] = 0;
109 (*static_cast <Utf8Str *> (this)) = data.cache;
110 ::RTMemTmpFree (data.cache);
111}
112
113// static
114DECLCALLBACK(size_t) Utf8StrFmt::strOutput (void *pvArg, const char *pachChars,
115 size_t cbChars)
116{
117 Assert (pvArg);
118 FormatData &data = *(FormatData *) pvArg;
119
120 if (!(pachChars == NULL && cbChars == 0))
121 {
122 Assert (pachChars);
123
124 // append to cache (always assume an extra byte for a terminating zero)
125 size_t needed = cbChars + 1;
126 if (data.pos + needed > data.size)
127 {
128 data.size += FormatData::CacheIncrement;
129 if (needed >= FormatData::CacheIncrement)
130 data.size += needed;
131 data.cache = (char *) ::RTMemRealloc (data.cache, data.size);
132 }
133 strncpy (data.cache + data.pos, pachChars, cbChars);
134 data.pos += cbChars;
135 }
136
137 return cbChars;
138}
139
140
141} /* 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