VirtualBox

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

Last change on this file since 4968 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

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