1 | /* $Id: strprintf.cpp 21337 2009-07-07 14:58:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - String Formatters.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include "internal/iprt.h"
|
---|
37 |
|
---|
38 | #include <iprt/assert.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Structures and Typedefs *
|
---|
43 | *******************************************************************************/
|
---|
44 | /** strbufoutput() argument structure. */
|
---|
45 | typedef struct STRBUFARG
|
---|
46 | {
|
---|
47 | /** Pointer to current buffer position. */
|
---|
48 | char *psz;
|
---|
49 | /** Number of bytes left in the buffer - not including the trailing zero. */
|
---|
50 | size_t cch;
|
---|
51 | } STRBUFARG;
|
---|
52 | /** Pointer to a strbufoutput() argument structure. */
|
---|
53 | typedef STRBUFARG *PSTRBUFARG;
|
---|
54 |
|
---|
55 |
|
---|
56 | /*******************************************************************************
|
---|
57 | * Internal Functions *
|
---|
58 | *******************************************************************************/
|
---|
59 | static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars);
|
---|
60 |
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Output callback.
|
---|
64 | *
|
---|
65 | * @returns number of bytes written.
|
---|
66 | * @param pvArg Pointer to a STRBUFARG structure.
|
---|
67 | * @param pachChars Pointer to an array of utf-8 characters.
|
---|
68 | * @param cbChars Number of bytes in the character array pointed to by pachChars.
|
---|
69 | */
|
---|
70 | static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars)
|
---|
71 | {
|
---|
72 | PSTRBUFARG pArg = (PSTRBUFARG)pvArg;
|
---|
73 |
|
---|
74 | cbChars = RT_MIN(pArg->cch, cbChars);
|
---|
75 | if (cbChars)
|
---|
76 | {
|
---|
77 | memcpy(pArg->psz, pachChars, cbChars);
|
---|
78 | pArg->cch -= cbChars;
|
---|
79 | pArg->psz += cbChars;
|
---|
80 | }
|
---|
81 | *pArg->psz = '\0';
|
---|
82 |
|
---|
83 | return cbChars;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
88 | {
|
---|
89 | STRBUFARG Arg;
|
---|
90 |
|
---|
91 | if (!cchBuffer)
|
---|
92 | {
|
---|
93 | AssertMsgFailed(("Excellent idea! Format a string with no space for the output!\n"));
|
---|
94 | return 0;
|
---|
95 | }
|
---|
96 |
|
---|
97 | Arg.psz = pszBuffer;
|
---|
98 | Arg.cch = cchBuffer - 1;
|
---|
99 | return RTStrFormatV(strbufoutput, &Arg, pfnFormat, pvArg, pszFormat, args);
|
---|
100 | }
|
---|
101 | RT_EXPORT_SYMBOL(RTStrPrintfExV);
|
---|
102 |
|
---|
103 |
|
---|
104 | RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
105 | {
|
---|
106 | return RTStrPrintfExV(NULL, NULL, pszBuffer, cchBuffer, pszFormat, args);
|
---|
107 | }
|
---|
108 | RT_EXPORT_SYMBOL(RTStrPrintfV);
|
---|
109 |
|
---|
110 |
|
---|
111 | RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
|
---|
112 | {
|
---|
113 | va_list args;
|
---|
114 | size_t cbRet;
|
---|
115 | va_start(args, pszFormat);
|
---|
116 | cbRet = RTStrPrintfExV(pfnFormat, pvArg, pszBuffer, cchBuffer, pszFormat, args);
|
---|
117 | va_end(args);
|
---|
118 | return cbRet;
|
---|
119 | }
|
---|
120 | RT_EXPORT_SYMBOL(RTStrPrintfEx);
|
---|
121 |
|
---|
122 |
|
---|
123 | RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
|
---|
124 | {
|
---|
125 | va_list args;
|
---|
126 | size_t cbRet;
|
---|
127 | va_start(args, pszFormat);
|
---|
128 | cbRet = RTStrPrintfV(pszBuffer, cchBuffer, pszFormat, args);
|
---|
129 | va_end(args);
|
---|
130 | return cbRet;
|
---|
131 | }
|
---|
132 | RT_EXPORT_SYMBOL(RTStrPrintf);
|
---|
133 |
|
---|