VirtualBox

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

Last change on this file since 30749 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1/* $Id: straprintf.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - Allocating String Formatters.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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} STRALLOCARG;
56/** Pointer to a strallocoutput() argument structure. */
57typedef STRALLOCARG *PSTRALLOCARG;
58
59
60/*******************************************************************************
61* Internal Functions *
62*******************************************************************************/
63static DECLCALLBACK(size_t) strallocoutput(void *pvArg, const char *pachChars, size_t cbChars);
64
65
66/**
67 * Output callback.
68 *
69 * @returns number of bytes written.
70 * @param pvArg Pointer to a STRBUFARG structure.
71 * @param pachChars Pointer to an array of utf-8 characters.
72 * @param cbChars Number of bytes in the character array pointed to by pachChars.
73 */
74static DECLCALLBACK(size_t) strallocoutput(void *pvArg, const char *pachChars, size_t cbChars)
75{
76 PSTRALLOCARG pArg = (PSTRALLOCARG)pvArg;
77 if (pArg->psz)
78 {
79 /*
80 * The fast path
81 */
82 if (cbChars <= pArg->cch)
83 {
84 if (cbChars)
85 {
86 memcpy(pArg->psz, pachChars, cbChars);
87 pArg->cch -= cbChars;
88 pArg->psz += cbChars;
89 }
90 *pArg->psz = '\0';
91 return cbChars;
92 }
93
94 /*
95 * Need to (re)allocate the buffer.
96 */
97 size_t cbAdded = RT_MIN(pArg->cchBuffer, _1M);
98 if (cbAdded <= cbChars)
99 cbAdded = RT_ALIGN_Z(cbChars, _4K);
100 if (cbAdded <= _1G)
101 {
102 char *pszBuffer = (char *)RTMemRealloc(pArg->fAllocated ? pArg->pszBuffer : NULL, cbAdded + pArg->cchBuffer);
103 if (pszBuffer)
104 {
105 size_t off = pArg->psz - pArg->pszBuffer;
106 if (!pArg->fAllocated)
107 {
108 memcpy(pszBuffer, pArg->pszBuffer, off);
109 pArg->fAllocated = true;
110 }
111
112 pArg->pszBuffer = pszBuffer;
113 pArg->cchBuffer += cbAdded;
114 pArg->psz = pszBuffer + off;
115 pArg->cch += cbAdded;
116
117 if (cbChars)
118 {
119 memcpy(pArg->psz, pachChars, cbChars);
120 pArg->cch -= cbChars;
121 pArg->psz += cbChars;
122 }
123 *pArg->psz = '\0';
124 return cbChars;
125 }
126 /* else allocation failure */
127 }
128 /* else wrap around */
129
130 /* failure */
131 pArg->psz = NULL;
132 }
133 return 0;
134}
135
136
137RTDECL(int) RTStrAPrintfV(char **ppszBuffer, const char *pszFormat, va_list args)
138{
139 char szBuf[2048];
140 STRALLOCARG Arg;
141 Arg.fAllocated = false;
142 Arg.cchBuffer = sizeof(szBuf);
143 Arg.pszBuffer = szBuf;
144 Arg.cch = sizeof(szBuf) - 1;
145 Arg.psz = szBuf;
146 szBuf[0] = '\0';
147 int cbRet = (int)RTStrFormatV(strallocoutput, &Arg, NULL, NULL, pszFormat, args);
148 if (Arg.psz)
149 {
150 if (!Arg.fAllocated)
151 {
152 /* duplicate the string in szBuf */
153 Assert(Arg.pszBuffer == szBuf);
154 char *psz = (char *)RTMemAlloc(cbRet + 1);
155 if (psz)
156 memcpy(psz, szBuf, cbRet + 1);
157 *ppszBuffer = psz;
158 }
159 else
160 {
161 /* adjust the allocated buffer */
162 char *psz = (char *)RTMemRealloc(Arg.pszBuffer, cbRet + 1);
163 *ppszBuffer = psz ? psz : Arg.pszBuffer;
164 }
165 }
166 else
167 {
168 /* allocation error */
169 *ppszBuffer = NULL;
170 cbRet = -1;
171
172 /* free any allocated buffer */
173 if (Arg.fAllocated)
174 RTMemFree(Arg.pszBuffer);
175 }
176
177 return cbRet;
178}
179RT_EXPORT_SYMBOL(RTStrAPrintfV);
180
181
182RTDECL(int) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
183{
184 va_list args;
185 va_start(args, pszFormat);
186 int cbRet = RTStrAPrintfV(ppszBuffer, pszFormat, args);
187 va_end(args);
188 return cbRet;
189}
190RT_EXPORT_SYMBOL(RTStrAPrintf);
191
192
193RTDECL(char *) RTStrAPrintf2V(const char *pszFormat, va_list args)
194{
195 char *pszBuffer;
196 RTStrAPrintfV(&pszBuffer, pszFormat, args);
197 return pszBuffer;
198}
199RT_EXPORT_SYMBOL(RTStrAPrintf2V);
200
201
202RTDECL(char *) RTStrAPrintf2(const char *pszFormat, ...)
203{
204 va_list va;
205 char *pszBuffer;
206
207 va_start(va, pszFormat);
208 RTStrAPrintfV(&pszBuffer, pszFormat, va);
209 va_end(va);
210
211 return pszBuffer;
212}
213RT_EXPORT_SYMBOL(RTStrAPrintf2);
214
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