VirtualBox

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

Last change on this file since 2976 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.5 KB
Line 
1/** @file
2 *
3 * MS COM / XPCOM Abstraction Layer:
4 * Smart string classes definition
5 */
6
7/*
8 * Copyright (C) 2006 InnoTek Systemberatung GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 */
22
23#include "VBox/com/string.h"
24
25namespace com
26{
27
28struct FormatData
29{
30 static const size_t CacheIncrement = 256;
31 size_t size;
32 size_t pos;
33 char *cache;
34};
35
36void Utf8StrFmt::init (const char *format, va_list args)
37{
38 if (!format)
39 return;
40
41 // assume an extra byte for a terminating zero
42 size_t fmtlen = strlen (format) + 1;
43
44 FormatData data;
45 data.size = FormatData::CacheIncrement;
46 if (fmtlen >= FormatData::CacheIncrement)
47 data.size += fmtlen;
48 data.pos = 0;
49 data.cache = (char *) ::RTMemTmpAllocZ (data.size);
50
51 size_t n = ::RTStrFormatV (strOutput, &data, NULL, NULL, format, args);
52
53 AssertMsg (n == data.pos,
54 ("The number of bytes formatted doesn't match: %d and %d!",
55 n, data.pos));
56 NOREF (n);
57
58 // finalize formatting
59 data.cache [data.pos] = 0;
60 (*static_cast <Utf8Str *> (this)) = data.cache;
61 ::RTMemTmpFree (data.cache);
62}
63
64// static
65DECLCALLBACK(size_t) Utf8StrFmt::strOutput (void *pvArg, const char *pachChars,
66 size_t cbChars)
67{
68 Assert (pvArg);
69 FormatData &data = *(FormatData *) pvArg;
70
71 if (!(pachChars == NULL && cbChars == 0))
72 {
73 Assert (pachChars);
74
75 // append to cache (always assume an extra byte for a terminating zero)
76 size_t needed = cbChars + 1;
77 if (data.pos + needed > data.size)
78 {
79 data.size += FormatData::CacheIncrement;
80 if (needed >= FormatData::CacheIncrement)
81 data.size += needed;
82 data.cache = (char *) ::RTMemRealloc (data.cache, data.size);
83 }
84 strncpy (data.cache + data.pos, pachChars, cbChars);
85 data.pos += cbChars;
86 }
87
88 return cbChars;
89}
90
91}; // namespace com
92
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