VirtualBox

source: vbox/trunk/include/iprt/stream.h@ 96316

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

IPRT/RTStream: Added RTStrmSeek, RTStrmTell, RTStrmSetBufferingMode and RTStrmQueryFileHandle. bugref:10261

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.7 KB
Line 
1/** @file
2 * IPRT - I/O Stream.
3 */
4
5/*
6 * Copyright (C) 2006-2022 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef IPRT_INCLUDED_stream_h
27#define IPRT_INCLUDED_stream_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/cdefs.h>
33#include <iprt/types.h>
34#include <iprt/stdarg.h>
35
36RT_C_DECLS_BEGIN
37
38/** @defgroup grp_rt_stream RTStrm - File Streams
39 * @ingroup grp_rt
40 * @{
41 */
42
43#ifndef IPRT_INCLUDED_message_h
44/** Pointer to a stream. */
45typedef struct RTSTREAM *PRTSTREAM;
46#endif
47
48/** Pointer to the standard input stream. */
49extern RTDATADECL(PRTSTREAM) g_pStdIn;
50
51/** Pointer to the standard error stream. */
52extern RTDATADECL(PRTSTREAM) g_pStdErr;
53
54/** Pointer to the standard output stream. */
55extern RTDATADECL(PRTSTREAM) g_pStdOut;
56
57
58/**
59 * Opens a file stream.
60 *
61 * @returns iprt status code.
62 * @param pszFilename Path to the file to open.
63 * @param pszMode The open mode. See fopen() standard.
64 * Format: <a|r|w>[+][b|t][x][e|N|E]
65 * - 'a': Open or create file and writes
66 * append tos it.
67 * - 'r': Open existing file and read from it.
68 * - 'w': Open or truncate existing file and write
69 * to it.
70 * - '+': Open for both read and write access.
71 * - 'b' / 't': binary / text
72 * - 'x': exclusively create, no open. Only
73 * possible with 'w'.
74 * - 'e' / 'N': No inherit on exec. (The 'e' is
75 * how Linux and FreeBSD expresses this, the
76 * latter is Visual C++).
77 * @param ppStream Where to store the opened stream.
78 */
79RTR3DECL(int) RTStrmOpen(const char *pszFilename, const char *pszMode, PRTSTREAM *ppStream);
80
81/**
82 * Opens a file stream.
83 *
84 * @returns iprt status code.
85 * @param pszMode The open mode. See fopen() standard.
86 * Format: <a|r|w>[+][b|t][x][e|N|E]
87 * - 'a': Open or create file and writes
88 * append tos it.
89 * - 'r': Open existing file and read from it.
90 * - 'w': Open or truncate existing file and write
91 * to it.
92 * - '+': Open for both read and write access.
93 * - 'b' / 't': binary / text
94 * - 'x': exclusively create, no open. Only
95 * possible with 'w'.
96 * - 'e' / 'N': No inherit on exec. (The 'e' is
97 * how Linux and FreeBSD expresses this, the
98 * latter is Visual C++).
99 * @param ppStream Where to store the opened stream.
100 * @param pszFilenameFmt Filename path format string.
101 * @param args Arguments to the format string.
102 */
103RTR3DECL(int) RTStrmOpenFV(const char *pszMode, PRTSTREAM *ppStream, const char *pszFilenameFmt,
104 va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
105
106/**
107 * Opens a file stream.
108 *
109 * @returns iprt status code.
110 * @param pszMode The open mode. See fopen() standard.
111 * Format: <a|r|w>[+][b|t][x][e|N|E]
112 * - 'a': Open or create file and writes
113 * append tos it.
114 * - 'r': Open existing file and read from it.
115 * - 'w': Open or truncate existing file and write
116 * to it.
117 * - '+': Open for both read and write access.
118 * - 'b' / 't': binary / text
119 * - 'x': exclusively create, no open. Only
120 * possible with 'w'.
121 * - 'e' / 'N': No inherit on exec. (The 'e' is
122 * how Linux and FreeBSD expresses this, the
123 * latter is Visual C++).
124 * @param ppStream Where to store the opened stream.
125 * @param pszFilenameFmt Filename path format string.
126 * @param ... Arguments to the format string.
127 */
128RTR3DECL(int) RTStrmOpenF(const char *pszMode, PRTSTREAM *ppStream, const char *pszFilenameFmt, ...) RT_IPRT_FORMAT_ATTR(3, 4);
129
130/**
131 * Opens a file stream for a RTFILE handle, taking ownership of the handle.
132 *
133 * @returns iprt status code.
134 * @param hFile The file handle to use. On success, handle
135 * ownership is transfered to the stream and it will be
136 * closed when the stream closes.
137 * @param pszMode The open mode, accept the same as RTStrOpen and
138 * friends however it is only used to figure out what
139 * we can do with the handle.
140 * @param fFlags Reserved, must be zero.
141 * @param ppStream Where to store the opened stream.
142 */
143RTR3DECL(int) RTStrmOpenFileHandle(RTFILE hFile, const char *pszMode, uint32_t fFlags, PRTSTREAM *ppStream);
144
145/**
146 * Queries the file handle backing the stream.
147 *
148 * @returns iprt status code.
149 * @retval VERR_NOT_AVAILABLE if the stream has no valid handle associated with
150 * it.
151 *
152 * @param pStream The stream.
153 * @param phFile Where to return the file handle. This should not be
154 * closed!
155 */
156RTR3DECL(int) RTStrmQueryFileHandle(PRTSTREAM pStream, PRTFILE phFile);
157
158/**
159 * Closes the specified stream.
160 *
161 * @returns iprt status code.
162 * @param pStream The stream to close.
163 *
164 * @note The stream will be closed and freed even when failure is returned.
165 * It cannot be used again after this call. The error status is only
166 * to indicate that the flushing of buffers or the closing of the
167 * underlying file handle failed.
168 */
169RTR3DECL(int) RTStrmClose(PRTSTREAM pStream);
170
171/**
172 * Get the pending error of the stream.
173 *
174 * @returns iprt status code. of the stream.
175 * @param pStream The stream.
176 */
177RTR3DECL(int) RTStrmError(PRTSTREAM pStream);
178
179/**
180 * Clears stream error condition.
181 *
182 * All stream operations save RTStrmClose and this will fail
183 * while an error is asserted on the stream
184 *
185 * @returns iprt status code.
186 * @param pStream The stream.
187 */
188RTR3DECL(int) RTStrmClearError(PRTSTREAM pStream);
189
190/**
191 * Changes the stream mode.
192 *
193 * @returns iprt status code.
194 * @param pStream The stream.
195 * @param fBinary The desired binary (@c true) / text mode (@c false).
196 * Pass -1 to leave it unchanged.
197 * @param fCurrentCodeSet Whether converting the stream from UTF-8 to the
198 * current code set is desired (@c true) or not (@c
199 * false). Pass -1 to leave this property unchanged.
200 */
201RTR3DECL(int) RTStrmSetMode(PRTSTREAM pStream, int fBinary, int fCurrentCodeSet);
202
203/** Stream buffering modes. */
204typedef enum RTSTRMBUFMODE
205{
206 RTSTRMBUFMODE_INVALID = 0,
207 RTSTRMBUFMODE_FULL, /**< Full buffering. */
208 RTSTRMBUFMODE_LINE, /**< Line buffering. On Windows this could be the same as RTSTRMBUFMODE_FULL. */
209 RTSTRMBUFMODE_UNBUFFERED, /**< No buffering. */
210 RTSTRMBUFMODE_END,
211 RTSTRMBUFMODE_32BIT_HACK = 0x7fffffff
212} RTSTRMBUFMODE;
213
214/**
215 * Changes the stream buffering mode.
216 *
217 * @returns iprt status code.
218 * @param pStream The stream.
219 * @param enmBufMode The new buffering mode.
220 */
221RTR3DECL(int) RTStrmSetBufferingMode(PRTSTREAM pStream, RTSTRMBUFMODE enmBufMode);
222
223/**
224 * Returns the current echo mode.
225 *
226 * This works only for standard input streams.
227 *
228 * @returns iprt status code.
229 * @retval VERR_INVALID_FUNCTION if not a TTY.
230 * @param pStream The stream.
231 * @param pfEchoChars Where to store the flag whether typed characters are echoed.
232 */
233RTR3DECL(int) RTStrmInputGetEchoChars(PRTSTREAM pStream, bool *pfEchoChars);
234
235/**
236 * Changes the behavior for echoing inpit characters on the command line.
237 *
238 * This works only for standard input streams.
239 *
240 * @returns iprt status code.
241 * @retval VERR_INVALID_FUNCTION if not a TTY.
242 * @param pStream The stream.
243 * @param fEchoChars Flag whether echoing typed characters is wanted.
244 */
245RTR3DECL(int) RTStrmInputSetEchoChars(PRTSTREAM pStream, bool fEchoChars);
246
247/**
248 * Checks if this is a terminal (TTY) or not.
249 *
250 * @returns true if it is, false if it isn't or the stream isn't valid.
251 * @param pStream The stream.
252 */
253RTR3DECL(bool) RTStrmIsTerminal(PRTSTREAM pStream);
254
255/**
256 * Gets the width of the terminal the stream is associated with.
257 *
258 * @returns IPRT status code.
259 * @retval VERR_INVALID_FUNCTION if not connected to a terminal.
260 * @param pStream The stream.
261 * @param pcchWidth Where to return the width. This will never be zero
262 * and always be set, even on error.
263 */
264RTR3DECL(int) RTStrmQueryTerminalWidth(PRTSTREAM pStream, uint32_t *pcchWidth);
265
266/**
267 * Rewinds the stream.
268 *
269 * Stream errors will be reset on success.
270 *
271 * @returns IPRT status code.
272 *
273 * @param pStream The stream.
274 *
275 * @remarks Not all streams are rewindable and that behavior is currently
276 * undefined for those.
277 */
278RTR3DECL(int) RTStrmRewind(PRTSTREAM pStream);
279
280/**
281 * Changes the file position.
282 *
283 * @returns IPRT status code.
284 *
285 * @param pStream The stream.
286 * @param off The seek offset.
287 * @param uMethod Seek method, i.e. one of the RTFILE_SEEK_* defines.
288 *
289 * @remarks Not all streams are seekable and that behavior is currently
290 * undefined for those.
291 */
292RTR3DECL(int) RTStrmSeek(PRTSTREAM pStream, RTFOFF off, uint32_t uMethod);
293
294/**
295 * Tells the stream position.
296 *
297 * @returns Stream position or IPRT error status. Non-negative numbers are
298 * stream positions, while negative numbers are IPRT error stauses.
299 *
300 * @param pStream The stream.
301 *
302 * @remarks Not all streams have a position and that behavior is currently
303 * undefined for those.
304 */
305RTR3DECL(RTFOFF) RTStrmTell(PRTSTREAM pStream);
306
307/**
308 * Reads from a file stream.
309 *
310 * @returns iprt status code.
311 * @param pStream The stream.
312 * @param pvBuf Where to put the read bits.
313 * Must be cbRead bytes or more.
314 * @param cbToRead Number of bytes to read.
315 * @param pcbRead Where to store the number of bytes actually read.
316 * If NULL cbRead bytes are read or an error is returned.
317 */
318RTR3DECL(int) RTStrmReadEx(PRTSTREAM pStream, void *pvBuf, size_t cbToRead, size_t *pcbRead);
319
320/**
321 * Writes to a file stream.
322 *
323 * @returns iprt status code.
324 * @param pStream The stream.
325 * @param pvBuf Where to get the bits to write from.
326 * @param cbToWrite Number of bytes to write.
327 * @param pcbWritten Where to store the number of bytes actually written.
328 * If NULL cbWrite bytes are written or an error is returned.
329 */
330RTR3DECL(int) RTStrmWriteEx(PRTSTREAM pStream, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
331
332/**
333 * Reads from a file stream.
334 *
335 * @returns iprt status code.
336 * @param pStream The stream.
337 * @param pvBuf Where to put the read bits.
338 * Must be cbRead bytes or more.
339 * @param cbToRead Number of bytes to read.
340 */
341DECLINLINE(int) RTStrmRead(PRTSTREAM pStream, void *pvBuf, size_t cbToRead)
342{
343 return RTStrmReadEx(pStream, pvBuf, cbToRead, NULL);
344}
345
346/**
347 * Writes to a file stream.
348 *
349 * @returns iprt status code.
350 * @param pStream The stream.
351 * @param pvBuf Where to get the bits to write from.
352 * @param cbToWrite Number of bytes to write.
353 */
354DECLINLINE(int) RTStrmWrite(PRTSTREAM pStream, const void *pvBuf, size_t cbToWrite)
355{
356 return RTStrmWriteEx(pStream, pvBuf, cbToWrite, NULL);
357}
358
359/**
360 * Reads a character from a file stream.
361 *
362 * @returns The char as an unsigned char cast to int.
363 * @returns -1 on failure.
364 * @param pStream The stream.
365 */
366RTR3DECL(int) RTStrmGetCh(PRTSTREAM pStream);
367
368/**
369 * Writes a character to a file stream.
370 *
371 * @returns iprt status code.
372 * @param pStream The stream.
373 * @param ch The char to write.
374 */
375RTR3DECL(int) RTStrmPutCh(PRTSTREAM pStream, int ch);
376
377/**
378 * Writes a string to a file stream.
379 *
380 * @returns iprt status code.
381 * @param pStream The stream.
382 * @param pszString The string to write.
383 * No newlines or anything are appended or prepended.
384 * The terminating '\\0' is not written, of course.
385 */
386RTR3DECL(int) RTStrmPutStr(PRTSTREAM pStream, const char *pszString);
387
388/**
389 * Reads a line from a file stream.
390 *
391 * A line ends with a '\\n', '\\r\\n', '\\0' or the end of the file.
392 *
393 * @returns iprt status code.
394 * @retval VINF_BUFFER_OVERFLOW if the buffer wasn't big enough to read an
395 * entire line.
396 * @retval VERR_BUFFER_OVERFLOW if a lone '\\r' was encountered at the end of
397 * the buffer and we ended up dropping the following character.
398 *
399 * @param pStream The stream.
400 * @param pszString Where to store the line.
401 * The line will *NOT* contain any '\\n'.
402 * @param cbString The size of the string buffer.
403 */
404RTR3DECL(int) RTStrmGetLine(PRTSTREAM pStream, char *pszString, size_t cbString);
405
406/**
407 * Flushes a stream.
408 *
409 * @returns iprt status code.
410 * @param pStream The stream to flush.
411 */
412RTR3DECL(int) RTStrmFlush(PRTSTREAM pStream);
413
414/**
415 * Prints a formatted string to the specified stream.
416 *
417 * @returns Number of bytes printed.
418 * @param pStream The stream to print to.
419 * @param pszFormat Runtime format string.
420 * @param ... Arguments specified by pszFormat.
421 */
422RTR3DECL(int) RTStrmPrintf(PRTSTREAM pStream, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3);
423
424/**
425 * Prints a formatted string to the specified stream.
426 *
427 * @returns Number of bytes printed.
428 * @param pStream The stream to print to.
429 * @param pszFormat Runtime format string.
430 * @param args Arguments specified by pszFormat.
431 */
432RTR3DECL(int) RTStrmPrintfV(PRTSTREAM pStream, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(2, 0);
433
434/**
435 * Prints a formatted string to the specified stream, performing wrapping of
436 * lines considered too long.
437 *
438 * If the stream is to a terminal, the terminal width is used as the max line
439 * width. Otherwise, the width is taken from @a fFlags
440 * (RTSTRMWRAPPED_F_NON_TERMINAL_WIDTH_MASK /
441 * RTSTRMWRAPPED_F_NON_TERMINAL_WIDTH_SHIFT), defaulting to 80 if zero.
442 *
443 * @returns Low 16 bits is the line offset, high 16 bits the number of lines
444 * outputted. Apply RTSTRMWRAPPED_F_LINE_OFFSET_MASK to the value and
445 * it can be passed via @a fFlags to the next invocation (not necessary
446 * if all format strings ends with a newline).
447 * Negative values are IPRT error status codes.
448 * @param pStream The stream to print to.
449 * @param fFlags RTSTRMWRAPPED_F_XXX - flags, configuration and state.
450 * @param pszFormat Runtime format string.
451 * @param ... Arguments specified by pszFormat.
452 * @sa RTStrmWrappedPrintfV, RTStrmPrintf, RTStrmPrintfV
453 */
454RTDECL(int32_t) RTStrmWrappedPrintf(PRTSTREAM pStream, uint32_t fFlags, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
455
456/**
457 * Prints a formatted string to the specified stream, performing wrapping of
458 * lines considered too long.
459 *
460 * If the stream is to a terminal, the terminal width is used as the max line
461 * width. Otherwise, the width is taken from @a fFlags
462 * (RTSTRMWRAPPED_F_NON_TERMINAL_WIDTH_MASK /
463 * RTSTRMWRAPPED_F_NON_TERMINAL_WIDTH_SHIFT), defaulting to 80 if zero.
464 *
465 * @returns Low 16 bits is the line offset, high 16 bits the number of lines
466 * outputted. Apply RTSTRMWRAPPED_F_LINE_OFFSET_MASK to the value and
467 * it can be passed via @a fFlags to the next invocation (not necessary
468 * if all format strings ends with a newline).
469 * Negative values are IPRT error status codes.
470 * @param pStream The stream to print to.
471 * @param fFlags RTSTRMWRAPPED_F_XXX - flags, configuration and state.
472 * @param pszFormat Runtime format string.
473 * @param va Arguments specified by pszFormat.
474 * @sa RTStrmWrappedPrintf, RTStrmPrintf, RTStrmPrintfV
475 */
476RTDECL(int32_t) RTStrmWrappedPrintfV(PRTSTREAM pStream, uint32_t fFlags, const char *pszFormat,
477 va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
478
479/** @name RTSTRMWRAPPED_F_XXX - Flags for RTStrmWrappedPrintf &
480 * RTStrmWrappedPrintfV.
481 * @{ */
482/** The current line offset mask.
483 * This should be used to passed the line off state from one call to the next
484 * when printing incomplete lines. If all format strings ends with a newline,
485 * this is not necessary. */
486#define RTSTRMWRAPPED_F_LINE_OFFSET_MASK UINT32_C(0x00000fff)
487/** The non-terminal width mask. Defaults to 80 if not specified (zero). */
488#define RTSTRMWRAPPED_F_NON_TERMINAL_WIDTH_MASK UINT32_C(0x000ff000)
489/** The non-terminal width shift. */
490#define RTSTRMWRAPPED_F_NON_TERMINAL_WIDTH_SHIFT 12
491/** The hanging indent level mask - defaults to 4 if zero.
492 * Used when RTSTRMWRAPPED_F_HANGING_INDENT is set. */
493#define RTSTRMWRAPPED_F_HANGING_INDENT_MASK UINT32_C(0x01f00000)
494/** The hanging indent level shift. */
495#define RTSTRMWRAPPED_F_HANGING_INDENT_SHIFT 20
496/** Hanging indent. Used for command synopsis and such. */
497#define RTSTRMWRAPPED_F_HANGING_INDENT UINT32_C(0x80000000)
498/** @} */
499
500/**
501 * Dumper vprintf-like function outputting to a stream.
502 *
503 * @param pvUser The stream to print to. NULL means standard output.
504 * @param pszFormat Runtime format string.
505 * @param va Arguments specified by pszFormat.
506 */
507RTR3DECL(void) RTStrmDumpPrintfV(void *pvUser, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0);
508
509/**
510 * Prints a formatted string to the standard output stream (g_pStdOut).
511 *
512 * @returns Number of bytes printed.
513 * @param pszFormat Runtime format string.
514 * @param ... Arguments specified by pszFormat.
515 */
516RTR3DECL(int) RTPrintf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
517
518/**
519 * Prints a formatted string to the standard output stream (g_pStdOut).
520 *
521 * @returns Number of bytes printed.
522 * @param pszFormat Runtime format string.
523 * @param args Arguments specified by pszFormat.
524 */
525RTR3DECL(int) RTPrintfV(const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(1, 0);
526
527/** @} */
528
529RT_C_DECLS_END
530
531#endif /* !IPRT_INCLUDED_stream_h */
532
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