VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/vfs/vfsprintf.cpp@ 84146

Last change on this file since 84146 was 84146, checked in by vboxsync, 5 years ago

IPRT: Added RTVfsFilePrintf, RTVfsFilePrintfV, RTVfsIoStrmPrintf and RTVfsIoStrmPrintfV. bugref:9699

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1/* $Id: vfsprintf.cpp 84146 2020-05-05 15:10:47Z vboxsync $ */
2/** @file
3 * IPRT - Virtual File System, File Printf.
4 */
5
6/*
7 * Copyright (C) 2010-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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/vfs.h>
32
33#include <iprt/err.h>
34#include <iprt/mem.h>
35#include <iprt/string.h>
36
37
38/*********************************************************************************************************************************
39* Structures and Typedefs *
40*********************************************************************************************************************************/
41typedef struct PRINTFBUF
42{
43 RTVFSIOSTREAM hVfsIos;
44 int rc;
45 size_t offBuf;
46 char szBuf[256];
47} PRINTFBUF;
48
49
50/** Writes the buffer to the VFS file. */
51static void FlushPrintfBuffer(PRINTFBUF *pBuf)
52{
53 if (pBuf->offBuf)
54 {
55 int rc = RTVfsIoStrmWrite(pBuf->hVfsIos, pBuf->szBuf, pBuf->offBuf, true /*fBlocking*/, NULL);
56 if (RT_FAILURE(rc))
57 pBuf->rc = rc;
58 pBuf->offBuf = 0;
59 pBuf->szBuf[0] = '\0';
60 }
61}
62
63
64/** @callback_method_impl{FNRTSTROUTPUT} */
65static DECLCALLBACK(size_t) MyPrintfOutputter(void *pvArg, const char *pachChars, size_t cbChars)
66{
67 PRINTFBUF *pBuf = (PRINTFBUF *)pvArg;
68 if (cbChars != 0)
69 {
70 size_t offSrc = 0;
71 while (offSrc < cbChars)
72 {
73 size_t cbLeft = sizeof(pBuf->szBuf) - pBuf->offBuf - 1;
74 if (cbLeft > 0)
75 {
76 size_t cbToCopy = RT_MIN(cbChars - offSrc, cbLeft);
77 memcpy(&pBuf->szBuf[pBuf->offBuf], &pachChars[offSrc], cbToCopy);
78 pBuf->offBuf += cbToCopy;
79 pBuf->szBuf[pBuf->offBuf] = '\0';
80 if (cbLeft > cbToCopy)
81 break;
82 offSrc += cbToCopy;
83 }
84 FlushPrintfBuffer(pBuf);
85 }
86 }
87 else /* Special zero byte write at the end of the formatting. */
88 FlushPrintfBuffer(pBuf);
89 return cbChars;
90}
91
92
93
94RTDECL(ssize_t) RTVfsIoStrmPrintfV(RTVFSIOSTREAM hVfsIos, const char *pszFormat, va_list va)
95{
96 PRINTFBUF Buf;
97 Buf.hVfsIos = hVfsIos;
98 Buf.rc = VINF_SUCCESS;
99 Buf.offBuf = 0;
100 Buf.szBuf[0] = '\0';
101
102 size_t cchRet = RTStrFormatV(MyPrintfOutputter, &Buf, NULL, NULL, pszFormat, va);
103 if (RT_SUCCESS(Buf.rc))
104 return cchRet;
105 return Buf.rc;
106}
107
108
109RTDECL(ssize_t) RTVfsIoStrmPrintf(RTVFSIOSTREAM hVfsIos, const char *pszFormat, ...)
110{
111 va_list va;
112 va_start(va, pszFormat);
113 ssize_t cchRet = RTVfsIoStrmPrintfV(hVfsIos, pszFormat, va);
114 va_end(va);
115 return cchRet;
116}
117
118
119RTDECL(ssize_t) RTVfsFilePrintfV(RTVFSFILE hVfsFile, const char *pszFormat, va_list va)
120{
121 ssize_t cchRet;
122 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(hVfsFile);
123 if (hVfsIos != NIL_RTVFSIOSTREAM)
124 {
125 cchRet = RTVfsIoStrmPrintfV(hVfsIos, pszFormat, va);
126 RTVfsIoStrmRelease(hVfsIos);
127 }
128 else
129 cchRet = VERR_INVALID_HANDLE;
130 return cchRet;
131}
132
133
134RTDECL(ssize_t) RTVfsFilePrintf(RTVFSFILE hVfsFile, const char *pszFormat, ...)
135{
136 ssize_t cchRet;
137 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(hVfsFile);
138 if (hVfsIos != NIL_RTVFSIOSTREAM)
139 {
140 va_list va;
141 va_start(va, pszFormat);
142 cchRet = RTVfsIoStrmPrintfV(hVfsIos, pszFormat, va);
143 va_end(va);
144 RTVfsIoStrmRelease(hVfsIos);
145 }
146 else
147 cchRet = VERR_INVALID_HANDLE;
148 return cchRet;
149}
150
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