VirtualBox

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

Last change on this file since 24285 was 21337, checked in by vboxsync, 16 years ago

IPRT,HostDrv,AddDrv: Export public IPRT symbols for the linux kernel (pain).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1/* $Id: straprintf.cpp 21337 2009-07-07 14:58:27Z vboxsync $ */
2/** @file
3 * IPRT - Allocating String Formatters.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/string.h>
36#include "internal/iprt.h"
37
38#include <iprt/assert.h>
39#include <iprt/alloc.h>
40
41
42/*******************************************************************************
43* Structures and Typedefs *
44*******************************************************************************/
45/** strallocoutput() argument structure. */
46typedef struct STRALLOCARG
47{
48 /** Pointer to current buffer position. */
49 char *psz;
50 /** Number of bytes left in the buffer - not including the trailing zero. */
51 size_t cch;
52 /** Pointer to the start of the buffer. */
53 char *pszBuffer;
54 /** The number of bytes in the buffer. */
55 size_t cchBuffer;
56 /** Set if the buffer was allocated using RTMemRealloc(). If clear
57 * pszBuffer points to the initial stack buffer. */
58 bool fAllocated;
59} STRALLOCARG;
60/** Pointer to a strallocoutput() argument structure. */
61typedef STRALLOCARG *PSTRALLOCARG;
62
63
64/*******************************************************************************
65* Internal Functions *
66*******************************************************************************/
67static DECLCALLBACK(size_t) strallocoutput(void *pvArg, const char *pachChars, size_t cbChars);
68
69
70/**
71 * Output callback.
72 *
73 * @returns number of bytes written.
74 * @param pvArg Pointer to a STRBUFARG structure.
75 * @param pachChars Pointer to an array of utf-8 characters.
76 * @param cbChars Number of bytes in the character array pointed to by pachChars.
77 */
78static DECLCALLBACK(size_t) strallocoutput(void *pvArg, const char *pachChars, size_t cbChars)
79{
80 PSTRALLOCARG pArg = (PSTRALLOCARG)pvArg;
81 if (pArg->psz)
82 {
83 /*
84 * The fast path
85 */
86 if (cbChars <= pArg->cch)
87 {
88 if (cbChars)
89 {
90 memcpy(pArg->psz, pachChars, cbChars);
91 pArg->cch -= cbChars;
92 pArg->psz += cbChars;
93 }
94 *pArg->psz = '\0';
95 return cbChars;
96 }
97
98 /*
99 * Need to (re)allocate the buffer.
100 */
101 size_t cbAdded = RT_MIN(pArg->cchBuffer, _1M);
102 if (cbAdded <= cbChars)
103 cbAdded = RT_ALIGN_Z(cbChars, _4K);
104 if (cbAdded <= _1G)
105 {
106 char *pszBuffer = (char *)RTMemRealloc(pArg->fAllocated ? pArg->pszBuffer : NULL, cbAdded + pArg->cchBuffer);
107 if (pszBuffer)
108 {
109 size_t off = pArg->psz - pArg->pszBuffer;
110 if (!pArg->fAllocated)
111 {
112 memcpy(pszBuffer, pArg->pszBuffer, off);
113 pArg->fAllocated = true;
114 }
115
116 pArg->pszBuffer = pszBuffer;
117 pArg->cchBuffer += cbAdded;
118 pArg->psz = pszBuffer + off;
119 pArg->cch += cbAdded;
120
121 if (cbChars)
122 {
123 memcpy(pArg->psz, pachChars, cbChars);
124 pArg->cch -= cbChars;
125 pArg->psz += cbChars;
126 }
127 *pArg->psz = '\0';
128 return cbChars;
129 }
130 /* else allocation failure */
131 }
132 /* else wrap around */
133
134 /* failure */
135 pArg->psz = NULL;
136 }
137 return 0;
138}
139
140
141RTDECL(int) RTStrAPrintfV(char **ppszBuffer, const char *pszFormat, va_list args)
142{
143 char szBuf[2048];
144 STRALLOCARG Arg;
145 Arg.fAllocated = false;
146 Arg.cchBuffer = sizeof(szBuf);
147 Arg.pszBuffer = szBuf;
148 Arg.cch = sizeof(szBuf) - 1;
149 Arg.psz = szBuf;
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 *)RTMemAlloc(cbRet + 1);
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 *)RTMemRealloc(Arg.pszBuffer, cbRet + 1);
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(RTStrAPrintfV);
184
185
186RTDECL(int) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
187{
188 va_list args;
189 va_start(args, pszFormat);
190 int cbRet = RTStrAPrintfV(ppszBuffer, pszFormat, args);
191 va_end(args);
192 return cbRet;
193}
194RT_EXPORT_SYMBOL(RTStrAPrintf);
195
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