VirtualBox

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

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

IPRT: PEM writer functions. bugref:9699

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1/* $Id: vfsprintf.cpp 84163 2020-05-06 15:31:33Z 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.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/vfs.h>
32
33#include <iprt/errcore.h>
34#include <iprt/string.h>
35
36
37/** Writes the buffer to the VFS file. */
38static void FlushPrintfBuffer(PVFSIOSTRMOUTBUF pBuf)
39{
40 if (pBuf->offBuf)
41 {
42 int rc = RTVfsIoStrmWrite(pBuf->hVfsIos, pBuf->szBuf, pBuf->offBuf, true /*fBlocking*/, NULL);
43 if (RT_FAILURE(rc))
44 pBuf->rc = rc;
45 pBuf->offBuf = 0;
46 pBuf->szBuf[0] = '\0';
47 }
48}
49
50
51/**
52 * @callback_method_impl{FNRTSTROUTPUT,
53 * For use with VFSIOSTRMOUTBUF.}
54 */
55RTDECL(size_t) RTVfsIoStrmStrOutputCallback(void *pvArg, const char *pachChars, size_t cbChars)
56{
57 PVFSIOSTRMOUTBUF pBuf = (PVFSIOSTRMOUTBUF)pvArg;
58 AssertReturn(pBuf->cbSelf == sizeof(*pBuf), 0);
59
60 if (cbChars != 0)
61 {
62 if (cbChars <= sizeof(pBuf->szBuf) * 3 / 2)
63 {
64 /*
65 * Small piece of output: Buffer it.
66 */
67 size_t offSrc = 0;
68 while (offSrc < cbChars)
69 {
70 size_t cbLeft = sizeof(pBuf->szBuf) - pBuf->offBuf - 1;
71 if (cbLeft > 0)
72 {
73 size_t cbToCopy = RT_MIN(cbChars - offSrc, cbLeft);
74 memcpy(&pBuf->szBuf[pBuf->offBuf], &pachChars[offSrc], cbToCopy);
75 pBuf->offBuf += cbToCopy;
76 pBuf->szBuf[pBuf->offBuf] = '\0';
77 if (cbLeft > cbToCopy)
78 break;
79 offSrc += cbToCopy;
80 }
81 FlushPrintfBuffer(pBuf);
82 }
83 }
84 else
85 {
86 /*
87 * Large chunk of output: Output it directly.
88 */
89 FlushPrintfBuffer(pBuf);
90
91 int rc = RTVfsIoStrmWrite(pBuf->hVfsIos, pachChars, cbChars, true /*fBlocking*/, NULL);
92 if (RT_FAILURE(rc))
93 pBuf->rc = rc;
94 }
95 }
96 else /* Special zero byte write at the end of the formatting. */
97 FlushPrintfBuffer(pBuf);
98 return cbChars;
99}
100
101
102RTDECL(ssize_t) RTVfsIoStrmPrintfV(RTVFSIOSTREAM hVfsIos, const char *pszFormat, va_list va)
103{
104 VFSIOSTRMOUTBUF Buf;
105 VFSIOSTRMOUTBUF_INIT(&Buf, hVfsIos);
106
107 size_t cchRet = RTStrFormatV(RTVfsIoStrmStrOutputCallback, &Buf, NULL, NULL, pszFormat, va);
108 if (RT_SUCCESS(Buf.rc))
109 return cchRet;
110 return Buf.rc;
111}
112
113
114RTDECL(ssize_t) RTVfsIoStrmPrintf(RTVFSIOSTREAM hVfsIos, const char *pszFormat, ...)
115{
116 va_list va;
117 va_start(va, pszFormat);
118 ssize_t cchRet = RTVfsIoStrmPrintfV(hVfsIos, pszFormat, va);
119 va_end(va);
120 return cchRet;
121}
122
123
124RTDECL(ssize_t) RTVfsFilePrintfV(RTVFSFILE hVfsFile, const char *pszFormat, va_list va)
125{
126 ssize_t cchRet;
127 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(hVfsFile);
128 if (hVfsIos != NIL_RTVFSIOSTREAM)
129 {
130 cchRet = RTVfsIoStrmPrintfV(hVfsIos, pszFormat, va);
131 RTVfsIoStrmRelease(hVfsIos);
132 }
133 else
134 cchRet = VERR_INVALID_HANDLE;
135 return cchRet;
136}
137
138
139RTDECL(ssize_t) RTVfsFilePrintf(RTVFSFILE hVfsFile, const char *pszFormat, ...)
140{
141 ssize_t cchRet;
142 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(hVfsFile);
143 if (hVfsIos != NIL_RTVFSIOSTREAM)
144 {
145 va_list va;
146 va_start(va, pszFormat);
147 cchRet = RTVfsIoStrmPrintfV(hVfsIos, pszFormat, va);
148 va_end(va);
149 RTVfsIoStrmRelease(hVfsIos);
150 }
151 else
152 cchRet = VERR_INVALID_HANDLE;
153 return cchRet;
154}
155
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