1 | /** @file
|
---|
2 | * IPRT - I/O Stream.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2017 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_stream_h
|
---|
27 | #define ___iprt_stream_h
|
---|
28 |
|
---|
29 | #include <iprt/cdefs.h>
|
---|
30 | #include <iprt/types.h>
|
---|
31 | #include <iprt/stdarg.h>
|
---|
32 |
|
---|
33 | RT_C_DECLS_BEGIN
|
---|
34 |
|
---|
35 | /** @defgroup grp_rt_stream RTStrm - File Streams
|
---|
36 | * @ingroup grp_rt
|
---|
37 | * @{
|
---|
38 | */
|
---|
39 |
|
---|
40 | /** Pointer to a stream. */
|
---|
41 | typedef struct RTSTREAM *PRTSTREAM;
|
---|
42 |
|
---|
43 | /** Pointer to the standard input stream. */
|
---|
44 | extern RTDATADECL(PRTSTREAM) g_pStdIn;
|
---|
45 |
|
---|
46 | /** Pointer to the standard error stream. */
|
---|
47 | extern RTDATADECL(PRTSTREAM) g_pStdErr;
|
---|
48 |
|
---|
49 | /** Pointer to the standard output stream. */
|
---|
50 | extern RTDATADECL(PRTSTREAM) g_pStdOut;
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Opens a file stream.
|
---|
55 | *
|
---|
56 | * @returns iprt status code.
|
---|
57 | * @param pszFilename Path to the file to open.
|
---|
58 | * @param pszMode The open mode. See fopen() standard.
|
---|
59 | * Format: <a|r|w>[+][b|t]
|
---|
60 | * @param ppStream Where to store the opened stream.
|
---|
61 | */
|
---|
62 | RTR3DECL(int) RTStrmOpen(const char *pszFilename, const char *pszMode, PRTSTREAM *ppStream);
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Opens a file stream.
|
---|
66 | *
|
---|
67 | * @returns iprt status code.
|
---|
68 | * @param pszMode The open mode. See fopen() standard.
|
---|
69 | * Format: <a|r|w>[+][b|t]
|
---|
70 | * @param ppStream Where to store the opened stream.
|
---|
71 | * @param pszFilenameFmt Filename path format string.
|
---|
72 | * @param args Arguments to the format string.
|
---|
73 | */
|
---|
74 | RTR3DECL(int) RTStrmOpenFV(const char *pszMode, PRTSTREAM *ppStream, const char *pszFilenameFmt,
|
---|
75 | va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Opens a file stream.
|
---|
79 | *
|
---|
80 | * @returns iprt status code.
|
---|
81 | * @param pszMode The open mode. See fopen() standard.
|
---|
82 | * Format: <a|r|w>[+][b|t]
|
---|
83 | * @param ppStream Where to store the opened stream.
|
---|
84 | * @param pszFilenameFmt Filename path format string.
|
---|
85 | * @param ... Arguments to the format string.
|
---|
86 | */
|
---|
87 | RTR3DECL(int) RTStrmOpenF(const char *pszMode, PRTSTREAM *ppStream, const char *pszFilenameFmt, ...) RT_IPRT_FORMAT_ATTR(3, 4);
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Closes the specified stream.
|
---|
91 | *
|
---|
92 | * @returns iprt status code.
|
---|
93 | * @param pStream The stream to close.
|
---|
94 | */
|
---|
95 | RTR3DECL(int) RTStrmClose(PRTSTREAM pStream);
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Get the pending error of the stream.
|
---|
99 | *
|
---|
100 | * @returns iprt status code. of the stream.
|
---|
101 | * @param pStream The stream.
|
---|
102 | */
|
---|
103 | RTR3DECL(int) RTStrmError(PRTSTREAM pStream);
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Clears stream error condition.
|
---|
107 | *
|
---|
108 | * All stream operations save RTStrmClose and this will fail
|
---|
109 | * while an error is asserted on the stream
|
---|
110 | *
|
---|
111 | * @returns iprt status code.
|
---|
112 | * @param pStream The stream.
|
---|
113 | */
|
---|
114 | RTR3DECL(int) RTStrmClearError(PRTSTREAM pStream);
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Changes the stream mode.
|
---|
118 | *
|
---|
119 | * @returns iprt status code.
|
---|
120 | * @param pStream The stream.
|
---|
121 | * @param fBinary The desired binary (@c true) / text mode (@c false).
|
---|
122 | * Pass -1 to leave it unchanged.
|
---|
123 | * @param fCurrentCodeSet Whether converting the stream from UTF-8 to the
|
---|
124 | * current code set is desired (@c true) or not (@c
|
---|
125 | * false). Pass -1 to leave this property unchanged.
|
---|
126 | */
|
---|
127 | RTR3DECL(int) RTStrmSetMode(PRTSTREAM pStream, int fBinary, int fCurrentCodeSet);
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Returns the current echo mode.
|
---|
131 | * This works only for standard input streams.
|
---|
132 | *
|
---|
133 | * @returns iprt status code.
|
---|
134 | * @param pStream The stream.
|
---|
135 | * @param pfEchoChars Where to store the flag whether typed characters are echoed.
|
---|
136 | */
|
---|
137 | RTR3DECL(int) RTStrmInputGetEchoChars(PRTSTREAM pStream, bool *pfEchoChars);
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Changes the behavior for echoing inpit characters on the command line.
|
---|
141 | * This works only for standard input streams.
|
---|
142 | *
|
---|
143 | * @returns iprt status code.
|
---|
144 | * @param pStream The stream.
|
---|
145 | * @param fEchoChars Flag whether echoing typed characters is wanted.
|
---|
146 | */
|
---|
147 | RTR3DECL(int) RTStrmInputSetEchoChars(PRTSTREAM pStream, bool fEchoChars);
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Checks if this is a terminal (TTY) or not.
|
---|
151 | *
|
---|
152 | * @returns true if it is, false if it isn't or the stream isn't valid.
|
---|
153 | * @param pStream The stream.
|
---|
154 | */
|
---|
155 | RTR3DECL(bool) RTStrmIsTerminal(PRTSTREAM pStream);
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Gets the width of the terminal the stream is associated with.
|
---|
159 | *
|
---|
160 | * @returns IPRT status code.
|
---|
161 | * @retval VERR_INVALID_FUNCTION if not connected to a terminal.
|
---|
162 | * @param pStream The stream.
|
---|
163 | * @param pcchWidth Where to return the width. This will never be zero
|
---|
164 | * and always be set, even on error.
|
---|
165 | */
|
---|
166 | RTR3DECL(int) RTStrmQueryTerminalWidth(PRTSTREAM pStream, uint32_t *pcchWidth);
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Rewinds the stream.
|
---|
170 | *
|
---|
171 | * Stream errors will be reset on success.
|
---|
172 | *
|
---|
173 | * @returns IPRT status code.
|
---|
174 | *
|
---|
175 | * @param pStream The stream.
|
---|
176 | *
|
---|
177 | * @remarks Not all streams are rewindable and that behavior is currently
|
---|
178 | * undefined for those.
|
---|
179 | */
|
---|
180 | RTR3DECL(int) RTStrmRewind(PRTSTREAM pStream);
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Reads from a file stream.
|
---|
184 | *
|
---|
185 | * @returns iprt status code.
|
---|
186 | * @param pStream The stream.
|
---|
187 | * @param pvBuf Where to put the read bits.
|
---|
188 | * Must be cbRead bytes or more.
|
---|
189 | * @param cbRead Number of bytes to read.
|
---|
190 | * @param pcbRead Where to store the number of bytes actually read.
|
---|
191 | * If NULL cbRead bytes are read or an error is returned.
|
---|
192 | */
|
---|
193 | RTR3DECL(int) RTStrmReadEx(PRTSTREAM pStream, void *pvBuf, size_t cbRead, size_t *pcbRead);
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Writes to a file stream.
|
---|
197 | *
|
---|
198 | * @returns iprt status code.
|
---|
199 | * @param pStream The stream.
|
---|
200 | * @param pvBuf Where to get the bits to write from.
|
---|
201 | * @param cbWrite Number of bytes to write.
|
---|
202 | * @param pcbWritten Where to store the number of bytes actually written.
|
---|
203 | * If NULL cbWrite bytes are written or an error is returned.
|
---|
204 | */
|
---|
205 | RTR3DECL(int) RTStrmWriteEx(PRTSTREAM pStream, const void *pvBuf, size_t cbWrite, size_t *pcbWritten);
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Reads from a file stream.
|
---|
209 | *
|
---|
210 | * @returns iprt status code.
|
---|
211 | * @param pStream The stream.
|
---|
212 | * @param pvBuf Where to put the read bits.
|
---|
213 | * Must be cbRead bytes or more.
|
---|
214 | * @param cbRead Number of bytes to read.
|
---|
215 | */
|
---|
216 | DECLINLINE(int) RTStrmRead(PRTSTREAM pStream, void *pvBuf, size_t cbRead)
|
---|
217 | {
|
---|
218 | return RTStrmReadEx(pStream, pvBuf, cbRead, NULL);
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Writes to a file stream.
|
---|
223 | *
|
---|
224 | * @returns iprt status code.
|
---|
225 | * @param pStream The stream.
|
---|
226 | * @param pvBuf Where to get the bits to write from.
|
---|
227 | * @param cbWrite Number of bytes to write.
|
---|
228 | */
|
---|
229 | DECLINLINE(int) RTStrmWrite(PRTSTREAM pStream, const void *pvBuf, size_t cbWrite)
|
---|
230 | {
|
---|
231 | return RTStrmWriteEx(pStream, pvBuf, cbWrite, NULL);
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Reads a character from a file stream.
|
---|
236 | *
|
---|
237 | * @returns The char as an unsigned char cast to int.
|
---|
238 | * @returns -1 on failure.
|
---|
239 | * @param pStream The stream.
|
---|
240 | */
|
---|
241 | RTR3DECL(int) RTStrmGetCh(PRTSTREAM pStream);
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Writes a character to a file stream.
|
---|
245 | *
|
---|
246 | * @returns iprt status code.
|
---|
247 | * @param pStream The stream.
|
---|
248 | * @param ch The char to write.
|
---|
249 | */
|
---|
250 | RTR3DECL(int) RTStrmPutCh(PRTSTREAM pStream, int ch);
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Writes a string to a file stream.
|
---|
254 | *
|
---|
255 | * @returns iprt status code.
|
---|
256 | * @param pStream The stream.
|
---|
257 | * @param pszString The string to write.
|
---|
258 | * No newlines or anything are appended or prepended.
|
---|
259 | * The terminating '\\0' is not written, of course.
|
---|
260 | */
|
---|
261 | RTR3DECL(int) RTStrmPutStr(PRTSTREAM pStream, const char *pszString);
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Reads a line from a file stream.
|
---|
265 | *
|
---|
266 | * A line ends with a '\\n', '\\r\\n', '\\0' or the end of the file.
|
---|
267 | *
|
---|
268 | * @returns iprt status code.
|
---|
269 | * @retval VINF_BUFFER_OVERFLOW if the buffer wasn't big enough to read an
|
---|
270 | * entire line.
|
---|
271 | * @retval VERR_BUFFER_OVERFLOW if a lone '\\r' was encountered at the end of
|
---|
272 | * the buffer and we ended up dropping the following character.
|
---|
273 | *
|
---|
274 | * @param pStream The stream.
|
---|
275 | * @param pszString Where to store the line.
|
---|
276 | * The line will *NOT* contain any '\\n'.
|
---|
277 | * @param cbString The size of the string buffer.
|
---|
278 | */
|
---|
279 | RTR3DECL(int) RTStrmGetLine(PRTSTREAM pStream, char *pszString, size_t cbString);
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Flushes a stream.
|
---|
283 | *
|
---|
284 | * @returns iprt status code.
|
---|
285 | * @param pStream The stream to flush.
|
---|
286 | */
|
---|
287 | RTR3DECL(int) RTStrmFlush(PRTSTREAM pStream);
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Prints a formatted string to the specified stream.
|
---|
291 | *
|
---|
292 | * @returns Number of bytes printed.
|
---|
293 | * @param pStream The stream to print to.
|
---|
294 | * @param pszFormat Runtime format string.
|
---|
295 | * @param ... Arguments specified by pszFormat.
|
---|
296 | */
|
---|
297 | RTR3DECL(int) RTStrmPrintf(PRTSTREAM pStream, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3);
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Prints a formatted string to the specified stream.
|
---|
301 | *
|
---|
302 | * @returns Number of bytes printed.
|
---|
303 | * @param pStream The stream to print to.
|
---|
304 | * @param pszFormat Runtime format string.
|
---|
305 | * @param args Arguments specified by pszFormat.
|
---|
306 | */
|
---|
307 | RTR3DECL(int) RTStrmPrintfV(PRTSTREAM pStream, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(2, 0);
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Dumper vprintf-like function outputting to a stream.
|
---|
311 | *
|
---|
312 | * @param pvUser The stream to print to. NULL means standard output.
|
---|
313 | * @param pszFormat Runtime format string.
|
---|
314 | * @param va Arguments specified by pszFormat.
|
---|
315 | */
|
---|
316 | RTR3DECL(void) RTStrmDumpPrintfV(void *pvUser, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0);
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Prints a formatted string to the standard output stream (g_pStdOut).
|
---|
320 | *
|
---|
321 | * @returns Number of bytes printed.
|
---|
322 | * @param pszFormat Runtime format string.
|
---|
323 | * @param ... Arguments specified by pszFormat.
|
---|
324 | */
|
---|
325 | RTR3DECL(int) RTPrintf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Prints a formatted string to the standard output stream (g_pStdOut).
|
---|
329 | *
|
---|
330 | * @returns Number of bytes printed.
|
---|
331 | * @param pszFormat Runtime format string.
|
---|
332 | * @param args Arguments specified by pszFormat.
|
---|
333 | */
|
---|
334 | RTR3DECL(int) RTPrintfV(const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(1, 0);
|
---|
335 |
|
---|
336 | /** @} */
|
---|
337 |
|
---|
338 | RT_C_DECLS_END
|
---|
339 |
|
---|
340 | #endif
|
---|
341 |
|
---|