VirtualBox

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

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

move variables around to be able to compile this as .c (debug kernel module)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1/* $Id: strprintf.cpp 7942 2008-04-11 20:32:20Z 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 STRBUFARG Arg;
84
85 if (!cchBuffer)
86 {
87 AssertMsgFailed(("Excellent idea! Format a string with no space for the output!\n"));
88 return 0;
89 }
90
91 Arg.psz = pszBuffer;
92 Arg.cch = cchBuffer - 1;
93 return RTStrFormatV(strbufoutput, &Arg, pfnFormat, pvArg, pszFormat, args);
94}
95
96
97RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
98{
99 return RTStrPrintfExV(NULL, NULL, pszBuffer, cchBuffer, pszFormat, args);
100}
101
102RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
103{
104 va_list args;
105 size_t cbRet;
106 va_start(args, pszFormat);
107 cbRet = RTStrPrintfExV(pfnFormat, pvArg, pszBuffer, cchBuffer, pszFormat, args);
108 va_end(args);
109 return cbRet;
110}
111
112RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
113{
114 va_list args;
115 size_t cbRet;
116 va_start(args, pszFormat);
117 cbRet = RTStrPrintfV(pszBuffer, cchBuffer, pszFormat, args);
118 va_end(args);
119 return cbRet;
120}
121
Note: See TracBrowser for help on using the repository browser.

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