VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/strprintf.cpp@ 9230

Last change on this file since 9230 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1/* $Id: strprintf.cpp 8245 2008-04-21 17:24:28Z 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 <iprt/assert.h>
37
38
39/*******************************************************************************
40* Structures and Typedefs *
41*******************************************************************************/
42/** strbufoutput() argument structure. */
43typedef struct STRBUFARG
44{
45 /** Pointer to current buffer position. */
46 char *psz;
47 /** Number of bytes left in the buffer - not including the trailing zero. */
48 size_t cch;
49} STRBUFARG;
50/** Pointer to a strbufoutput() argument structure. */
51typedef STRBUFARG *PSTRBUFARG;
52
53
54/*******************************************************************************
55* Internal Functions *
56*******************************************************************************/
57static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars);
58
59
60/**
61 * Output callback.
62 *
63 * @returns number of bytes written.
64 * @param pvArg Pointer to a STRBUFARG structure.
65 * @param pachChars Pointer to an array of utf-8 characters.
66 * @param cbChars Number of bytes in the character array pointed to by pachChars.
67 */
68static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars)
69{
70 PSTRBUFARG pArg = (PSTRBUFARG)pvArg;
71
72 cbChars = RT_MIN(pArg->cch, cbChars);
73 if (cbChars)
74 {
75 memcpy(pArg->psz, pachChars, cbChars);
76 pArg->cch -= cbChars;
77 pArg->psz += cbChars;
78 }
79 *pArg->psz = '\0';
80
81 return cbChars;
82}
83
84
85RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
86{
87 STRBUFARG Arg;
88
89 if (!cchBuffer)
90 {
91 AssertMsgFailed(("Excellent idea! Format a string with no space for the output!\n"));
92 return 0;
93 }
94
95 Arg.psz = pszBuffer;
96 Arg.cch = cchBuffer - 1;
97 return RTStrFormatV(strbufoutput, &Arg, pfnFormat, pvArg, pszFormat, args);
98}
99
100
101RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
102{
103 return RTStrPrintfExV(NULL, NULL, pszBuffer, cchBuffer, pszFormat, args);
104}
105
106RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
107{
108 va_list args;
109 size_t cbRet;
110 va_start(args, pszFormat);
111 cbRet = RTStrPrintfExV(pfnFormat, pvArg, pszBuffer, cchBuffer, pszFormat, args);
112 va_end(args);
113 return cbRet;
114}
115
116RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
117{
118 va_list args;
119 size_t cbRet;
120 va_start(args, pszFormat);
121 cbRet = RTStrPrintfV(pszBuffer, cchBuffer, pszFormat, args);
122 va_end(args);
123 return cbRet;
124}
125
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette