···103103 // if (IsConsoleHandle(Stream->hHandle))
104104 if (Stream->IsConsole)
105105 {
106106- // TODO: Check if (ConStream->Mode == WideText or UTF16Text) ??
106106+ // TODO: Check if (Stream->Mode == WideText or UTF16Text) ??
107107108108 /*
109109 * This code is inspired from _cputws, in particular from the fact that,
···136136 *
137137 * Implementation NOTE:
138138 * MultiByteToWideChar (resp. WideCharToMultiByte) are equivalent to
139139- * OemToCharBuffW (resp. CharToOemBuffW), but the latters uselessly
140140- * depend on user32.dll, while MultiByteToWideChar and WideCharToMultiByte
141141- * only need kernel32.dll.
139139+ * OemToCharBuffW (resp. CharToOemBuffW), but these latter functions
140140+ * uselessly depend on user32.dll, while MultiByteToWideChar and
141141+ * WideCharToMultiByte only need kernel32.dll.
142142 */
143143 if ((Stream->Mode == WideText) || (Stream->Mode == UTF16Text))
144144 {
145145#ifndef _UNICODE // UNICODE means that TCHAR == WCHAR == UTF-16
146146- /* Convert from the current process/thread's codepage to UTF-16 */
147147- WCHAR *buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR));
146146+ /* Convert from the current process/thread's code page to UTF-16 */
147147+ PWCHAR buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR));
148148 if (!buffer)
149149 {
150150 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
···200200 }
201201 else if ((Stream->Mode == UTF8Text) || (Stream->Mode == AnsiText))
202202 {
203203- CHAR *buffer;
203203+ UINT CodePage;
204204+ PCHAR buffer;
204205205206 /*
206206- * Resolve the codepage cache if it was not assigned yet
207207- * (only if the stream is in ANSI mode; in UTF8 mode the
208208- * codepage was already set to CP_UTF8).
207207+ * Resolve the current code page if it has not been assigned yet
208208+ * (we do this only if the stream is in ANSI mode; in UTF8 mode
209209+ * the code page is always set to CP_UTF8). Otherwise use the
210210+ * current stream's code page.
209211 */
210212 if (/*(Stream->Mode == AnsiText) &&*/ (Stream->CodePage == INVALID_CP))
211211- Stream->CodePage = GetConsoleOutputCP(); // CP_ACP, CP_OEMCP
213213+ CodePage = GetConsoleOutputCP(); // CP_ACP, CP_OEMCP
214214+ else
215215+ CodePage = Stream->CodePage;
212216213217#ifdef _UNICODE // UNICODE means that TCHAR == WCHAR == UTF-16
214214- /* Convert from UTF-16 to either UTF-8 or ANSI, using stream codepage */
218218+ /* Convert from UTF-16 to either UTF-8 or ANSI, using the stream code page */
215219 // NOTE: MB_LEN_MAX defined either in limits.h or in stdlib.h .
216220 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * MB_LEN_MAX);
217221 if (!buffer)
···219223 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
220224 return 0;
221225 }
222222- len = WideCharToMultiByte(Stream->CodePage, 0,
226226+ len = WideCharToMultiByte(CodePage, 0,
223227 szStr, len, buffer, len * MB_LEN_MAX,
224228 NULL, NULL);
225229 szStr = (PVOID)buffer;
226230#else
227231 /*
228228- * Convert from the current process/thread's codepage to either
229229- * UTF-8 or ANSI, using stream codepage.
232232+ * Convert from the current process/thread's code page to either
233233+ * UTF-8 or ANSI, using the stream code page.
230234 * We need to perform a double conversion, by going through UTF-16.
231235 */
232236 // TODO!
+9-11
sdk/lib/conutils/stream.c
···7373 _O_U16TEXT, // UTF16Text (UTF16 without BOM; translated)
7474 _O_U8TEXT, // UTF8Text (UTF8 without BOM; translated)
7575};
7676-#endif
7777-7878-#ifdef USE_CRT
79768077/*
8178 * See http://archives.miloush.net/michkap/archive/2008/03/18/8306597.html
···8380 * for more details.
8481 */
85828686-// NOTE: May the translated mode be cached somehow?
8383+// NOTE1: May the translated mode be cached somehow?
8784// NOTE2: We may also call IsConsoleHandle to directly set the mode to
8885// _O_U16TEXT if it's ok??
8986// NOTE3: _setmode returns the previous mode, or -1 if failure.
···9996#else /* defined(USE_CRT) */
1009710198/*
102102- * We set Stream->CodePage to INVALID_CP (= -1) to signal that the codepage
9999+ * We set Stream->CodePage to INVALID_CP (== -1) to signal that the code page
103100 * is either not assigned (if the mode is Binary, WideText, or UTF16Text), or
104104- * is not cached yet (if the mode is AnsiText). In this latter case the cache
105105- * is resolved inside ConWrite. Finally, if the mode is UTF8Text, the codepage
106106- * cache is set to CP_UTF8.
107107- * The codepage cache can be reset by an explicit call to CON_STREAM_SET_MODE
101101+ * is not cached (if the mode is AnsiText). In this latter case the code page
102102+ * is resolved inside ConWrite. Finally, if the mode is UTF8Text, the code page
103103+ * cache is always set to CP_UTF8.
104104+ * The code page cache can be reset by an explicit call to CON_STREAM_SET_MODE
108105 * (i.e. by calling ConStreamSetMode, or by reinitializing the stream with
109106 * ConStreamInit(Ex)).
110107 *
111111- * NOTE: the magic value could not be '0' since it is reserved for CP_ACP.
108108+ * NOTE: the reserved values are: 0 (CP_ACP), 1 (CP_OEMCP), 2 (CP_MACCP),
109109+ * 3 (CP_THREAD_ACP), 42 (CP_SYMBOL), 65000 (CP_UTF7) and 65001 (CP_UTF8).
112110 */
113111#define CON_STREAM_SET_MODE(Stream, Mode, CacheCodePage) \
114112do { \
···229227 return FALSE;
230228231229 /*
232232- * Keep the original stream mode but set the correct file codepage
230230+ * Keep the original stream mode but set the correct file code page
233231 * (will be reset only if Mode == AnsiText).
234232 */
235233 Mode = Stream->Mode;
+4-10
sdk/lib/conutils/stream.h
···113113} while(0)
114114#endif /* defined(USE_CRT) */
115115116116-#ifdef _UNICODE
117116/*
118118- * Use UTF8 by default for file output, because this mode is back-compatible
119119- * with ANSI, and it displays nice on terminals that support UTF8 by default
120120- * (not many terminals support UTF16 on the contrary).
117117+ * Use ANSI by default for file output, with no cached code page.
118118+ * Note that setting the stream mode to AnsiText and the code page value
119119+ * to CP_UTF8 sets the stream to UTF8 mode, and has the same effect as if
120120+ * the stream mode UTF8Text had been specified instead.
121121 */
122122#define ConInitStdStreams() \
123123- ConInitStdStreamsAndMode(UTF8Text, INVALID_CP)
124124- /* Note that here the cache code page is unused */
125125-#else
126126-/* Use ANSI by default for file output */
127127-#define ConInitStdStreams() \
128123 ConInitStdStreamsAndMode(AnsiText, INVALID_CP)
129129-#endif /* defined(_UNICODE) */
130124131125/* Stream translation modes */
132126BOOL
+1-1
sdk/lib/conutils/stream_private.h
···4646 * when 'hHandle' refers to a file or a pipe.
4747 */
4848 CON_STREAM_MODE Mode;
4949- UINT CodePage; // Used to convert UTF-16 text to some ANSI codepage.
4949+ UINT CodePage; // Used to convert UTF-16 text to some ANSI code page.
5050#endif /* defined(USE_CRT) */
5151} CON_STREAM, *PCON_STREAM;
5252