VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/nocrt-streams-win.cpp@ 98103

Last change on this file since 98103 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
1/* $Id: nocrt-streams-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - No-CRT - minimal stream implementation
4 */
5
6/*
7 * Copyright (C) 2006-2023 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 "internal/iprt.h"
42#include <iprt/stream.h>
43
44#include <iprt/nt/nt-and-windows.h>
45
46#include <iprt/ctype.h>
47#include <iprt/file.h>
48#include <iprt/string.h>
49
50
51
52/*********************************************************************************************************************************
53* Structures and Typedefs *
54*********************************************************************************************************************************/
55typedef struct PRINTFBUF
56{
57 HANDLE hNative;
58 size_t offBuf;
59 char szBuf[128];
60} PRINTFBUF;
61
62struct RTSTREAM
63{
64 int iStream;
65 HANDLE hNative;
66 RTFILE hFile;
67};
68
69
70/*********************************************************************************************************************************
71* Defined Constants And Macros *
72*********************************************************************************************************************************/
73#define MAKE_SURE_WE_HAVE_HFILE_RETURN(a_pStream) do { \
74 if ((a_pStream)->hFile != NIL_RTFILE) \
75 break; \
76 int rc = RTFileFromNative(&(a_pStream)->hFile, (uintptr_t)(a_pStream)->hNative); \
77 AssertRCReturn(rc, rc); \
78 } while (0)
79
80
81/*********************************************************************************************************************************
82* Global Variables *
83*********************************************************************************************************************************/
84RTSTREAM g_aStdStreams[3] =
85{
86 { 0, NULL, NIL_RTFILE },
87 { 1, NULL, NIL_RTFILE },
88 { 2, NULL, NIL_RTFILE },
89};
90
91RTSTREAM *g_pStdIn = &g_aStdStreams[0];
92RTSTREAM *g_pStdOut = &g_aStdStreams[1];
93RTSTREAM *g_pStdErr = &g_aStdStreams[2];
94
95
96
97DECLHIDDEN(void) InitStdHandles(PRTL_USER_PROCESS_PARAMETERS pParams)
98{
99 if (pParams)
100 {
101 g_pStdIn->hNative = pParams->StandardInput;
102 g_pStdOut->hNative = pParams->StandardOutput;
103 g_pStdErr->hNative = pParams->StandardError;
104 }
105}
106
107
108static void FlushPrintfBuffer(PRINTFBUF *pBuf)
109{
110 if (pBuf->offBuf)
111 {
112 DWORD cbWritten = 0;
113 WriteFile(pBuf->hNative, pBuf->szBuf, (DWORD)pBuf->offBuf, &cbWritten, NULL);
114 pBuf->offBuf = 0;
115 pBuf->szBuf[0] = '\0';
116 }
117}
118
119
120/** @callback_method_impl{FNRTSTROUTPUT} */
121static DECLCALLBACK(size_t) MyPrintfOutputter(void *pvArg, const char *pachChars, size_t cbChars)
122{
123 PRINTFBUF *pBuf = (PRINTFBUF *)pvArg;
124 if (cbChars != 0)
125 {
126 size_t offSrc = 0;
127 while (offSrc < cbChars)
128 {
129 size_t cbLeft = sizeof(pBuf->szBuf) - pBuf->offBuf - 1;
130 if (cbLeft > 0)
131 {
132 size_t cbToCopy = RT_MIN(cbChars - offSrc, cbLeft);
133 memcpy(&pBuf->szBuf[pBuf->offBuf], &pachChars[offSrc], cbToCopy);
134 pBuf->offBuf += cbToCopy;
135 pBuf->szBuf[pBuf->offBuf] = '\0';
136 if (cbLeft > cbToCopy)
137 break;
138 offSrc += cbToCopy;
139 }
140 FlushPrintfBuffer(pBuf);
141 }
142 }
143 else /* Special zero byte write at the end of the formatting. */
144 FlushPrintfBuffer(pBuf);
145 return cbChars;
146}
147
148
149RTR3DECL(int) RTStrmPrintfV(PRTSTREAM pStream, const char *pszFormat, va_list args)
150{
151 PRINTFBUF Buf;
152 Buf.hNative = pStream->hNative;
153 Buf.offBuf = 0;
154 Buf.szBuf[0] = '\0';
155
156 return (int)RTStrFormatV(MyPrintfOutputter, &Buf, NULL, NULL, pszFormat, args);
157}
158
159
160RTR3DECL(int) RTStrmPrintf(PRTSTREAM pStream, const char *pszFormat, ...)
161{
162 va_list args;
163 va_start(args, pszFormat);
164 int rc = RTStrmPrintfV(pStream, pszFormat, args);
165 va_end(args);
166 return rc;
167}
168
169
170RTR3DECL(int) RTPrintfV(const char *pszFormat, va_list va)
171{
172 PRINTFBUF Buf;
173 Buf.hNative = g_pStdOut->hNative;
174 Buf.offBuf = 0;
175 Buf.szBuf[0] = '\0';
176
177 return (int)RTStrFormatV(MyPrintfOutputter, &Buf, NULL, NULL, pszFormat, va);
178}
179
180
181RTR3DECL(int) RTPrintf(const char *pszFormat, ...)
182{
183 va_list va;
184 va_start(va, pszFormat);
185 int rc = RTPrintfV(pszFormat, va);
186 va_end(va);
187 return rc;
188}
189
190#ifndef IPRT_MINIMAL_STREAM
191
192# if 0
193RTR3DECL(int) RTStrmReadEx(PRTSTREAM pStream, void *pvBuf, size_t cbToRead, size_t *pcbRead)
194{
195 MAKE_SURE_WE_HAVE_HFILE_RETURN(pStream);
196 return RTFileRead(pStream->hFile, pvBuf, cbToRead, pcbRead);
197}
198# endif
199
200
201RTR3DECL(int) RTStrmWriteEx(PRTSTREAM pStream, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
202{
203 MAKE_SURE_WE_HAVE_HFILE_RETURN(pStream);
204 return RTFileWrite(pStream->hFile, pvBuf, cbToWrite, pcbWritten);
205}
206
207
208RTR3DECL(int) RTStrmFlush(PRTSTREAM pStream)
209{
210 MAKE_SURE_WE_HAVE_HFILE_RETURN(pStream);
211 return RTFileFlush(pStream->hFile);
212}
213
214
215RTR3DECL(int) RTStrmSetMode(PRTSTREAM pStream, int fBinary, int fCurrentCodeSet)
216{
217 AssertReturn(fBinary != (int)false, VERR_NOT_IMPLEMENTED);
218 AssertReturn(fCurrentCodeSet <= (int)false, VERR_NOT_IMPLEMENTED);
219 RT_NOREF(pStream);
220 return VINF_SUCCESS;
221}
222
223#endif
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