VirtualBox

source: vbox/trunk/src/VBox/Runtime/strprintf.cpp@ 2013

Last change on this file since 2013 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1/* $Id: strprintf.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - String Formatters.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung 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 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include <iprt/string.h>
27#include <iprt/assert.h>
28
29
30/*******************************************************************************
31* Structures and Typedefs *
32*******************************************************************************/
33/** strbufoutput() argument structure. */
34typedef struct STRBUFARG
35{
36 /** Pointer to current buffer position. */
37 char *psz;
38 /** Number of bytes left in the buffer - not including the trailing zero. */
39 size_t cch;
40} STRBUFARG;
41/** Pointer to a strbufoutput() argument structure. */
42typedef STRBUFARG *PSTRBUFARG;
43
44
45/*******************************************************************************
46* Internal Functions *
47*******************************************************************************/
48static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars);
49
50
51/**
52 * Output callback.
53 *
54 * @returns number of bytes written.
55 * @param pvArg Pointer to a STRBUFARG structure.
56 * @param pachChars Pointer to an array of utf-8 characters.
57 * @param cbChars Number of bytes in the character array pointed to by pachChars.
58 */
59static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars)
60{
61 PSTRBUFARG pArg = (PSTRBUFARG)pvArg;
62
63 cbChars = RT_MIN(pArg->cch, cbChars);
64 if (cbChars)
65 {
66 memcpy(pArg->psz, pachChars, cbChars);
67 pArg->cch -= cbChars;
68 pArg->psz += cbChars;
69 }
70 *pArg->psz = '\0';
71
72 return cbChars;
73}
74
75
76RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
77{
78 if (!cchBuffer)
79 {
80 AssertMsgFailed(("Excellent idea! Format a string with no space for the output!\n"));
81 return 0;
82 }
83
84 STRBUFARG Arg;
85 Arg.psz = pszBuffer;
86 Arg.cch = cchBuffer - 1;
87 return RTStrFormatV(strbufoutput, &Arg, pfnFormat, pvArg, pszFormat, args);
88}
89
90
91RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)
92{
93 return RTStrPrintfExV(NULL, NULL, pszBuffer, cchBuffer, pszFormat, args);
94}
95
96RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
97{
98 va_list args;
99 va_start(args, pszFormat);
100 size_t cbRet = RTStrPrintfExV(pfnFormat, pvArg, pszBuffer, cchBuffer, pszFormat, args);
101 va_end(args);
102 return cbRet;
103}
104
105RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)
106{
107 va_list args;
108 va_start(args, pszFormat);
109 size_t cbRet = RTStrPrintfV(pszBuffer, cchBuffer, pszFormat, args);
110 va_end(args);
111 return cbRet;
112}
113
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