ReactOS 0.4.16-dev-2522-g97cc325
stream.c File Reference

Console I/O streams. More...

#include <windef.h>
#include <winbase.h>
#include <wincon.h>
#include <winnls.h>
#include <strsafe.h>
#include "conutils.h"
#include "stream.h"
#include "stream_private.h"
Include dependency graph for stream.c:

Go to the source code of this file.

Macros

#define UNICODE
 
#define _UNICODE
 
#define CON_STREAM_SET_MODE(Stream, Mode, CacheCodePage)
 

Functions

BOOL ConStreamInitEx (OUT PCON_STREAM Stream, IN PVOID Handle, IN CON_STREAM_MODE Mode, IN UINT CacheCodePage OPTIONAL, IN CON_WRITE_FUNC WriteFunc OPTIONAL)
 
BOOL ConStreamInit (OUT PCON_STREAM Stream, IN PVOID Handle, IN CON_STREAM_MODE Mode, IN UINT CacheCodePage OPTIONAL)
 
BOOL ConStreamSetMode (IN PCON_STREAM Stream, IN CON_STREAM_MODE Mode, IN UINT CacheCodePage OPTIONAL)
 
BOOL ConStreamSetCacheCodePage (IN PCON_STREAM Stream, IN UINT CacheCodePage)
 
HANDLE ConStreamGetOSHandle (IN PCON_STREAM Stream)
 
BOOL ConStreamSetOSHandle (IN PCON_STREAM Stream, IN HANDLE Handle)
 

Variables

CON_STREAM csStdIn
 
CON_STREAM csStdOut
 
CON_STREAM csStdErr
 

Detailed Description

Console I/O streams.

Definition in file stream.c.

Macro Definition Documentation

◆ _UNICODE

#define _UNICODE

Definition at line 28 of file stream.c.

◆ CON_STREAM_SET_MODE

#define CON_STREAM_SET_MODE (   Stream,
  Mode,
  CacheCodePage 
)
Value:
do { \
(Stream)->Mode = (Mode); \
\
if ((Mode) == AnsiText) \
(Stream)->CodePage = CacheCodePage; /* Possibly assigned */ \
else if ((Mode) == UTF8Text) \
(Stream)->CodePage = CP_UTF8; /* Fixed */ \
else /* Mode == Binary, WideText, UTF16Text */ \
(Stream)->CodePage = INVALID_CP; /* Not assigned (meaningless) */ \
} while(0)
_In_ ULONG Mode
Definition: hubbusif.h:303
static IStream Stream
Definition: htmldoc.c:1115
#define CP_UTF8
Definition: nls.h:20
@ AnsiText
Definition: stream.h:47
@ UTF8Text
Definition: stream.h:50
#define INVALID_CP
Definition: stream.h:53

Definition at line 110 of file stream.c.

◆ UNICODE

NOTE: Experimental! Don't use USE_CRT yet because output to console is a bit broken

Definition at line 27 of file stream.c.

Function Documentation

◆ ConStreamGetOSHandle()

HANDLE ConStreamGetOSHandle ( IN PCON_STREAM  Stream)

Definition at line 239 of file stream.c.

241{
242 /* Parameters validation */
243 if (!Stream)
245
246 /*
247 * See https://support.microsoft.com/kb/99173
248 * for more details.
249 */
250
251#ifdef USE_CRT
252 if (!Stream->fStream)
254
255 return (HANDLE)_get_osfhandle(_fileno(Stream->fStream));
256#else
257 return Stream->hHandle;
258#endif
259}
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
intptr_t CDECL _get_osfhandle(int fd)
Definition: file.c:2117
int CDECL _fileno(FILE *file)
Definition: file.c:1925

Referenced by _tmain(), BreakHandler(), Cleanup(), cmd_beep(), cmd_start(), CommandColor(), ConClearLine(), ConClearScreen(), ConGetScreenInfo(), Execute(), GetHandle(), Initialize(), InputWait(), and PagePrompt().

◆ ConStreamInit()

BOOL ConStreamInit ( OUT PCON_STREAM  Stream,
IN PVOID  Handle,
IN CON_STREAM_MODE  Mode,
IN UINT CacheCodePage  OPTIONAL 
)

Definition at line 184 of file stream.c.

189{
190 return ConStreamInitEx(Stream, Handle, Mode, CacheCodePage, ConWrite);
191}
ULONG Handle
Definition: gdb_input.c:15
INT __stdcall ConWrite(IN PCON_STREAM Stream, IN PCTCH szStr, IN DWORD len)
Definition: outstream.c:84
BOOL ConStreamInitEx(OUT PCON_STREAM Stream, IN PVOID Handle, IN CON_STREAM_MODE Mode, IN UINT CacheCodePage OPTIONAL, IN CON_WRITE_FUNC WriteFunc OPTIONAL)
Definition: stream.c:126

Referenced by _tmain(), and wmain().

◆ ConStreamInitEx()

BOOL ConStreamInitEx ( OUT PCON_STREAM  Stream,
IN PVOID  Handle,
IN CON_STREAM_MODE  Mode,
IN UINT CacheCodePage  OPTIONAL,
IN CON_WRITE_FUNC WriteFunc  OPTIONAL 
)

Definition at line 126 of file stream.c.

133{
134 /* Parameters validation */
135 if (!Stream || !Handle || (Mode > UTF8Text))
136 return FALSE;
137
138#ifdef USE_CRT
139
140 Stream->fStream = (FILE*)Handle;
141
142#else
143
145 return FALSE;
146
147 /*
148 * As the user calls us by giving us an existing handle to attach on,
149 * it is not our duty to close it if we are called again. The user
150 * is responsible for having opened those handles, and is responsible
151 * for closing them!
152 */
153#if 0
154 /* Attempt to close the handle of the old stream */
155 if (/* Stream->IsInitialized && */ Stream->hHandle &&
156 Stream->hHandle != INVALID_HANDLE_VALUE)
157 {
158 CloseHandle(Stream->hHandle);
159 }
160#endif
161
162 /* Initialize the stream critical section if not already done */
163 if (!Stream->IsInitialized)
164 {
165 InitializeCriticalSection/*AndSpinCount*/(&Stream->Lock /* , 4000 */);
166 Stream->IsInitialized = TRUE;
167 }
168
169 Stream->hHandle = (HANDLE)Handle;
170 Stream->IsConsole = IsConsoleHandle(Stream->hHandle);
171
172#endif /* defined(USE_CRT) */
173
174 /* Set the correct file translation mode */
175 CON_STREAM_SET_MODE(Stream, Mode, CacheCodePage);
176
177 /* Use the default 'ConWrite' helper if nothing is specified */
178 Stream->WriteFunc = (WriteFunc ? WriteFunc : ConWrite);
179
180 return TRUE;
181}
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
#define IsConsoleHandle(h)
Definition: console.h:14
#define CON_STREAM_SET_MODE(Stream, Mode, CacheCodePage)
Definition: stream.c:110
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:687
PVOID HANDLE
Definition: typedefs.h:73

Referenced by ConStreamInit().

◆ ConStreamSetCacheCodePage()

BOOL ConStreamSetCacheCodePage ( IN PCON_STREAM  Stream,
IN UINT  CacheCodePage 
)

Definition at line 214 of file stream.c.

217{
218#ifdef USE_CRT
219// FIXME!
220#warning The ConStreamSetCacheCodePage function does not make much sense with the CRT!
221#else
223
224 /* Parameters validation */
225 if (!Stream)
226 return FALSE;
227
228 /*
229 * Keep the original stream mode but set the correct file code page
230 * (will be reset only if Mode == AnsiText).
231 */
232 Mode = Stream->Mode;
233 CON_STREAM_SET_MODE(Stream, Mode, CacheCodePage);
234#endif
235 return TRUE;
236}
enum _CON_STREAM_MODE CON_STREAM_MODE

◆ ConStreamSetMode()

BOOL ConStreamSetMode ( IN PCON_STREAM  Stream,
IN CON_STREAM_MODE  Mode,
IN UINT CacheCodePage  OPTIONAL 
)

Definition at line 194 of file stream.c.

198{
199 /* Parameters validation */
200 if (!Stream || (Mode > UTF8Text))
201 return FALSE;
202
203#ifdef USE_CRT
204 if (!Stream->fStream)
205 return FALSE;
206#endif
207
208 /* Set the correct file translation mode */
209 CON_STREAM_SET_MODE(Stream, Mode, CacheCodePage);
210 return TRUE;
211}

Referenced by Initialize().

◆ ConStreamSetOSHandle()

BOOL ConStreamSetOSHandle ( IN PCON_STREAM  Stream,
IN HANDLE  Handle 
)

Definition at line 262 of file stream.c.

265{
266 /* Parameters validation */
267 if (!Stream)
268 return FALSE;
269
270 /*
271 * See https://support.microsoft.com/kb/99173
272 * for more details.
273 */
274
275#ifdef USE_CRT
276 if (!Stream->fStream)
277 return FALSE;
278
279 int fdOut = _open_osfhandle((intptr_t)Handle, _O_TEXT /* FIXME! */);
280 FILE* fpOut = _fdopen(fdOut, "w");
281 *Stream->fStream = *fpOut;
283
284 return TRUE;
285#else
286 /* Flush the stream and reset its handle */
287 if (Stream->hHandle != INVALID_HANDLE_VALUE)
288 FlushFileBuffers(Stream->hHandle);
289
290 Stream->hHandle = Handle;
291 Stream->IsConsole = IsConsoleHandle(Stream->hHandle);
292
293 // NOTE: Mode reset??
294
295 return TRUE;
296#endif
297}
#define _O_TEXT
Definition: cabinet.h:50
BOOL WINAPI FlushFileBuffers(IN HANDLE hFile)
Definition: fileinfo.c:25
int CDECL _open_osfhandle(intptr_t handle, int oflags)
Definition: file.c:2711
FILE *CDECL _fdopen(int fd, const char *mode)
Definition: file.c:1847
int intptr_t
Definition: corecrt.h:176

Referenced by SetHandle(), and wmain().

Variable Documentation

◆ csStdErr

CON_STREAM csStdErr

Definition at line 60 of file stream.c.

◆ csStdIn

CON_STREAM csStdIn

Definition at line 58 of file stream.c.

◆ csStdOut

CON_STREAM csStdOut

Definition at line 59 of file stream.c.