1 | /* $Id: strprintf.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - String Formatters.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
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 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | /*********************************************************************************************************************************
|
---|
38 | * Structures and Typedefs *
|
---|
39 | *********************************************************************************************************************************/
|
---|
40 | /** strbufoutput() argument structure. */
|
---|
41 | typedef struct STRBUFARG
|
---|
42 | {
|
---|
43 | /** Pointer to current buffer position. */
|
---|
44 | char *psz;
|
---|
45 | /** Number of bytes left in the buffer - not including the trailing zero. */
|
---|
46 | size_t cch;
|
---|
47 | } STRBUFARG;
|
---|
48 | /** Pointer to a strbufoutput() argument structure. */
|
---|
49 | typedef STRBUFARG *PSTRBUFARG;
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Internal Functions *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 | static DECLCALLBACK(size_t) strbufoutput(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 | */
|
---|
66 | static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars)
|
---|
67 | {
|
---|
68 | PSTRBUFARG pArg = (PSTRBUFARG)pvArg;
|
---|
69 | char *pszCur = pArg->psz; /* We actually have to spell this out for VS2010, or it will load for each case. */
|
---|
70 |
|
---|
71 | cbChars = RT_MIN(pArg->cch, cbChars);
|
---|
72 | if (cbChars)
|
---|
73 | {
|
---|
74 | pArg->cch -= cbChars;
|
---|
75 |
|
---|
76 | /* Note! For VS2010/64 we need at least 7 case statements before it generates a jump table. */
|
---|
77 | switch (cbChars)
|
---|
78 | {
|
---|
79 | default:
|
---|
80 | memcpy(pszCur, pachChars, cbChars);
|
---|
81 | break;
|
---|
82 | case 8: pszCur[7] = pachChars[7]; RT_FALL_THRU();
|
---|
83 | case 7: pszCur[6] = pachChars[6]; RT_FALL_THRU();
|
---|
84 | case 6: pszCur[5] = pachChars[5]; RT_FALL_THRU();
|
---|
85 | case 5: pszCur[4] = pachChars[4]; RT_FALL_THRU();
|
---|
86 | case 4: pszCur[3] = pachChars[3]; RT_FALL_THRU();
|
---|
87 | case 3: pszCur[2] = pachChars[2]; RT_FALL_THRU();
|
---|
88 | case 2: pszCur[1] = pachChars[1]; RT_FALL_THRU();
|
---|
89 | case 1: pszCur[0] = pachChars[0]; RT_FALL_THRU();
|
---|
90 | case 0:
|
---|
91 | break;
|
---|
92 | }
|
---|
93 | pArg->psz = pszCur += cbChars;
|
---|
94 | }
|
---|
95 | *pszCur = '\0';
|
---|
96 |
|
---|
97 | return cbChars;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
102 | {
|
---|
103 | STRBUFARG Arg;
|
---|
104 | AssertMsgReturn(cchBuffer, ("Excellent idea! Format a string with no space for the output!\n"), 0);
|
---|
105 | Arg.psz = pszBuffer;
|
---|
106 | Arg.cch = cchBuffer - 1;
|
---|
107 | return RTStrFormatV(strbufoutput, &Arg, NULL, NULL, pszFormat, args);
|
---|
108 | }
|
---|
109 | RT_EXPORT_SYMBOL(RTStrPrintfV);
|
---|
110 |
|
---|
111 |
|
---|
112 | RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
113 | {
|
---|
114 | STRBUFARG Arg;
|
---|
115 | AssertMsgReturn(cchBuffer, ("Excellent idea! Format a string with no space for the output!\n"), 0);
|
---|
116 | Arg.psz = pszBuffer;
|
---|
117 | Arg.cch = cchBuffer - 1;
|
---|
118 | return RTStrFormatV(strbufoutput, &Arg, pfnFormat, pvArg, pszFormat, args);
|
---|
119 | }
|
---|
120 | RT_EXPORT_SYMBOL(RTStrPrintfExV);
|
---|
121 |
|
---|