1 | /* $Id: strprintf.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - String Formatters.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 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) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
|
---|
102 | {
|
---|
103 | /* Explicitly inline RTStrPrintfV + RTStrPrintfExV here because this is a frequently use API. */
|
---|
104 | STRBUFARG Arg;
|
---|
105 | va_list args;
|
---|
106 | size_t cbRet;
|
---|
107 |
|
---|
108 | AssertMsgReturn(cchBuffer, ("Excellent idea! Format a string with no space for the output!\n"), 0);
|
---|
109 | Arg.psz = pszBuffer;
|
---|
110 | Arg.cch = cchBuffer - 1;
|
---|
111 |
|
---|
112 | va_start(args, pszFormat);
|
---|
113 | cbRet = RTStrFormatV(strbufoutput, &Arg, NULL, NULL, pszFormat, args);
|
---|
114 | va_end(args);
|
---|
115 |
|
---|
116 | return cbRet;
|
---|
117 | }
|
---|
118 | RT_EXPORT_SYMBOL(RTStrPrintf);
|
---|
119 |
|
---|
120 |
|
---|
121 | RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
122 | {
|
---|
123 | STRBUFARG Arg;
|
---|
124 | AssertMsgReturn(cchBuffer, ("Excellent idea! Format a string with no space for the output!\n"), 0);
|
---|
125 | Arg.psz = pszBuffer;
|
---|
126 | Arg.cch = cchBuffer - 1;
|
---|
127 | return RTStrFormatV(strbufoutput, &Arg, pfnFormat, pvArg, pszFormat, args);
|
---|
128 | }
|
---|
129 | RT_EXPORT_SYMBOL(RTStrPrintfExV);
|
---|
130 |
|
---|
131 |
|
---|
132 | RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
133 | {
|
---|
134 | return RTStrPrintfExV(NULL, NULL, pszBuffer, cchBuffer, pszFormat, args);
|
---|
135 | }
|
---|
136 | RT_EXPORT_SYMBOL(RTStrPrintfV);
|
---|
137 |
|
---|
138 |
|
---|
139 | RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
|
---|
140 | {
|
---|
141 | va_list args;
|
---|
142 | size_t cbRet;
|
---|
143 | va_start(args, pszFormat);
|
---|
144 | cbRet = RTStrPrintfExV(pfnFormat, pvArg, pszBuffer, cchBuffer, pszFormat, args);
|
---|
145 | va_end(args);
|
---|
146 | return cbRet;
|
---|
147 | }
|
---|
148 | RT_EXPORT_SYMBOL(RTStrPrintfEx);
|
---|
149 |
|
---|