VirtualBox

source: vbox/trunk/src/VBox/Runtime/straprintf.cpp@ 3699

Last change on this file since 3699 was 2981, checked in by vboxsync, 17 years ago

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.7 KB
Line 
1/* $Id: straprintf.cpp 2981 2007-06-01 16:01:28Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Allocating String Formatters.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#include <iprt/string.h>
26#include <iprt/assert.h>
27#include <iprt/alloc.h>
28
29
30/*******************************************************************************
31* Structures and Typedefs *
32*******************************************************************************/
33/** strallocoutput() argument structure. */
34typedef struct STRALLOCARG
35{
36 /** Pointer to current buffer position. */
37 char *psz;
38 /** Number of bytes left in the buffer - not including the trailing zero. */
39 size_t cch;
40 /** Pointer to the start of the buffer. */
41 char *pszBuffer;
42 /** The number of bytes in the buffer. */
43 size_t cchBuffer;
44 /** Set if the buffer was allocated using RTMemRealloc(). If clear
45 * pszBuffer points to the initial stack buffer. */
46 bool fAllocated;
47} STRALLOCARG;
48/** Pointer to a strallocoutput() argument structure. */
49typedef STRALLOCARG *PSTRALLOCARG;
50
51
52/*******************************************************************************
53* Internal Functions *
54*******************************************************************************/
55static DECLCALLBACK(size_t) strallocoutput(void *pvArg, const char *pachChars, size_t cbChars);
56
57
58/**
59 * Output callback.
60 *
61 * @returns number of bytes written.
62 * @param pvArg Pointer to a STRBUFARG structure.
63 * @param pachChars Pointer to an array of utf-8 characters.
64 * @param cbChars Number of bytes in the character array pointed to by pachChars.
65 */
66static DECLCALLBACK(size_t) strallocoutput(void *pvArg, const char *pachChars, size_t cbChars)
67{
68 PSTRALLOCARG pArg = (PSTRALLOCARG)pvArg;
69 if (pArg->psz)
70 {
71 /*
72 * The fast path
73 */
74 if (cbChars <= pArg->cch)
75 {
76 if (cbChars)
77 {
78 memcpy(pArg->psz, pachChars, cbChars);
79 pArg->cch -= cbChars;
80 pArg->psz += cbChars;
81 }
82 *pArg->psz = '\0';
83 return cbChars;
84 }
85
86 /*
87 * Need to (re)allocate the buffer.
88 */
89 size_t cbAdded = RT_MIN(pArg->cchBuffer, _1M);
90 if (cbAdded <= cbChars)
91 cbAdded = RT_ALIGN_Z(cbChars, _4K);
92 if (cbAdded <= _1G)
93 {
94 char *pszBuffer = (char *)RTMemRealloc(pArg->fAllocated ? pArg->pszBuffer : NULL, cbAdded + pArg->cchBuffer);
95 if (pszBuffer)
96 {
97 size_t off = pArg->psz - pArg->pszBuffer;
98 if (!pArg->fAllocated)
99 {
100 memcpy(pszBuffer, pArg->pszBuffer, off);
101 pArg->fAllocated = true;
102 }
103
104 pArg->pszBuffer = pszBuffer;
105 pArg->cchBuffer += cbAdded;
106 pArg->psz = pszBuffer + off;
107 pArg->cch += cbAdded;
108
109 if (cbChars)
110 {
111 memcpy(pArg->psz, pachChars, cbChars);
112 pArg->cch -= cbChars;
113 pArg->psz += cbChars;
114 }
115 *pArg->psz = '\0';
116 return cbChars;
117 }
118 /* else allocation failure */
119 }
120 /* else wrap around */
121
122 /* failure */
123 pArg->psz = NULL;
124 }
125 return 0;
126}
127
128
129RTDECL(int) RTStrAPrintfV(char **ppszBuffer, const char *pszFormat, va_list args)
130{
131 char szBuf[2048];
132 STRALLOCARG Arg;
133 Arg.fAllocated = false;
134 Arg.cchBuffer = sizeof(szBuf);
135 Arg.pszBuffer = szBuf;
136 Arg.cch = sizeof(szBuf) - 1;
137 Arg.psz = szBuf;
138 szBuf[0] = '\0';
139 int cbRet = (int)RTStrFormatV(strallocoutput, &Arg, NULL, NULL, pszFormat, args);
140 if (Arg.psz)
141 {
142 if (!Arg.fAllocated)
143 {
144 /* duplicate the string in szBuf */
145 Assert(Arg.pszBuffer == szBuf);
146 char *psz = (char *)RTMemAlloc(cbRet + 1);
147 if (psz)
148 memcpy(psz, szBuf, cbRet + 1);
149 *ppszBuffer = psz;
150 }
151 else
152 {
153 /* adjust the allocated buffer */
154 char *psz = (char *)RTMemRealloc(Arg.pszBuffer, cbRet + 1);
155 *ppszBuffer = psz ? psz : Arg.pszBuffer;
156 }
157 }
158 else
159 {
160 /* allocation error */
161 *ppszBuffer = NULL;
162 cbRet = -1;
163
164 /* free any allocated buffer */
165 if (Arg.fAllocated)
166 RTMemFree(Arg.pszBuffer);
167 }
168
169 return cbRet;
170}
171
172
173RTDECL(int) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
174{
175 va_list args;
176 va_start(args, pszFormat);
177 size_t cbRet = RTStrAPrintfV(ppszBuffer, pszFormat, args);
178 va_end(args);
179 return cbRet;
180}
181
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