VirtualBox

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

Last change on this file since 7430 was 6076, checked in by vboxsync, 17 years ago

Merged dmik/s2 branch (r25959:26751) to the trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.5 KB
Line 
1/* $Id: string.cpp 6076 2007-12-14 19:23:03Z vboxsync $ */
2
3/** @file
4 *
5 * MS COM / XPCOM Abstraction Layer:
6 * Smart string classes definition
7 */
8
9/*
10 * Copyright (C) 2006-2007 innotek GmbH
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
21#include "VBox/com/string.h"
22
23#include <iprt/err.h>
24
25namespace com
26{
27
28/* static */
29const Bstr Bstr::Null; /* default ctor is OK */
30
31/* static */
32const Utf8Str Utf8Str::Null; /* default ctor is OK */
33
34struct FormatData
35{
36 static const size_t CacheIncrement = 256;
37 size_t size;
38 size_t pos;
39 char *cache;
40};
41
42void Utf8StrFmt::init (const char *format, va_list args)
43{
44 if (!format)
45 return;
46
47 // assume an extra byte for a terminating zero
48 size_t fmtlen = strlen (format) + 1;
49
50 FormatData data;
51 data.size = FormatData::CacheIncrement;
52 if (fmtlen >= FormatData::CacheIncrement)
53 data.size += fmtlen;
54 data.pos = 0;
55 data.cache = (char *) ::RTMemTmpAllocZ (data.size);
56
57 size_t n = ::RTStrFormatV (strOutput, &data, NULL, NULL, format, args);
58
59 AssertMsg (n == data.pos,
60 ("The number of bytes formatted doesn't match: %d and %d!",
61 n, data.pos));
62 NOREF (n);
63
64 // finalize formatting
65 data.cache [data.pos] = 0;
66 (*static_cast <Utf8Str *> (this)) = data.cache;
67 ::RTMemTmpFree (data.cache);
68}
69
70// static
71DECLCALLBACK(size_t) Utf8StrFmt::strOutput (void *pvArg, const char *pachChars,
72 size_t cbChars)
73{
74 Assert (pvArg);
75 FormatData &data = *(FormatData *) pvArg;
76
77 if (!(pachChars == NULL && cbChars == 0))
78 {
79 Assert (pachChars);
80
81 // append to cache (always assume an extra byte for a terminating zero)
82 size_t needed = cbChars + 1;
83 if (data.pos + needed > data.size)
84 {
85 data.size += FormatData::CacheIncrement;
86 if (needed >= FormatData::CacheIncrement)
87 data.size += needed;
88 data.cache = (char *) ::RTMemRealloc (data.cache, data.size);
89 }
90 strncpy (data.cache + data.pos, pachChars, cbChars);
91 data.pos += cbChars;
92 }
93
94 return cbChars;
95}
96
97} /* 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