VirtualBox

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

Last change on this file since 7240 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

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