1 | /* $Id: strprintf.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - String Formatters.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <iprt/string.h>
|
---|
23 | #include <iprt/assert.h>
|
---|
24 |
|
---|
25 |
|
---|
26 | /*******************************************************************************
|
---|
27 | * Structures and Typedefs *
|
---|
28 | *******************************************************************************/
|
---|
29 | /** strbufoutput() argument structure. */
|
---|
30 | typedef struct STRBUFARG
|
---|
31 | {
|
---|
32 | /** Pointer to current buffer position. */
|
---|
33 | char *psz;
|
---|
34 | /** Number of bytes left in the buffer - not including the trailing zero. */
|
---|
35 | size_t cch;
|
---|
36 | } STRBUFARG;
|
---|
37 | /** Pointer to a strbufoutput() argument structure. */
|
---|
38 | typedef STRBUFARG *PSTRBUFARG;
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Internal Functions *
|
---|
43 | *******************************************************************************/
|
---|
44 | static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars);
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Output callback.
|
---|
49 | *
|
---|
50 | * @returns number of bytes written.
|
---|
51 | * @param pvArg Pointer to a STRBUFARG structure.
|
---|
52 | * @param pachChars Pointer to an array of utf-8 characters.
|
---|
53 | * @param cbChars Number of bytes in the character array pointed to by pachChars.
|
---|
54 | */
|
---|
55 | static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars)
|
---|
56 | {
|
---|
57 | PSTRBUFARG pArg = (PSTRBUFARG)pvArg;
|
---|
58 |
|
---|
59 | cbChars = RT_MIN(pArg->cch, cbChars);
|
---|
60 | if (cbChars)
|
---|
61 | {
|
---|
62 | memcpy(pArg->psz, pachChars, cbChars);
|
---|
63 | pArg->cch -= cbChars;
|
---|
64 | pArg->psz += cbChars;
|
---|
65 | }
|
---|
66 | *pArg->psz = '\0';
|
---|
67 |
|
---|
68 | return cbChars;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
73 | {
|
---|
74 | if (!cchBuffer)
|
---|
75 | {
|
---|
76 | AssertMsgFailed(("Excellent idea! Format a string with no space for the output!\n"));
|
---|
77 | return 0;
|
---|
78 | }
|
---|
79 |
|
---|
80 | STRBUFARG Arg;
|
---|
81 | Arg.psz = pszBuffer;
|
---|
82 | Arg.cch = cchBuffer - 1;
|
---|
83 | return RTStrFormatV(strbufoutput, &Arg, pfnFormat, pvArg, pszFormat, args);
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
|
---|
88 | {
|
---|
89 | return RTStrPrintfExV(NULL, NULL, pszBuffer, cchBuffer, pszFormat, args);
|
---|
90 | }
|
---|
91 |
|
---|
92 | RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
|
---|
93 | {
|
---|
94 | va_list args;
|
---|
95 | va_start(args, pszFormat);
|
---|
96 | size_t cbRet = RTStrPrintfExV(pfnFormat, pvArg, pszBuffer, cchBuffer, pszFormat, args);
|
---|
97 | va_end(args);
|
---|
98 | return cbRet;
|
---|
99 | }
|
---|
100 |
|
---|
101 | RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
|
---|
102 | {
|
---|
103 | va_list args;
|
---|
104 | va_start(args, pszFormat);
|
---|
105 | size_t cbRet = RTStrPrintfV(pszBuffer, cchBuffer, pszFormat, args);
|
---|
106 | va_end(args);
|
---|
107 | return cbRet;
|
---|
108 | }
|
---|
109 |
|
---|