ReactOS 0.4.15-dev-8093-g3285f69
stream.c File Reference

Console I/O streams. More...

#include <windef.h>
#include <winbase.h>
#include <winnls.h>
#include <wincon.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 111 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 240 of file stream.c.

242{
243 /* Parameters validation */
244 if (!Stream)
246
247 /*
248 * See https://support.microsoft.com/kb/99173
249 * for more details.
250 */
251
252#ifdef USE_CRT
253 if (!Stream->fStream)
255
256 return (HANDLE)_get_osfhandle(_fileno(Stream->fStream));
257#else
258 return Stream->hHandle;
259#endif
260}
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
_Check_return_ _CRTIMP int __cdecl _fileno(_In_ FILE *_File)
_CRTIMP intptr_t __cdecl _get_osfhandle(_In_ int _FileHandle)

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 185 of file stream.c.

190{
191 return ConStreamInitEx(Stream, Handle, Mode, CacheCodePage, ConWrite);
192}
ULONG Handle
Definition: gdb_input.c:15
INT __stdcall ConWrite(IN PCON_STREAM Stream, IN PCTCH szStr, IN DWORD len)
Definition: outstream.c:85
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:127

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 127 of file stream.c.

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

Referenced by ConStreamInit().

◆ ConStreamSetCacheCodePage()

BOOL ConStreamSetCacheCodePage ( IN PCON_STREAM  Stream,
IN UINT  CacheCodePage 
)

Definition at line 215 of file stream.c.

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

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

Referenced by Initialize().

◆ ConStreamSetOSHandle()

BOOL ConStreamSetOSHandle ( IN PCON_STREAM  Stream,
IN HANDLE  Handle 
)

Definition at line 263 of file stream.c.

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

Referenced by SetHandle(), and wmain().

Variable Documentation

◆ csStdErr

CON_STREAM csStdErr

Definition at line 61 of file stream.c.

◆ csStdIn

CON_STREAM csStdIn

Definition at line 59 of file stream.c.

◆ csStdOut

CON_STREAM csStdOut

Definition at line 60 of file stream.c.