VirtualBox

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

Last change on this file since 32599 was 31157, checked in by vboxsync, 14 years ago

iprt,++: Tag allocation in all builds with a string, defaulting to FILE.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1/* $Id: straprintf.cpp 31157 2010-07-28 03:15:35Z vboxsync $ */
2/** @file
3 * IPRT - Allocating String Formatters.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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 char szBuf[2048];
143 STRALLOCARG Arg;
144 Arg.fAllocated = false;
145 Arg.cchBuffer = sizeof(szBuf);
146 Arg.pszBuffer = szBuf;
147 Arg.cch = sizeof(szBuf) - 1;
148 Arg.psz = szBuf;
149 Arg.pszTag = pszTag;
150 szBuf[0] = '\0';
151 int cbRet = (int)RTStrFormatV(strallocoutput, &Arg, NULL, NULL, pszFormat, args);
152 if (Arg.psz)
153 {
154 if (!Arg.fAllocated)
155 {
156 /* duplicate the string in szBuf */
157 Assert(Arg.pszBuffer == szBuf);
158 char *psz = (char *)RTMemAllocTag(cbRet + 1, pszTag);
159 if (psz)
160 memcpy(psz, szBuf, cbRet + 1);
161 *ppszBuffer = psz;
162 }
163 else
164 {
165 /* adjust the allocated buffer */
166 char *psz = (char *)RTMemReallocTag(Arg.pszBuffer, cbRet + 1, pszTag);
167 *ppszBuffer = psz ? psz : Arg.pszBuffer;
168 }
169 }
170 else
171 {
172 /* allocation error */
173 *ppszBuffer = NULL;
174 cbRet = -1;
175
176 /* free any allocated buffer */
177 if (Arg.fAllocated)
178 RTMemFree(Arg.pszBuffer);
179 }
180
181 return cbRet;
182}
183RT_EXPORT_SYMBOL(RTStrAPrintfVTag);
184
185
186RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag)
187{
188 char *pszBuffer;
189 RTStrAPrintfVTag(&pszBuffer, pszFormat, args, pszTag);
190 return pszBuffer;
191}
192RT_EXPORT_SYMBOL(RTStrAPrintf2VTag);
193
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