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 |
|
---|
36 | RT_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. */
|
---|
45 | typedef struct RTSTREAM *PRTSTREAM;
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | /** Pointer to the standard input stream. */
|
---|
49 | extern RTDATADECL(PRTSTREAM) g_pStdIn;
|
---|
50 |
|
---|
51 | /** Pointer to the standard error stream. */
|
---|
52 | extern RTDATADECL(PRTSTREAM) g_pStdErr;
|
---|
53 |
|
---|
54 | /** Pointer to the standard output stream. */
|
---|
55 | extern 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]
|
---|
65 | * @param ppStream Where to store the opened stream.
|
---|
66 | */
|
---|
67 | RTR3DECL(int) RTStrmOpen(const char *pszFilename, const char *pszMode, PRTSTREAM *ppStream);
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Opens a file stream.
|
---|
71 | *
|
---|
72 | * @returns iprt status code.
|
---|
73 | * @param pszMode The open mode. See fopen() standard.
|
---|
74 | * Format: <a|r|w>[+][b]
|
---|
75 | * @param ppStream Where to store the opened stream.
|
---|
76 | * @param pszFilenameFmt Filename path format string.
|
---|
77 | * @param args Arguments to the format string.
|
---|
78 | */
|
---|
79 | RTR3DECL(int) RTStrmOpenFV(const char *pszMode, PRTSTREAM *ppStream, const char *pszFilenameFmt,
|
---|
80 | va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Opens a file stream.
|
---|
84 | *
|
---|
85 | * @returns iprt status code.
|
---|
86 | * @param pszMode The open mode. See fopen() standard.
|
---|
87 | * Format: <a|r|w>[+][b]
|
---|
88 | * @param ppStream Where to store the opened stream.
|
---|
89 | * @param pszFilenameFmt Filename path format string.
|
---|
90 | * @param ... Arguments to the format string.
|
---|
91 | */
|
---|
92 | RTR3DECL(int) RTStrmOpenF(const char *pszMode, PRTSTREAM *ppStream, const char *pszFilenameFmt, ...) RT_IPRT_FORMAT_ATTR(3, 4);
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Closes the specified stream.
|
---|
96 | *
|
---|
97 | * @returns iprt status code.
|
---|
98 | * @param pStream The stream to close.
|
---|
99 | *
|
---|
100 | * @note The stream will be closed and freed even when failure is returned.
|
---|
101 | * It cannot be used again after this call. The error status is only
|
---|
102 | * to indicate that the flushing of buffers or the closing of the
|
---|
103 | * underlying file handle failed.
|
---|
104 | */
|
---|
105 | RTR3DECL(int) RTStrmClose(PRTSTREAM pStream);
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Get the pending error of the stream.
|
---|
109 | *
|
---|
110 | * @returns iprt status code. of the stream.
|
---|
111 | * @param pStream The stream.
|
---|
112 | */
|
---|
113 | RTR3DECL(int) RTStrmError(PRTSTREAM pStream);
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Clears stream error condition.
|
---|
117 | *
|
---|
118 | * All stream operations save RTStrmClose and this will fail
|
---|
119 | * while an error is asserted on the stream
|
---|
120 | *
|
---|
121 | * @returns iprt status code.
|
---|
122 | * @param pStream The stream.
|
---|
123 | */
|
---|
124 | RTR3DECL(int) RTStrmClearError(PRTSTREAM pStream);
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Changes the stream mode.
|
---|
128 | *
|
---|
129 | * @returns iprt status code.
|
---|
130 | * @param pStream The stream.
|
---|
131 | * @param fBinary The desired binary (@c true) / text mode (@c false).
|
---|
132 | * Pass -1 to leave it unchanged.
|
---|
133 | * @param fCurrentCodeSet Whether converting the stream from UTF-8 to the
|
---|
134 | * current code set is desired (@c true) or not (@c
|
---|
135 | * false). Pass -1 to leave this property unchanged.
|
---|
136 | */
|
---|
137 | RTR3DECL(int) RTStrmSetMode(PRTSTREAM pStream, int fBinary, int fCurrentCodeSet);
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Returns the current echo mode.
|
---|
141 | * This works only for standard input streams.
|
---|
142 | *
|
---|
143 | * @returns iprt status code.
|
---|
144 | * @retval VERR_INVALID_FUNCTION if not a TTY.
|
---|
145 | * @param pStream The stream.
|
---|
146 | * @param pfEchoChars Where to store the flag whether typed characters are echoed.
|
---|
147 | */
|
---|
148 | RTR3DECL(int) RTStrmInputGetEchoChars(PRTSTREAM pStream, bool *pfEchoChars);
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Changes the behavior for echoing inpit characters on the command line.
|
---|
152 | * This works only for standard input streams.
|
---|
153 | *
|
---|
154 | * @returns iprt status code.
|
---|
155 | * @retval VERR_INVALID_FUNCTION if not a TTY.
|
---|
156 | * @param pStream The stream.
|
---|
157 | * @param fEchoChars Flag whether echoing typed characters is wanted.
|
---|
158 | */
|
---|
159 | RTR3DECL(int) RTStrmInputSetEchoChars(PRTSTREAM pStream, bool fEchoChars);
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Checks if this is a terminal (TTY) or not.
|
---|
163 | *
|
---|
164 | * @returns true if it is, false if it isn't or the stream isn't valid.
|
---|
165 | * @param pStream The stream.
|
---|
166 | */
|
---|
167 | RTR3DECL(bool) RTStrmIsTerminal(PRTSTREAM pStream);
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Gets the width of the terminal the stream is associated with.
|
---|
171 | *
|
---|
172 | * @returns IPRT status code.
|
---|
173 | * @retval VERR_INVALID_FUNCTION if not connected to a terminal.
|
---|
174 | * @param pStream The stream.
|
---|
175 | * @param pcchWidth Where to return the width. This will never be zero
|
---|
176 | * and always be set, even on error.
|
---|
177 | */
|
---|
178 | RTR3DECL(int) RTStrmQueryTerminalWidth(PRTSTREAM pStream, uint32_t *pcchWidth);
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Rewinds the stream.
|
---|
182 | *
|
---|
183 | * Stream errors will be reset on success.
|
---|
184 | *
|
---|
185 | * @returns IPRT status code.
|
---|
186 | *
|
---|
187 | * @param pStream The stream.
|
---|
188 | *
|
---|
189 | * @remarks Not all streams are rewindable and that behavior is currently
|
---|
190 | * undefined for those.
|
---|
191 | */
|
---|
192 | RTR3DECL(int) RTStrmRewind(PRTSTREAM pStream);
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Reads from a file stream.
|
---|
196 | *
|
---|
197 | * @returns iprt status code.
|
---|
198 | * @param pStream The stream.
|
---|
199 | * @param pvBuf Where to put the read bits.
|
---|
200 | * Must be cbRead bytes or more.
|
---|
201 | * @param cbRead Number of bytes to read.
|
---|
202 | * @param pcbRead Where to store the number of bytes actually read.
|
---|
203 | * If NULL cbRead bytes are read or an error is returned.
|
---|
204 | */
|
---|
205 | RTR3DECL(int) RTStrmReadEx(PRTSTREAM pStream, void *pvBuf, size_t cbRead, size_t *pcbRead);
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Writes to a file stream.
|
---|
209 | *
|
---|
210 | * @returns iprt status code.
|
---|
211 | * @param pStream The stream.
|
---|
212 | * @param pvBuf Where to get the bits to write from.
|
---|
213 | * @param cbWrite Number of bytes to write.
|
---|
214 | * @param pcbWritten Where to store the number of bytes actually written.
|
---|
215 | * If NULL cbWrite bytes are written or an error is returned.
|
---|
216 | */
|
---|
217 | RTR3DECL(int) RTStrmWriteEx(PRTSTREAM pStream, const void *pvBuf, size_t cbWrite, size_t *pcbWritten);
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Reads from a file stream.
|
---|
221 | *
|
---|
222 | * @returns iprt status code.
|
---|
223 | * @param pStream The stream.
|
---|
224 | * @param pvBuf Where to put the read bits.
|
---|
225 | * Must be cbRead bytes or more.
|
---|
226 | * @param cbRead Number of bytes to read.
|
---|
227 | */
|
---|
228 | DECLINLINE(int) RTStrmRead(PRTSTREAM pStream, void *pvBuf, size_t cbRead)
|
---|
229 | {
|
---|
230 | return RTStrmReadEx(pStream, pvBuf, cbRead, NULL);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Writes to a file stream.
|
---|
235 | *
|
---|
236 | * @returns iprt status code.
|
---|
237 | * @param pStream The stream.
|
---|
238 | * @param pvBuf Where to get the bits to write from.
|
---|
239 | * @param cbWrite Number of bytes to write.
|
---|
240 | */
|
---|
241 | DECLINLINE(int) RTStrmWrite(PRTSTREAM pStream, const void *pvBuf, size_t cbWrite)
|
---|
242 | {
|
---|
243 | return RTStrmWriteEx(pStream, pvBuf, cbWrite, NULL);
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Reads a character from a file stream.
|
---|
248 | *
|
---|
249 | * @returns The char as an unsigned char cast to int.
|
---|
250 | * @returns -1 on failure.
|
---|
251 | * @param pStream The stream.
|
---|
252 | */
|
---|
253 | RTR3DECL(int) RTStrmGetCh(PRTSTREAM pStream);
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Writes a character to a file stream.
|
---|
257 | *
|
---|
258 | * @returns iprt status code.
|
---|
259 | * @param pStream The stream.
|
---|
260 | * @param ch The char to write.
|
---|
261 | */
|
---|
262 | RTR3DECL(int) RTStrmPutCh(PRTSTREAM pStream, int ch);
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Writes a string to a file stream.
|
---|
266 | *
|
---|
267 | * @returns iprt status code.
|
---|
268 | * @param pStream The stream.
|
---|
269 | * @param pszString The string to write.
|
---|
270 | * No newlines or anything are appended or prepended.
|
---|
271 | * The terminating '\\0' is not written, of course.
|
---|
272 | */
|
---|
273 | RTR3DECL(int) RTStrmPutStr(PRTSTREAM pStream, const char *pszString);
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Reads a line from a file stream.
|
---|
277 | *
|
---|
278 | * A line ends with a '\\n', '\\r\\n', '\\0' or the end of the file.
|
---|
279 | *
|
---|
280 | * @returns iprt status code.
|
---|
281 | * @retval VINF_BUFFER_OVERFLOW if the buffer wasn't big enough to read an
|
---|
282 | * entire line.
|
---|
283 | * @retval VERR_BUFFER_OVERFLOW if a lone '\\r' was encountered at the end of
|
---|
284 | * the buffer and we ended up dropping the following character.
|
---|
285 | *
|
---|
286 | * @param pStream The stream.
|
---|
287 | * @param pszString Where to store the line.
|
---|
288 | * The line will *NOT* contain any '\\n'.
|
---|
289 | * @param cbString The size of the string buffer.
|
---|
290 | */
|
---|
291 | RTR3DECL(int) RTStrmGetLine(PRTSTREAM pStream, char *pszString, size_t cbString);
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Flushes a stream.
|
---|
295 | *
|
---|
296 | * @returns iprt status code.
|
---|
297 | * @param pStream The stream to flush.
|
---|
298 | */
|
---|
299 | RTR3DECL(int) RTStrmFlush(PRTSTREAM pStream);
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Prints a formatted string to the specified stream.
|
---|
303 | *
|
---|
304 | * @returns Number of bytes printed.
|
---|
305 | * @param pStream The stream to print to.
|
---|
306 | * @param pszFormat Runtime format string.
|
---|
307 | * @param ... Arguments specified by pszFormat.
|
---|
308 | */
|
---|
309 | RTR3DECL(int) RTStrmPrintf(PRTSTREAM pStream, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3);
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Prints a formatted string to the specified stream.
|
---|
313 | *
|
---|
314 | * @returns Number of bytes printed.
|
---|
315 | * @param pStream The stream to print to.
|
---|
316 | * @param pszFormat Runtime format string.
|
---|
317 | * @param args Arguments specified by pszFormat.
|
---|
318 | */
|
---|
319 | RTR3DECL(int) RTStrmPrintfV(PRTSTREAM pStream, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(2, 0);
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Prints a formatted string to the specified stream, performing wrapping of
|
---|
323 | * lines considered too long.
|
---|
324 | *
|
---|
325 | * If the stream is to a terminal, the terminal width is used as the max line
|
---|
326 | * width. Otherwise, the width is taken from @a fFlags
|
---|
327 | * (RTSTRMWRAPPED_F_NON_TERMINAL_WIDTH_MASK /
|
---|
328 | * RTSTRMWRAPPED_F_NON_TERMINAL_WIDTH_SHIFT), defaulting to 80 if zero.
|
---|
329 | *
|
---|
330 | * @returns Low 16 bits is the line offset, high 16 bits the number of lines
|
---|
331 | * outputted. Apply RTSTRMWRAPPED_F_LINE_OFFSET_MASK to the value and
|
---|
332 | * it can be passed via @a fFlags to the next invocation (not necessary
|
---|
333 | * if all format strings ends with a newline).
|
---|
334 | * Negative values are IPRT error status codes.
|
---|
335 | * @param pStream The stream to print to.
|
---|
336 | * @param fFlags RTSTRMWRAPPED_F_XXX - flags, configuration and state.
|
---|
337 | * @param pszFormat Runtime format string.
|
---|
338 | * @param ... Arguments specified by pszFormat.
|
---|
339 | * @sa RTStrmWrappedPrintfV, RTStrmPrintf, RTStrmPrintfV
|
---|
340 | */
|
---|
341 | RTDECL(int32_t) RTStrmWrappedPrintf(PRTSTREAM pStream, uint32_t fFlags, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * Prints a formatted string to the specified stream, performing wrapping of
|
---|
345 | * lines considered too long.
|
---|
346 | *
|
---|
347 | * If the stream is to a terminal, the terminal width is used as the max line
|
---|
348 | * width. Otherwise, the width is taken from @a fFlags
|
---|
349 | * (RTSTRMWRAPPED_F_NON_TERMINAL_WIDTH_MASK /
|
---|
350 | * RTSTRMWRAPPED_F_NON_TERMINAL_WIDTH_SHIFT), defaulting to 80 if zero.
|
---|
351 | *
|
---|
352 | * @returns Low 16 bits is the line offset, high 16 bits the number of lines
|
---|
353 | * outputted. Apply RTSTRMWRAPPED_F_LINE_OFFSET_MASK to the value and
|
---|
354 | * it can be passed via @a fFlags to the next invocation (not necessary
|
---|
355 | * if all format strings ends with a newline).
|
---|
356 | * Negative values are IPRT error status codes.
|
---|
357 | * @param pStream The stream to print to.
|
---|
358 | * @param fFlags RTSTRMWRAPPED_F_XXX - flags, configuration and state.
|
---|
359 | * @param pszFormat Runtime format string.
|
---|
360 | * @param va Arguments specified by pszFormat.
|
---|
361 | * @sa RTStrmWrappedPrintf, RTStrmPrintf, RTStrmPrintfV
|
---|
362 | */
|
---|
363 | RTDECL(int32_t) RTStrmWrappedPrintfV(PRTSTREAM pStream, uint32_t fFlags, const char *pszFormat,
|
---|
364 | va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
|
---|
365 |
|
---|
366 | /** @name RTSTRMWRAPPED_F_XXX - Flags for RTStrmWrappedPrintf &
|
---|
367 | * RTStrmWrappedPrintfV.
|
---|
368 | * @{ */
|
---|
369 | /** The current line offset mask.
|
---|
370 | * This should be used to passed the line off state from one call to the next
|
---|
371 | * when printing incomplete lines. If all format strings ends with a newline,
|
---|
372 | * this is not necessary. */
|
---|
373 | #define RTSTRMWRAPPED_F_LINE_OFFSET_MASK UINT32_C(0x00000fff)
|
---|
374 | /** The non-terminal width mask. Defaults to 80 if not specified (zero). */
|
---|
375 | #define RTSTRMWRAPPED_F_NON_TERMINAL_WIDTH_MASK UINT32_C(0x000ff000)
|
---|
376 | /** The non-terminal width shift. */
|
---|
377 | #define RTSTRMWRAPPED_F_NON_TERMINAL_WIDTH_SHIFT 12
|
---|
378 | /** The hanging indent level mask - defaults to 4 if zero.
|
---|
379 | * Used when RTSTRMWRAPPED_F_HANGING_INDENT is set. */
|
---|
380 | #define RTSTRMWRAPPED_F_HANGING_INDENT_MASK UINT32_C(0x01f00000)
|
---|
381 | /** The hanging indent level shift. */
|
---|
382 | #define RTSTRMWRAPPED_F_HANGING_INDENT_SHIFT 20
|
---|
383 | /** Hanging indent. Used for command synopsis and such. */
|
---|
384 | #define RTSTRMWRAPPED_F_HANGING_INDENT UINT32_C(0x80000000)
|
---|
385 | /** @} */
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Dumper vprintf-like function outputting to a stream.
|
---|
389 | *
|
---|
390 | * @param pvUser The stream to print to. NULL means standard output.
|
---|
391 | * @param pszFormat Runtime format string.
|
---|
392 | * @param va Arguments specified by pszFormat.
|
---|
393 | */
|
---|
394 | RTR3DECL(void) RTStrmDumpPrintfV(void *pvUser, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0);
|
---|
395 |
|
---|
396 | /**
|
---|
397 | * Prints a formatted string to the standard output stream (g_pStdOut).
|
---|
398 | *
|
---|
399 | * @returns Number of bytes printed.
|
---|
400 | * @param pszFormat Runtime format string.
|
---|
401 | * @param ... Arguments specified by pszFormat.
|
---|
402 | */
|
---|
403 | RTR3DECL(int) RTPrintf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
|
---|
404 |
|
---|
405 | /**
|
---|
406 | * Prints a formatted string to the standard output stream (g_pStdOut).
|
---|
407 | *
|
---|
408 | * @returns Number of bytes printed.
|
---|
409 | * @param pszFormat Runtime format string.
|
---|
410 | * @param args Arguments specified by pszFormat.
|
---|
411 | */
|
---|
412 | RTR3DECL(int) RTPrintfV(const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(1, 0);
|
---|
413 |
|
---|
414 | /** @} */
|
---|
415 |
|
---|
416 | RT_C_DECLS_END
|
---|
417 |
|
---|
418 | #endif /* !IPRT_INCLUDED_stream_h */
|
---|
419 |
|
---|