]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - ubuntu/vbox/vboxguest/common/string/strprintf.c
1 /* $Id: strprintf.cpp $ */
3 * IPRT - String Formatters.
7 * Copyright (C) 2006-2016 Oracle Corporation
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.
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.
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.
28 /*********************************************************************************************************************************
30 *********************************************************************************************************************************/
31 #include <iprt/string.h>
32 #include "internal/iprt.h"
34 #include <iprt/assert.h>
37 /*********************************************************************************************************************************
38 * Structures and Typedefs *
39 *********************************************************************************************************************************/
40 /** strbufoutput() argument structure. */
41 typedef struct STRBUFARG
43 /** Pointer to current buffer position. */
45 /** Number of bytes left in the buffer - not including the trailing zero. */
48 /** Pointer to a strbufoutput() argument structure. */
49 typedef STRBUFARG
*PSTRBUFARG
;
52 /*********************************************************************************************************************************
53 * Internal Functions *
54 *********************************************************************************************************************************/
55 static DECLCALLBACK(size_t) strbufoutput(void *pvArg
, const char *pachChars
, size_t cbChars
);
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.
66 static DECLCALLBACK(size_t) strbufoutput(void *pvArg
, const char *pachChars
, size_t cbChars
)
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. */
71 cbChars
= RT_MIN(pArg
->cch
, cbChars
);
76 /* Note! For VS2010/64 we need at least 7 case statements before it generates a jump table. */
80 memcpy(pszCur
, pachChars
, cbChars
);
82 case 8: pszCur
[7] = pachChars
[7]; /* fall thru */
83 case 7: pszCur
[6] = pachChars
[6]; /* fall thru */
84 case 6: pszCur
[5] = pachChars
[5]; /* fall thru */
85 case 5: pszCur
[4] = pachChars
[4]; /* fall thru */
86 case 4: pszCur
[3] = pachChars
[3]; /* fall thru */
87 case 3: pszCur
[2] = pachChars
[2]; /* fall thru */
88 case 2: pszCur
[1] = pachChars
[1]; /* fall thru */
89 case 1: pszCur
[0] = pachChars
[0]; /* fall thru */
93 pArg
->psz
= pszCur
+= cbChars
;
101 RTDECL(size_t) RTStrPrintf(char *pszBuffer
, size_t cchBuffer
, const char *pszFormat
, ...)
103 /* Explicitly inline RTStrPrintfV + RTStrPrintfExV here because this is a frequently use API. */
108 AssertMsgReturn(cchBuffer
, ("Excellent idea! Format a string with no space for the output!\n"), 0);
110 Arg
.cch
= cchBuffer
- 1;
112 va_start(args
, pszFormat
);
113 cbRet
= RTStrFormatV(strbufoutput
, &Arg
, NULL
, NULL
, pszFormat
, args
);
118 RT_EXPORT_SYMBOL(RTStrPrintf
);
121 RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat
, void *pvArg
, char *pszBuffer
, size_t cchBuffer
, const char *pszFormat
, va_list args
)
124 AssertMsgReturn(cchBuffer
, ("Excellent idea! Format a string with no space for the output!\n"), 0);
126 Arg
.cch
= cchBuffer
- 1;
127 return RTStrFormatV(strbufoutput
, &Arg
, pfnFormat
, pvArg
, pszFormat
, args
);
129 RT_EXPORT_SYMBOL(RTStrPrintfExV
);
132 RTDECL(size_t) RTStrPrintfV(char *pszBuffer
, size_t cchBuffer
, const char *pszFormat
, va_list args
)
134 return RTStrPrintfExV(NULL
, NULL
, pszBuffer
, cchBuffer
, pszFormat
, args
);
136 RT_EXPORT_SYMBOL(RTStrPrintfV
);
139 RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat
, void *pvArg
, char *pszBuffer
, size_t cchBuffer
, const char *pszFormat
, ...)
143 va_start(args
, pszFormat
);
144 cbRet
= RTStrPrintfExV(pfnFormat
, pvArg
, pszBuffer
, cchBuffer
, pszFormat
, args
);
148 RT_EXPORT_SYMBOL(RTStrPrintfEx
);