xref: /reactos/sdk/lib/conutils/stream.h (revision c2c66aff)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS Console Utilities Library
4  * FILE:            sdk/lib/conutils/stream.h
5  * PURPOSE:         Provides basic abstraction wrappers around CRT streams or
6  *                  Win32 console API I/O functions, to deal with i18n + Unicode
7  *                  related problems.
8  * PROGRAMMERS:     - Hermes Belusca-Maito (for the library);
9  *                  - All programmers who wrote the different console applications
10  *                    from which I took those functions and improved them.
11  */
12 
13 #ifndef __STREAM_H__
14 #define __STREAM_H__
15 
16 /*
17  * Enable this define if you want to only use CRT functions to output
18  * UNICODE stream to the console, as in the way explained by
19  * http://archives.miloush.net/michkap/archive/2008/03/18/8306597.html
20  */
21 /** NOTE: Experimental! Don't use USE_CRT yet because output to console is a bit broken **/
22 // #define USE_CRT
23 
24 #ifndef _UNICODE
25 #error The ConUtils library at the moment only supports compilation with _UNICODE defined!
26 #endif
27 
28 /*
29  * Console I/O streams
30  */
31 
32 /*
33  * See http://archives.miloush.net/michkap/archive/2009/08/14/9869928.html
34  * for more information.
35  */
36 typedef enum _CON_STREAM_MODE
37 {
38     Binary = 0, // #define _O_BINARY    0x8000  // file mode is binary (untranslated)
39                 // #define _O_RAW       _O_BINARY
40     AnsiText,   // #define _O_TEXT      0x4000  // file mode is text (translated) -- "ANSI"
41     WideText,   // #define _O_WTEXT     0x10000 // file mode is UTF16 with BOM (translated) -- "Unicode" of Windows
42     UTF16Text,  // #define _O_U16TEXT   0x20000 // file mode is UTF16   no BOM (translated) --    ""          ""
43     UTF8Text,   // #define _O_U8TEXT    0x40000 // file mode is UTF8    no BOM (translated)
44 } CON_STREAM_MODE, *PCON_STREAM_MODE;
45 
46 #define INVALID_CP  ((UINT)-1)
47 
48 // Shadow type, implementation-specific
49 typedef struct _CON_STREAM CON_STREAM, *PCON_STREAM;
50 
51 // typedef INT (__stdcall *CON_READ_FUNC)(IN PCON_STREAM, IN PTCHAR, IN DWORD);
52                                         // Stream,         szStr,     len
53 typedef INT (__stdcall *CON_WRITE_FUNC)(IN PCON_STREAM, IN PTCHAR, IN DWORD);
54 
55 /*
56  * Standard console streams, initialized by
57  * calls to ConStreamInit/ConInitStdStreams.
58  */
59 #if 0 // FIXME!
60 extern CON_STREAM StdStreams[3];
61 #define StdIn   (&StdStreams[0])
62 #define StdOut  (&StdStreams[1])
63 #define StdErr  (&StdStreams[2])
64 #else
65 extern CON_STREAM csStdIn;
66 extern CON_STREAM csStdOut;
67 extern CON_STREAM csStdErr;
68 #define StdIn   (&csStdIn )
69 #define StdOut  (&csStdOut)
70 #define StdErr  (&csStdErr)
71 #endif
72 
73 BOOL
74 ConStreamInitEx(
75     OUT PCON_STREAM Stream,
76     IN  PVOID Handle,
77     IN  CON_STREAM_MODE Mode,
78     IN  UINT CacheCodePage OPTIONAL,
79     // IN ReadWriteMode ????
80     // IN  CON_READ_FUNC ReadFunc OPTIONAL,
81     IN  CON_WRITE_FUNC WriteFunc OPTIONAL);
82 
83 BOOL
84 ConStreamInit(
85     OUT PCON_STREAM Stream,
86     IN  PVOID Handle,
87     // IN ReadWriteMode ????
88     IN  CON_STREAM_MODE Mode,
89     IN  UINT CacheCodePage OPTIONAL);
90 
91 
92 /* Console Standard Streams initialization helpers */
93 #ifdef USE_CRT
94 #define ConInitStdStreamsAndMode(Mode, CacheCodePage) \
95 do { \
96     ConStreamInit(StdIn , stdin , (Mode), (CacheCodePage)); \
97     ConStreamInit(StdOut, stdout, (Mode), (CacheCodePage)); \
98     ConStreamInit(StdErr, stderr, (Mode), (CacheCodePage)); \
99 } while(0)
100 #else
101 #define ConInitStdStreamsAndMode(Mode, CacheCodePage) \
102 do { \
103     ConStreamInit(StdIn , GetStdHandle(STD_INPUT_HANDLE) , (Mode), (CacheCodePage)); \
104     ConStreamInit(StdOut, GetStdHandle(STD_OUTPUT_HANDLE), (Mode), (CacheCodePage)); \
105     ConStreamInit(StdErr, GetStdHandle(STD_ERROR_HANDLE) , (Mode), (CacheCodePage)); \
106 } while(0)
107 #endif /* defined(USE_CRT) */
108 
109 #ifdef _UNICODE
110 /*
111  * Use UTF8 by default for file output, because this mode is back-compatible
112  * with ANSI, and it displays nice on terminals that support UTF8 by default
113  * (not many terminals support UTF16 on the contrary).
114  */
115 #define ConInitStdStreams() \
116     ConInitStdStreamsAndMode(UTF8Text, INVALID_CP); // Cache code page unused
117 #else
118 /* Use ANSI by default for file output */
119 #define ConInitStdStreams() \
120     ConInitStdStreamsAndMode(AnsiText, INVALID_CP);
121 #endif /* defined(_UNICODE) */
122 
123 /* Stream translation modes */
124 BOOL
125 ConStreamSetMode(
126     IN PCON_STREAM Stream,
127     IN CON_STREAM_MODE Mode,
128     IN UINT CacheCodePage OPTIONAL);
129 
130 #ifdef USE_CRT
131 // FIXME!
132 #warning The ConStreamSetCacheCodePage function does not make much sense with the CRT!
133 #else
134 BOOL
135 ConStreamSetCacheCodePage(
136     IN PCON_STREAM Stream,
137     IN UINT CacheCodePage);
138 #endif
139 
140 HANDLE
141 ConStreamGetOSHandle(
142     IN PCON_STREAM Stream);
143 
144 BOOL
145 ConStreamSetOSHandle(
146     IN PCON_STREAM Stream,
147     IN HANDLE Handle);
148 
149 
150 /*
151  * Console I/O utility API
152  * (for the moment, only Output)
153  */
154 
155 INT
156 __stdcall
157 ConWrite(
158     IN PCON_STREAM Stream,
159     IN PTCHAR szStr,
160     IN DWORD len);
161 
162 INT
163 ConStreamWrite(
164     IN PCON_STREAM Stream,
165     IN PTCHAR szStr,
166     IN DWORD len);
167 
168 INT
169 ConPuts(
170     IN PCON_STREAM Stream,
171     IN LPWSTR szStr);
172 
173 INT
174 ConPrintfV(
175     IN PCON_STREAM Stream,
176     IN LPWSTR  szStr,
177     IN va_list args); // arg_ptr
178 
179 INT
180 __cdecl
181 ConPrintf(
182     IN PCON_STREAM Stream,
183     IN LPWSTR szStr,
184     ...);
185 
186 INT
187 ConResPutsEx(
188     IN PCON_STREAM Stream,
189     IN HINSTANCE hInstance OPTIONAL,
190     IN UINT uID);
191 
192 INT
193 ConResPuts(
194     IN PCON_STREAM Stream,
195     IN UINT uID);
196 
197 INT
198 ConResPrintfExV(
199     IN PCON_STREAM Stream,
200     IN HINSTANCE hInstance OPTIONAL,
201     IN UINT    uID,
202     IN va_list args); // arg_ptr
203 
204 INT
205 ConResPrintfV(
206     IN PCON_STREAM Stream,
207     IN UINT    uID,
208     IN va_list args); // arg_ptr
209 
210 INT
211 __cdecl
212 ConResPrintfEx(
213     IN PCON_STREAM Stream,
214     IN HINSTANCE hInstance OPTIONAL,
215     IN UINT uID,
216     ...);
217 
218 INT
219 __cdecl
220 ConResPrintf(
221     IN PCON_STREAM Stream,
222     IN UINT uID,
223     ...);
224 
225 INT
226 ConMsgPuts(
227     IN PCON_STREAM Stream,
228     IN DWORD   dwFlags,
229     IN LPCVOID lpSource OPTIONAL,
230     IN DWORD   dwMessageId,
231     IN DWORD   dwLanguageId);
232 
233 INT
234 ConMsgPrintf2V(
235     IN PCON_STREAM Stream,
236     IN DWORD   dwFlags,
237     IN LPCVOID lpSource OPTIONAL,
238     IN DWORD   dwMessageId,
239     IN DWORD   dwLanguageId,
240     IN va_list args); // arg_ptr
241 
242 INT
243 ConMsgPrintfV(
244     IN PCON_STREAM Stream,
245     IN DWORD   dwFlags,
246     IN LPCVOID lpSource OPTIONAL,
247     IN DWORD   dwMessageId,
248     IN DWORD   dwLanguageId,
249     IN va_list args); // arg_ptr
250 
251 INT
252 __cdecl
253 ConMsgPrintf(
254     IN PCON_STREAM Stream,
255     IN DWORD   dwFlags,
256     IN LPCVOID lpSource OPTIONAL,
257     IN DWORD   dwMessageId,
258     IN DWORD   dwLanguageId,
259     ...);
260 
261 
262 
263 VOID
264 ConClearLine(IN PCON_STREAM Stream);
265 
266 
267 #endif  /* __STREAM_H__ */
268