1 | /* $Id: strprintf.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - String Formatters.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 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 | /** strbufoutput() argument structure. */
|
---|
51 | typedef struct STRBUFARG
|
---|
52 | {
|
---|
53 | /** Pointer to current buffer position. */
|
---|
54 | char *psz;
|
---|
55 | /** Number of bytes left in the buffer - not including the trailing zero. */
|
---|
56 | size_t cch;
|
---|
57 | } STRBUFARG;
|
---|
58 | /** Pointer to a strbufoutput() argument structure. */
|
---|
59 | typedef STRBUFARG *PSTRBUFARG;
|
---|
60 |
|
---|
61 |
|
---|
62 | /*********************************************************************************************************************************
|
---|
63 | * Internal Functions *
|
---|
64 | *********************************************************************************************************************************/
|
---|
65 | static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars);
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Output callback.
|
---|
70 | *
|
---|
71 | * @returns number of bytes written.
|
---|
72 | * @param pvArg Pointer to a STRBUFARG structure.
|
---|
73 | * @param pachChars Pointer to an array of utf-8 characters.
|
---|
74 | * @param cbChars Number of bytes in the character array pointed to by pachChars.
|
---|
75 | */
|
---|
76 | static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars)
|
---|
77 | {
|
---|
78 | PSTRBUFARG pArg = (PSTRBUFARG)pvArg;
|
---|
79 | char *pszCur = pArg->psz; /* We actually have to spell this out for VS2010, or it will load for each case. */
|
---|
80 |
|
---|
81 | cbChars = RT_MIN(pArg->cch, cbChars);
|
---|
82 | if (cbChars)
|
---|
83 | {
|
---|
84 | pArg->cch -= cbChars;
|
---|
85 |
|
---|
86 | /* Note! For VS2010/64 we need at least 7 case statements before it generates a jump table. */
|
---|
87 | switch (cbChars)
|
---|
88 | {
|
---|
89 | default:
|
---|
90 | memcpy(pszCur, pachChars, cbChars);
|
---|
91 | break;
|
---|
92 | case 8: pszCur[7] = pachChars[7]; RT_FALL_THRU();
|
---|
93 | case 7: pszCur[6] = pachChars[6]; RT_FALL_THRU();
|
---|
94 | case 6: pszCur[5] = pachChars[5]; RT_FALL_THRU();
|
---|
95 | case 5: pszCur[4] = pachChars[4]; RT_FALL_THRU();
|
---|
96 | case 4: pszCur[3] = pachChars[3]; RT_FALL_THRU();
|
---|
97 | case 3: pszCur[2] = pachChars[2]; RT_FALL_THRU();
|
---|
98 | case 2: pszCur[1] = pachChars[1]; RT_FALL_THRU();
|
---|
99 | case 1: pszCur[0] = pachChars[0]; RT_FALL_THRU();
|
---|
100 | case 0:
|
---|
101 | break;
|
---|
102 | }
|
---|
103 | pArg->psz = pszCur += cbChars;
|
---|
104 | }
|
---|
105 | *pszCur = '\0';
|
---|
106 |
|
---|
107 | return cbChars;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
112 | {
|
---|
113 | STRBUFARG Arg;
|
---|
114 | AssertMsgReturn(cchBuffer, ("Excellent idea! Format a string with no space for the output!\n"), 0);
|
---|
115 | Arg.psz = pszBuffer;
|
---|
116 | Arg.cch = cchBuffer - 1;
|
---|
117 | return RTStrFormatV(strbufoutput, &Arg, NULL, NULL, pszFormat, args);
|
---|
118 | }
|
---|
119 | RT_EXPORT_SYMBOL(RTStrPrintfV);
|
---|
120 |
|
---|
121 |
|
---|
122 | RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
123 | {
|
---|
124 | STRBUFARG Arg;
|
---|
125 | AssertMsgReturn(cchBuffer, ("Excellent idea! Format a string with no space for the output!\n"), 0);
|
---|
126 | Arg.psz = pszBuffer;
|
---|
127 | Arg.cch = cchBuffer - 1;
|
---|
128 | return RTStrFormatV(strbufoutput, &Arg, pfnFormat, pvArg, pszFormat, args);
|
---|
129 | }
|
---|
130 | RT_EXPORT_SYMBOL(RTStrPrintfExV);
|
---|
131 |
|
---|