1 | /* $Id: bs3-cmn-StrPrintf.c 62484 2016-07-22 18:35:33Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - Bs3StrPrintf, Bs3StrPrintfV
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2016 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 "bs3kit-template-header.h"
|
---|
32 | #include <iprt/ctype.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | /*********************************************************************************************************************************
|
---|
36 | * Structures and Typedefs *
|
---|
37 | *********************************************************************************************************************************/
|
---|
38 | typedef struct BS3STRPRINTFSTATE
|
---|
39 | {
|
---|
40 | /** Current buffer position. */
|
---|
41 | char *pchBuf;
|
---|
42 | /** Number of bytes left in the buffer. */
|
---|
43 | size_t cchLeft;
|
---|
44 | } BS3STRPRINTFSTATE;
|
---|
45 | typedef BS3STRPRINTFSTATE BS3_FAR *PBS3STRPRINTFSTATE;
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 | static BS3_DECL_CALLBACK(size_t) bs3StrPrintfFmtOutput(char ch, void BS3_FAR *pvUser)
|
---|
50 | {
|
---|
51 | PBS3STRPRINTFSTATE pState = (PBS3STRPRINTFSTATE)pvUser;
|
---|
52 | if (ch)
|
---|
53 | {
|
---|
54 | /* Put to the buffer if there is place for this char and a terminator. */
|
---|
55 | if (pState->cchLeft > 1)
|
---|
56 | {
|
---|
57 | pState->cchLeft--;
|
---|
58 | *pState->pchBuf++ = ch;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /* Always return 1. */
|
---|
62 | return 1;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* Terminate the string. */
|
---|
66 | if (pState->cchLeft)
|
---|
67 | {
|
---|
68 | pState->cchLeft--;
|
---|
69 | *pState->pchBuf++ = '\0';
|
---|
70 | }
|
---|
71 | return 0;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | #undef Bs3StrPrintfV
|
---|
76 | BS3_CMN_DEF(size_t, Bs3StrPrintfV,(char BS3_FAR *pszBuf, size_t cchBuf, const char BS3_FAR *pszFormat, va_list va))
|
---|
77 | {
|
---|
78 | BS3STRPRINTFSTATE State;
|
---|
79 | State.pchBuf = pszBuf;
|
---|
80 | State.cchLeft = cchBuf;
|
---|
81 | return Bs3StrFormatV(pszFormat, va, bs3StrPrintfFmtOutput, &State);
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | #undef Bs3StrPrintf
|
---|
86 | BS3_CMN_DEF(size_t, Bs3StrPrintf,(char BS3_FAR *pszBuf, size_t cchBuf, const char BS3_FAR *pszFormat, ...))
|
---|
87 | {
|
---|
88 | size_t cchRet;
|
---|
89 | va_list va;
|
---|
90 | va_start(va, pszFormat);
|
---|
91 | cchRet = BS3_CMN_NM(Bs3StrPrintfV)(pszBuf, cchBuf, pszFormat, va);
|
---|
92 | va_end(va);
|
---|
93 | return cchRet;
|
---|
94 | }
|
---|
95 |
|
---|