VirtualBox

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

Last change on this file since 11162 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.7 KB
Line 
1/* $Id: string.cpp 8155 2008-04-18 15:16:47Z 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
38struct FormatData
39{
40 static const size_t CacheIncrement = 256;
41 size_t size;
42 size_t pos;
43 char *cache;
44};
45
46void Utf8StrFmt::init (const char *format, va_list args)
47{
48 if (!format)
49 return;
50
51 // assume an extra byte for a terminating zero
52 size_t fmtlen = strlen (format) + 1;
53
54 FormatData data;
55 data.size = FormatData::CacheIncrement;
56 if (fmtlen >= FormatData::CacheIncrement)
57 data.size += fmtlen;
58 data.pos = 0;
59 data.cache = (char *) ::RTMemTmpAllocZ (data.size);
60
61 size_t n = ::RTStrFormatV (strOutput, &data, NULL, NULL, format, args);
62
63 AssertMsg (n == data.pos,
64 ("The number of bytes formatted doesn't match: %d and %d!",
65 n, data.pos));
66 NOREF (n);
67
68 // finalize formatting
69 data.cache [data.pos] = 0;
70 (*static_cast <Utf8Str *> (this)) = data.cache;
71 ::RTMemTmpFree (data.cache);
72}
73
74// static
75DECLCALLBACK(size_t) Utf8StrFmt::strOutput (void *pvArg, const char *pachChars,
76 size_t cbChars)
77{
78 Assert (pvArg);
79 FormatData &data = *(FormatData *) pvArg;
80
81 if (!(pachChars == NULL && cbChars == 0))
82 {
83 Assert (pachChars);
84
85 // append to cache (always assume an extra byte for a terminating zero)
86 size_t needed = cbChars + 1;
87 if (data.pos + needed > data.size)
88 {
89 data.size += FormatData::CacheIncrement;
90 if (needed >= FormatData::CacheIncrement)
91 data.size += needed;
92 data.cache = (char *) ::RTMemRealloc (data.cache, data.size);
93 }
94 strncpy (data.cache + data.pos, pachChars, cbChars);
95 data.pos += cbChars;
96 }
97
98 return cbChars;
99}
100
101} /* 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