[CONUTILS]: Create a C library for console output/input functions, last-error message...
[reactos.git] / reactos / sdk / lib / conutils / conutils.h
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Console Utilities Library
4 * FILE: sdk/lib/conutils/conutils.h
5 * PURPOSE: Provides simple ready-to-use abstraction wrappers around
6 * CRT streams or Win32 console API I/O functions, to deal with
7 * i18n + Unicode related problems.
8 * PROGRAMMERS: - Hermes Belusca-Maito (for making this library);
9 * - All programmers who wrote the different console applications
10 * from which I took those functions and improved them.
11 */
12
13 #ifndef __CONUTILS_H__
14 #define __CONUTILS_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 only supports compilation with _UNICODE defined, at the moment!
26 #endif
27
28 /*
29 * General-purpose utility functions (wrappers around,
30 * or reimplementations of, Win32 APIs).
31 */
32
33 INT
34 WINAPI
35 K32LoadStringW(
36 IN HINSTANCE hInstance OPTIONAL,
37 IN UINT uID,
38 OUT LPWSTR lpBuffer,
39 IN INT nBufferMax);
40
41 DWORD
42 WINAPI
43 FormatMessageSafeW(
44 IN DWORD dwFlags,
45 IN LPCVOID lpSource OPTIONAL,
46 IN DWORD dwMessageId,
47 IN DWORD dwLanguageId,
48 OUT LPWSTR lpBuffer,
49 IN DWORD nSize,
50 IN va_list *Arguments OPTIONAL);
51
52
53 /*
54 * Console I/O streams
55 */
56
57 /*
58 * See http://archives.miloush.net/michkap/archive/2009/08/14/9869928.html
59 * for more information.
60 */
61 typedef enum _CON_STREAM_MODE
62 {
63 Binary = 0, // #define _O_BINARY 0x8000 // file mode is binary (untranslated)
64 // #define _O_RAW _O_BINARY
65 AnsiText, // #define _O_TEXT 0x4000 // file mode is text (translated) -- "ANSI"
66 WideText, // #define _O_WTEXT 0x10000 // file mode is UTF16 with BOM (translated) -- "Unicode" of Windows
67 UTF16Text, // #define _O_U16TEXT 0x20000 // file mode is UTF16 no BOM (translated) -- "" ""
68 UTF8Text, // #define _O_U8TEXT 0x40000 // file mode is UTF8 no BOM (translated)
69 } CON_STREAM_MODE, *PCON_STREAM_MODE;
70
71 // Shadow type, implementation-specific
72 typedef struct _CON_STREAM CON_STREAM, *PCON_STREAM;
73
74 // Stream, szStr, len
75 typedef INT (__stdcall *CON_WRITE_FUNC)(IN PCON_STREAM, IN PTCHAR, IN DWORD);
76
77 /*
78 * Standard console streams, initialized by
79 * calls to ConStreamInit/ConInitStdStreams.
80 */
81 #if 0 // FIXME!
82 extern CON_STREAM StdStreams[3];
83 #define StdIn (&StdStreams[0]) // TODO!
84 #define StdOut (&StdStreams[1])
85 #define StdErr (&StdStreams[2])
86 #else
87 extern CON_STREAM csStdIn;
88 extern CON_STREAM csStdOut;
89 extern CON_STREAM csStdErr;
90 #define StdIn (&csStdIn) // TODO!
91 #define StdOut (&csStdOut)
92 #define StdErr (&csStdErr)
93 #endif
94
95 // static
96 BOOL
97 IsConsoleHandle(IN HANDLE hHandle);
98
99 BOOL
100 ConStreamInitEx(
101 OUT PCON_STREAM Stream,
102 IN PVOID Handle,
103 IN CON_STREAM_MODE Mode,
104 IN CON_WRITE_FUNC WriteFunc OPTIONAL);
105
106 BOOL
107 ConStreamInit(
108 OUT PCON_STREAM Stream,
109 IN PVOID Handle,
110 IN CON_STREAM_MODE Mode);
111
112
113 /* Console Standard Streams initialization helpers */
114 #ifdef _UNICODE
115
116 #ifdef USE_CRT
117 #define ConInitStdStreams() \
118 do { \
119 ConStreamInit(StdOut, stdout, UTF16Text); \
120 ConStreamInit(StdErr, stderr, UTF16Text); \
121 } while(0)
122 #else
123 #define ConInitStdStreams() \
124 do { \
125 ConStreamInit(StdOut, GetStdHandle(STD_OUTPUT_HANDLE), UTF16Text); \
126 ConStreamInit(StdErr, GetStdHandle(STD_ERROR_HANDLE) , UTF16Text); \
127 } while(0)
128 #endif /* defined(USE_CRT) */
129
130 #else
131
132 #ifdef USE_CRT
133 #define ConInitStdStreams() \
134 do { \
135 ConStreamInit(StdOut, stdout, AnsiText); \
136 ConStreamInit(StdErr, stderr, AnsiText); \
137 } while(0)
138 #else
139 #define ConInitStdStreams() \
140 do { \
141 ConStreamInit(StdOut, GetStdHandle(STD_OUTPUT_HANDLE), AnsiText); \
142 ConStreamInit(StdErr, GetStdHandle(STD_ERROR_HANDLE) , AnsiText); \
143 } while(0)
144 #endif /* defined(USE_CRT) */
145
146 #endif /* defined(_UNICODE) */
147
148
149
150 /*
151 * Console I/O utility API
152 * (for the moment, only Output)
153 */
154
155 /*** Redundant defines to keep compat with existing code for now... ***/
156 /*** Must be removed later! ***/
157
158 #define CON_RC_STRING_MAX_SIZE 4096
159 #define MAX_BUFFER_SIZE 4096 // some exotic programs set it to 5024
160 #define OUTPUT_BUFFER_SIZE 4096
161 // MAX_STRING_SIZE
162
163 // #define MAX_MESSAGE_SIZE 512
164
165
166 INT
167 __stdcall
168 ConWrite(
169 IN PCON_STREAM Stream,
170 IN PTCHAR szStr,
171 IN DWORD len);
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 ConPrintf(
181 IN PCON_STREAM Stream,
182 IN LPWSTR szStr,
183 ...);
184
185 INT
186 ConResPrintfV(
187 IN PCON_STREAM Stream,
188 IN UINT uID,
189 IN va_list args); // arg_ptr
190
191 INT
192 ConResPrintf(
193 IN PCON_STREAM Stream,
194 IN UINT uID,
195 ...);
196
197 INT
198 ConMsgPrintf2V(
199 IN PCON_STREAM Stream,
200 IN DWORD dwFlags,
201 IN LPCVOID lpSource OPTIONAL,
202 IN DWORD dwMessageId,
203 IN DWORD dwLanguageId,
204 IN va_list args); // arg_ptr
205
206 INT
207 ConMsgPrintfV(
208 IN PCON_STREAM Stream,
209 IN DWORD dwFlags,
210 IN LPCVOID lpSource OPTIONAL,
211 IN DWORD dwMessageId,
212 IN DWORD dwLanguageId,
213 IN va_list args); // arg_ptr
214
215 INT
216 ConMsgPrintf(
217 IN PCON_STREAM Stream,
218 IN DWORD dwFlags,
219 IN LPCVOID lpSource OPTIONAL,
220 IN DWORD dwMessageId,
221 IN DWORD dwLanguageId,
222 ...);
223
224
225 /*
226 * Those are compatibility #defines for old code!
227 */
228
229 /*** tree.c ***/
230
231 #define PrintStringV(szStr, args) \
232 ConPrintfV(StdOut, (szStr), (args))
233 #define PrintString(szStr, ...) \
234 ConPrintf(StdOut, (szStr), ##__VA_ARGS__)
235
236 /*** network/net/main.c ***/
237 #define PrintToConsole(szStr, ...) \
238 ConPrintf(StdOut, (szStr), ##__VA_ARGS__)
239
240 /*** clip.c, comp.c, help.c, tree.c ***/
241 /*** subst.c ***/
242 /*** format.c, network/net/main.c, shutdown.c, wlanconf.c, diskpart.c ***/
243
244 #define PrintResourceStringV(uID, args) \
245 ConResPrintfV(StdOut, (uID), (args))
246 #define PrintResourceString(uID, ...) \
247 ConResPrintf(StdOut, (uID), ##__VA_ARGS__)
248
249 //
250 // TODO: Add Console paged-output printf & ResPrintf functions!
251 //
252
253 #endif /* __CONUTILS_H__ */