VirtualBox

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

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* $Id: vfsprintf.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - Virtual File System, File Printf.
4 */
5
6/*
7 * Copyright (C) 2010-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/vfs.h>
42
43#include <iprt/errcore.h>
44#include <iprt/string.h>
45
46
47/** Writes the buffer to the VFS file. */
48static void FlushPrintfBuffer(PVFSIOSTRMOUTBUF pBuf)
49{
50 if (pBuf->offBuf)
51 {
52 int rc = RTVfsIoStrmWrite(pBuf->hVfsIos, pBuf->szBuf, pBuf->offBuf, true /*fBlocking*/, NULL);
53 if (RT_FAILURE(rc))
54 pBuf->rc = rc;
55 pBuf->offBuf = 0;
56 pBuf->szBuf[0] = '\0';
57 }
58}
59
60
61/**
62 * @callback_method_impl{FNRTSTROUTPUT,
63 * For use with VFSIOSTRMOUTBUF.}
64 */
65RTDECL(size_t) RTVfsIoStrmStrOutputCallback(void *pvArg, const char *pachChars, size_t cbChars)
66{
67 PVFSIOSTRMOUTBUF pBuf = (PVFSIOSTRMOUTBUF)pvArg;
68 AssertReturn(pBuf->cbSelf == sizeof(*pBuf), 0);
69
70 if (cbChars != 0)
71 {
72 if (cbChars <= sizeof(pBuf->szBuf) * 3 / 2)
73 {
74 /*
75 * Small piece of output: Buffer it.
76 */
77 size_t offSrc = 0;
78 while (offSrc < cbChars)
79 {
80 size_t cbLeft = sizeof(pBuf->szBuf) - pBuf->offBuf - 1;
81 if (cbLeft > 0)
82 {
83 size_t cbToCopy = RT_MIN(cbChars - offSrc, cbLeft);
84 memcpy(&pBuf->szBuf[pBuf->offBuf], &pachChars[offSrc], cbToCopy);
85 pBuf->offBuf += cbToCopy;
86 pBuf->szBuf[pBuf->offBuf] = '\0';
87 if (cbLeft > cbToCopy)
88 break;
89 offSrc += cbToCopy;
90 }
91 FlushPrintfBuffer(pBuf);
92 }
93 }
94 else
95 {
96 /*
97 * Large chunk of output: Output it directly.
98 */
99 FlushPrintfBuffer(pBuf);
100
101 int rc = RTVfsIoStrmWrite(pBuf->hVfsIos, pachChars, cbChars, true /*fBlocking*/, NULL);
102 if (RT_FAILURE(rc))
103 pBuf->rc = rc;
104 }
105 }
106 else /* Special zero byte write at the end of the formatting. */
107 FlushPrintfBuffer(pBuf);
108 return cbChars;
109}
110
111
112RTDECL(ssize_t) RTVfsIoStrmPrintfV(RTVFSIOSTREAM hVfsIos, const char *pszFormat, va_list va)
113{
114 VFSIOSTRMOUTBUF Buf;
115 VFSIOSTRMOUTBUF_INIT(&Buf, hVfsIos);
116
117 size_t cchRet = RTStrFormatV(RTVfsIoStrmStrOutputCallback, &Buf, NULL, NULL, pszFormat, va);
118 if (RT_SUCCESS(Buf.rc))
119 return cchRet;
120 return Buf.rc;
121}
122
123
124RTDECL(ssize_t) RTVfsIoStrmPrintf(RTVFSIOSTREAM hVfsIos, const char *pszFormat, ...)
125{
126 va_list va;
127 va_start(va, pszFormat);
128 ssize_t cchRet = RTVfsIoStrmPrintfV(hVfsIos, pszFormat, va);
129 va_end(va);
130 return cchRet;
131}
132
133
134RTDECL(ssize_t) RTVfsFilePrintfV(RTVFSFILE hVfsFile, const char *pszFormat, va_list va)
135{
136 ssize_t cchRet;
137 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(hVfsFile);
138 if (hVfsIos != NIL_RTVFSIOSTREAM)
139 {
140 cchRet = RTVfsIoStrmPrintfV(hVfsIos, pszFormat, va);
141 RTVfsIoStrmRelease(hVfsIos);
142 }
143 else
144 cchRet = VERR_INVALID_HANDLE;
145 return cchRet;
146}
147
148
149RTDECL(ssize_t) RTVfsFilePrintf(RTVFSFILE hVfsFile, const char *pszFormat, ...)
150{
151 ssize_t cchRet;
152 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(hVfsFile);
153 if (hVfsIos != NIL_RTVFSIOSTREAM)
154 {
155 va_list va;
156 va_start(va, pszFormat);
157 cchRet = RTVfsIoStrmPrintfV(hVfsIos, pszFormat, va);
158 va_end(va);
159 RTVfsIoStrmRelease(hVfsIos);
160 }
161 else
162 cchRet = VERR_INVALID_HANDLE;
163 return cchRet;
164}
165
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