1 | /* $Id: straprintf.cpp 8245 2008-04-21 17:24:28Z 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 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/alloc.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*******************************************************************************
|
---|
40 | * Structures and Typedefs *
|
---|
41 | *******************************************************************************/
|
---|
42 | /** strallocoutput() argument structure. */
|
---|
43 | typedef struct STRALLOCARG
|
---|
44 | {
|
---|
45 | /** Pointer to current buffer position. */
|
---|
46 | char *psz;
|
---|
47 | /** Number of bytes left in the buffer - not including the trailing zero. */
|
---|
48 | size_t cch;
|
---|
49 | /** Pointer to the start of the buffer. */
|
---|
50 | char *pszBuffer;
|
---|
51 | /** The number of bytes in the buffer. */
|
---|
52 | size_t cchBuffer;
|
---|
53 | /** Set if the buffer was allocated using RTMemRealloc(). If clear
|
---|
54 | * pszBuffer points to the initial stack buffer. */
|
---|
55 | bool fAllocated;
|
---|
56 | } STRALLOCARG;
|
---|
57 | /** Pointer to a strallocoutput() argument structure. */
|
---|
58 | typedef STRALLOCARG *PSTRALLOCARG;
|
---|
59 |
|
---|
60 |
|
---|
61 | /*******************************************************************************
|
---|
62 | * Internal Functions *
|
---|
63 | *******************************************************************************/
|
---|
64 | static DECLCALLBACK(size_t) strallocoutput(void *pvArg, const char *pachChars, size_t cbChars);
|
---|
65 |
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Output callback.
|
---|
69 | *
|
---|
70 | * @returns number of bytes written.
|
---|
71 | * @param pvArg Pointer to a STRBUFARG structure.
|
---|
72 | * @param pachChars Pointer to an array of utf-8 characters.
|
---|
73 | * @param cbChars Number of bytes in the character array pointed to by pachChars.
|
---|
74 | */
|
---|
75 | static DECLCALLBACK(size_t) strallocoutput(void *pvArg, const char *pachChars, size_t cbChars)
|
---|
76 | {
|
---|
77 | PSTRALLOCARG pArg = (PSTRALLOCARG)pvArg;
|
---|
78 | if (pArg->psz)
|
---|
79 | {
|
---|
80 | /*
|
---|
81 | * The fast path
|
---|
82 | */
|
---|
83 | if (cbChars <= pArg->cch)
|
---|
84 | {
|
---|
85 | if (cbChars)
|
---|
86 | {
|
---|
87 | memcpy(pArg->psz, pachChars, cbChars);
|
---|
88 | pArg->cch -= cbChars;
|
---|
89 | pArg->psz += cbChars;
|
---|
90 | }
|
---|
91 | *pArg->psz = '\0';
|
---|
92 | return cbChars;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Need to (re)allocate the buffer.
|
---|
97 | */
|
---|
98 | size_t cbAdded = RT_MIN(pArg->cchBuffer, _1M);
|
---|
99 | if (cbAdded <= cbChars)
|
---|
100 | cbAdded = RT_ALIGN_Z(cbChars, _4K);
|
---|
101 | if (cbAdded <= _1G)
|
---|
102 | {
|
---|
103 | char *pszBuffer = (char *)RTMemRealloc(pArg->fAllocated ? pArg->pszBuffer : NULL, cbAdded + pArg->cchBuffer);
|
---|
104 | if (pszBuffer)
|
---|
105 | {
|
---|
106 | size_t off = pArg->psz - pArg->pszBuffer;
|
---|
107 | if (!pArg->fAllocated)
|
---|
108 | {
|
---|
109 | memcpy(pszBuffer, pArg->pszBuffer, off);
|
---|
110 | pArg->fAllocated = true;
|
---|
111 | }
|
---|
112 |
|
---|
113 | pArg->pszBuffer = pszBuffer;
|
---|
114 | pArg->cchBuffer += cbAdded;
|
---|
115 | pArg->psz = pszBuffer + off;
|
---|
116 | pArg->cch += cbAdded;
|
---|
117 |
|
---|
118 | if (cbChars)
|
---|
119 | {
|
---|
120 | memcpy(pArg->psz, pachChars, cbChars);
|
---|
121 | pArg->cch -= cbChars;
|
---|
122 | pArg->psz += cbChars;
|
---|
123 | }
|
---|
124 | *pArg->psz = '\0';
|
---|
125 | return cbChars;
|
---|
126 | }
|
---|
127 | /* else allocation failure */
|
---|
128 | }
|
---|
129 | /* else wrap around */
|
---|
130 |
|
---|
131 | /* failure */
|
---|
132 | pArg->psz = NULL;
|
---|
133 | }
|
---|
134 | return 0;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | RTDECL(int) RTStrAPrintfV(char **ppszBuffer, const char *pszFormat, va_list args)
|
---|
139 | {
|
---|
140 | char szBuf[2048];
|
---|
141 | STRALLOCARG Arg;
|
---|
142 | Arg.fAllocated = false;
|
---|
143 | Arg.cchBuffer = sizeof(szBuf);
|
---|
144 | Arg.pszBuffer = szBuf;
|
---|
145 | Arg.cch = sizeof(szBuf) - 1;
|
---|
146 | Arg.psz = szBuf;
|
---|
147 | szBuf[0] = '\0';
|
---|
148 | int cbRet = (int)RTStrFormatV(strallocoutput, &Arg, NULL, NULL, pszFormat, args);
|
---|
149 | if (Arg.psz)
|
---|
150 | {
|
---|
151 | if (!Arg.fAllocated)
|
---|
152 | {
|
---|
153 | /* duplicate the string in szBuf */
|
---|
154 | Assert(Arg.pszBuffer == szBuf);
|
---|
155 | char *psz = (char *)RTMemAlloc(cbRet + 1);
|
---|
156 | if (psz)
|
---|
157 | memcpy(psz, szBuf, cbRet + 1);
|
---|
158 | *ppszBuffer = psz;
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | /* adjust the allocated buffer */
|
---|
163 | char *psz = (char *)RTMemRealloc(Arg.pszBuffer, cbRet + 1);
|
---|
164 | *ppszBuffer = psz ? psz : Arg.pszBuffer;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | else
|
---|
168 | {
|
---|
169 | /* allocation error */
|
---|
170 | *ppszBuffer = NULL;
|
---|
171 | cbRet = -1;
|
---|
172 |
|
---|
173 | /* free any allocated buffer */
|
---|
174 | if (Arg.fAllocated)
|
---|
175 | RTMemFree(Arg.pszBuffer);
|
---|
176 | }
|
---|
177 |
|
---|
178 | return cbRet;
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | RTDECL(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 | }
|
---|
190 |
|
---|