1 | /* $Id: strprintf.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - String Formatters.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 |
|
---|
70 | cbChars = RT_MIN(pArg->cch, cbChars);
|
---|
71 | if (cbChars)
|
---|
72 | {
|
---|
73 | memcpy(pArg->psz, pachChars, cbChars);
|
---|
74 | pArg->cch -= cbChars;
|
---|
75 | pArg->psz += cbChars;
|
---|
76 | }
|
---|
77 | *pArg->psz = '\0';
|
---|
78 |
|
---|
79 | return cbChars;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
84 | {
|
---|
85 | STRBUFARG Arg;
|
---|
86 |
|
---|
87 | if (!cchBuffer)
|
---|
88 | {
|
---|
89 | AssertMsgFailed(("Excellent idea! Format a string with no space for the output!\n"));
|
---|
90 | return 0;
|
---|
91 | }
|
---|
92 |
|
---|
93 | Arg.psz = pszBuffer;
|
---|
94 | Arg.cch = cchBuffer - 1;
|
---|
95 | return RTStrFormatV(strbufoutput, &Arg, pfnFormat, pvArg, pszFormat, args);
|
---|
96 | }
|
---|
97 | RT_EXPORT_SYMBOL(RTStrPrintfExV);
|
---|
98 |
|
---|
99 |
|
---|
100 | RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
101 | {
|
---|
102 | return RTStrPrintfExV(NULL, NULL, pszBuffer, cchBuffer, pszFormat, args);
|
---|
103 | }
|
---|
104 | RT_EXPORT_SYMBOL(RTStrPrintfV);
|
---|
105 |
|
---|
106 |
|
---|
107 | RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
|
---|
108 | {
|
---|
109 | va_list args;
|
---|
110 | size_t cbRet;
|
---|
111 | va_start(args, pszFormat);
|
---|
112 | cbRet = RTStrPrintfExV(pfnFormat, pvArg, pszBuffer, cchBuffer, pszFormat, args);
|
---|
113 | va_end(args);
|
---|
114 | return cbRet;
|
---|
115 | }
|
---|
116 | RT_EXPORT_SYMBOL(RTStrPrintfEx);
|
---|
117 |
|
---|
118 |
|
---|
119 | RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
|
---|
120 | {
|
---|
121 | va_list args;
|
---|
122 | size_t cbRet;
|
---|
123 | va_start(args, pszFormat);
|
---|
124 | cbRet = RTStrPrintfV(pszBuffer, cchBuffer, pszFormat, args);
|
---|
125 | va_end(args);
|
---|
126 | return cbRet;
|
---|
127 | }
|
---|
128 | RT_EXPORT_SYMBOL(RTStrPrintf);
|
---|
129 |
|
---|