1 | /* $Id: strprintf2.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - String Formatters, alternative.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/string.h>
|
---|
42 | #include "internal/iprt.h"
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Structures and Typedefs *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | /** rtStrPrintf2Output() argument structure. */
|
---|
51 | typedef struct STRPRINTF2OUTPUTARGS
|
---|
52 | {
|
---|
53 | /** Pointer to current buffer position. */
|
---|
54 | char *pszCur;
|
---|
55 | /** Number of bytes left in the buffer (including the trailing zero). */
|
---|
56 | size_t cbLeft;
|
---|
57 | /** Set if we overflowed. */
|
---|
58 | bool fOverflowed;
|
---|
59 | } STRPRINTF2OUTPUTARGS;
|
---|
60 | /** Pointer to a rtStrPrintf2Output() argument structure. */
|
---|
61 | typedef STRPRINTF2OUTPUTARGS *PSTRPRINTF2OUTPUTARGS;
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Output callback.
|
---|
66 | *
|
---|
67 | * @returns cbChars
|
---|
68 | *
|
---|
69 | * @param pvArg Pointer to a STRBUFARG structure.
|
---|
70 | * @param pachChars Pointer to an array of utf-8 characters.
|
---|
71 | * @param cbChars Number of bytes in the character array pointed to by pachChars.
|
---|
72 | */
|
---|
73 | static DECLCALLBACK(size_t) rtStrPrintf2Output(void *pvArg, const char *pachChars, size_t cbChars)
|
---|
74 | {
|
---|
75 | PSTRPRINTF2OUTPUTARGS pArgs = (PSTRPRINTF2OUTPUTARGS)pvArg;
|
---|
76 | char *pszCur = pArgs->pszCur; /* We actually have to spell this out for VS2010, or it will load it for each case. */
|
---|
77 |
|
---|
78 | if (cbChars < pArgs->cbLeft)
|
---|
79 | {
|
---|
80 | pArgs->cbLeft -= cbChars;
|
---|
81 |
|
---|
82 | /* Note! For VS2010/64 we need at least 7 case statements before it generates a jump table. */
|
---|
83 | switch (cbChars)
|
---|
84 | {
|
---|
85 | default:
|
---|
86 | memcpy(pszCur, pachChars, cbChars);
|
---|
87 | break;
|
---|
88 | case 8: pszCur[7] = pachChars[7]; RT_FALL_THRU();
|
---|
89 | case 7: pszCur[6] = pachChars[6]; RT_FALL_THRU();
|
---|
90 | case 6: pszCur[5] = pachChars[5]; RT_FALL_THRU();
|
---|
91 | case 5: pszCur[4] = pachChars[4]; RT_FALL_THRU();
|
---|
92 | case 4: pszCur[3] = pachChars[3]; RT_FALL_THRU();
|
---|
93 | case 3: pszCur[2] = pachChars[2]; RT_FALL_THRU();
|
---|
94 | case 2: pszCur[1] = pachChars[1]; RT_FALL_THRU();
|
---|
95 | case 1: pszCur[0] = pachChars[0]; RT_FALL_THRU();
|
---|
96 | case 0:
|
---|
97 | break;
|
---|
98 | }
|
---|
99 | pArgs->pszCur = pszCur += cbChars;
|
---|
100 | *pszCur = '\0';
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | size_t cbLeft = pArgs->cbLeft;
|
---|
105 | if (cbLeft-- > 1)
|
---|
106 | {
|
---|
107 | memcpy(pszCur, pachChars, cbLeft);
|
---|
108 | pArgs->pszCur = pszCur += cbLeft;
|
---|
109 | *pszCur = '\0';
|
---|
110 | pArgs->cbLeft = 1;
|
---|
111 | }
|
---|
112 | pArgs->fOverflowed = true;
|
---|
113 | }
|
---|
114 |
|
---|
115 | return cbChars;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | RTDECL(ssize_t) RTStrPrintf2V(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
120 | {
|
---|
121 | STRPRINTF2OUTPUTARGS Args;
|
---|
122 | size_t cchRet;
|
---|
123 | AssertMsg(cchBuffer > 0, ("Excellent idea! Format a string with no space for the output!\n"));
|
---|
124 |
|
---|
125 | Args.pszCur = pszBuffer;
|
---|
126 | Args.cbLeft = cchBuffer;
|
---|
127 | Args.fOverflowed = false;
|
---|
128 |
|
---|
129 | cchRet = RTStrFormatV(rtStrPrintf2Output, &Args, NULL, NULL, pszFormat, args);
|
---|
130 |
|
---|
131 | return !Args.fOverflowed ? (ssize_t)cchRet : -(ssize_t)cchRet - 1;
|
---|
132 | }
|
---|
133 | RT_EXPORT_SYMBOL(RTStrPrintf2V);
|
---|
134 |
|
---|
135 |
|
---|
136 | RTDECL(ssize_t) RTStrPrintf2ExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
|
---|
137 | const char *pszFormat, va_list args)
|
---|
138 | {
|
---|
139 | STRPRINTF2OUTPUTARGS Args;
|
---|
140 | size_t cchRet;
|
---|
141 | AssertMsg(cchBuffer > 0, ("Excellent idea! Format a string with no space for the output!\n"));
|
---|
142 |
|
---|
143 | Args.pszCur = pszBuffer;
|
---|
144 | Args.cbLeft = cchBuffer;
|
---|
145 | Args.fOverflowed = false;
|
---|
146 | cchRet = RTStrFormatV(rtStrPrintf2Output, &Args, pfnFormat, pvArg, pszFormat, args);
|
---|
147 | return !Args.fOverflowed ? (ssize_t)cchRet : -(ssize_t)cchRet - 1;
|
---|
148 | }
|
---|
149 | RT_EXPORT_SYMBOL(RTStrPrintf2ExV);
|
---|
150 |
|
---|