VirtualBox

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

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

Utf8Str::substr error handling

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1/* $Id: string.cpp 16369 2009-01-29 14:26:18Z 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.215389.xyz. 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_SUCCESS(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_SUCCESS(RTStrGetCpEx(&psz, &cp))))
68 return ret; // return empty string on bad encoding
69
70 size_t len = psz - pFirst;
71 char *psz = (char*)RTMemAlloc(len + 1);
72 memcpy(psz, pFirst, len);
73 psz[len] = '\0';
74 ret = psz;
75 RTMemFree(psz);
76 }
77 }
78 }
79
80 return ret;
81}
82
83struct FormatData
84{
85 static const size_t CacheIncrement = 256;
86 size_t size;
87 size_t pos;
88 char *cache;
89};
90
91void Utf8StrFmt::init (const char *format, va_list args)
92{
93 if (!format)
94 return;
95
96 // assume an extra byte for a terminating zero
97 size_t fmtlen = strlen (format) + 1;
98
99 FormatData data;
100 data.size = FormatData::CacheIncrement;
101 if (fmtlen >= FormatData::CacheIncrement)
102 data.size += fmtlen;
103 data.pos = 0;
104 data.cache = (char *) ::RTMemTmpAllocZ (data.size);
105
106 size_t n = ::RTStrFormatV (strOutput, &data, NULL, NULL, format, args);
107
108 AssertMsg (n == data.pos,
109 ("The number of bytes formatted doesn't match: %d and %d!",
110 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.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette