VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/straprintf.cpp@ 45221

Last change on this file since 45221 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.3 KB
Line 
1/* $Id: straprintf.cpp 44528 2013-02-04 14:27:54Z vboxsync $ */
2/** @file
3 * IPRT - Allocating String Formatters.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <iprt/string.h>
32#include "internal/iprt.h"
33
34#include <iprt/assert.h>
35#include <iprt/alloc.h>
36
37
38/*******************************************************************************
39* Structures and Typedefs *
40*******************************************************************************/
41/** strallocoutput() argument structure. */
42typedef struct STRALLOCARG
43{
44 /** Pointer to current buffer position. */
45 char *psz;
46 /** Number of bytes left in the buffer - not including the trailing zero. */
47 size_t cch;
48 /** Pointer to the start of the buffer. */
49 char *pszBuffer;
50 /** The number of bytes in the buffer. */
51 size_t cchBuffer;
52 /** Set if the buffer was allocated using RTMemRealloc(). If clear
53 * pszBuffer points to the initial stack buffer. */
54 bool fAllocated;
55 /** Allocation tag used for statistics and such. */
56 const char *pszTag;
57} STRALLOCARG;
58/** Pointer to a strallocoutput() argument structure. */
59typedef STRALLOCARG *PSTRALLOCARG;
60
61
62/*******************************************************************************
63* Internal Functions *
64*******************************************************************************/
65static DECLCALLBACK(size_t) strallocoutput(void *pvArg, const char *pachChars, size_t cbChars);
66
67
68/**
69 * Output callback.
70 *
71 * @returns number of bytes written.
72 * @param pvArg Pointer to a STRBUFARG structure.
73 * @param pachChars Pointer to an array of utf-8 characters.
74 * @param cbChars Number of bytes in the character array pointed to by pachChars.
75 */
76static DECLCALLBACK(size_t) strallocoutput(void *pvArg, const char *pachChars, size_t cbChars)
77{
78 PSTRALLOCARG pArg = (PSTRALLOCARG)pvArg;
79 if (pArg->psz)
80 {
81 /*
82 * The fast path
83 */
84 if (cbChars <= pArg->cch)
85 {
86 if (cbChars)
87 {
88 memcpy(pArg->psz, pachChars, cbChars);
89 pArg->cch -= cbChars;
90 pArg->psz += cbChars;
91 }
92 *pArg->psz = '\0';
93 return cbChars;
94 }
95
96 /*
97 * Need to (re)allocate the buffer.
98 */
99 size_t cbAdded = RT_MIN(pArg->cchBuffer, _1M);
100 if (cbAdded <= cbChars)
101 cbAdded = RT_ALIGN_Z(cbChars, _4K);
102 if (cbAdded <= _1G)
103 {
104 char *pszBuffer = (char *)RTMemReallocTag(pArg->fAllocated ? pArg->pszBuffer : NULL,
105 cbAdded + pArg->cchBuffer, pArg->pszTag);
106 if (pszBuffer)
107 {
108 size_t off = pArg->psz - pArg->pszBuffer;
109 if (!pArg->fAllocated)
110 {
111 memcpy(pszBuffer, pArg->pszBuffer, off);
112 pArg->fAllocated = true;
113 }
114
115 pArg->pszBuffer = pszBuffer;
116 pArg->cchBuffer += cbAdded;
117 pArg->psz = pszBuffer + off;
118 pArg->cch += cbAdded;
119
120 if (cbChars)
121 {
122 memcpy(pArg->psz, pachChars, cbChars);
123 pArg->cch -= cbChars;
124 pArg->psz += cbChars;
125 }
126 *pArg->psz = '\0';
127 return cbChars;
128 }
129 /* else allocation failure */
130 }
131 /* else wrap around */
132
133 /* failure */
134 pArg->psz = NULL;
135 }
136 return 0;
137}
138
139
140RTDECL(int) RTStrAPrintfVTag(char **ppszBuffer, const char *pszFormat, va_list args, const char *pszTag)
141{
142#ifdef IN_RING3
143 char szBuf[2048];
144#else
145 char szBuf[256];
146#endif
147 STRALLOCARG Arg;
148 Arg.fAllocated = false;
149 Arg.cchBuffer = sizeof(szBuf);
150 Arg.pszBuffer = szBuf;
151 Arg.cch = sizeof(szBuf) - 1;
152 Arg.psz = szBuf;
153 Arg.pszTag = pszTag;
154 szBuf[0] = '\0';
155 int cbRet = (int)RTStrFormatV(strallocoutput, &Arg, NULL, NULL, pszFormat, args);
156 if (Arg.psz)
157 {
158 if (!Arg.fAllocated)
159 {
160 /* duplicate the string in szBuf */
161 Assert(Arg.pszBuffer == szBuf);
162 char *psz = (char *)RTMemAllocTag(cbRet + 1, pszTag);
163 if (psz)
164 memcpy(psz, szBuf, cbRet + 1);
165 *ppszBuffer = psz;
166 }
167 else
168 {
169 /* adjust the allocated buffer */
170 char *psz = (char *)RTMemReallocTag(Arg.pszBuffer, cbRet + 1, pszTag);
171 *ppszBuffer = psz ? psz : Arg.pszBuffer;
172 }
173 }
174 else
175 {
176 /* allocation error */
177 *ppszBuffer = NULL;
178 cbRet = -1;
179
180 /* free any allocated buffer */
181 if (Arg.fAllocated)
182 RTMemFree(Arg.pszBuffer);
183 }
184
185 return cbRet;
186}
187RT_EXPORT_SYMBOL(RTStrAPrintfVTag);
188
189
190RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag)
191{
192 char *pszBuffer;
193 RTStrAPrintfVTag(&pszBuffer, pszFormat, args, pszTag);
194 return pszBuffer;
195}
196RT_EXPORT_SYMBOL(RTStrAPrintf2VTag);
197
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