Reactos

[CONUTILS] Use the currently active console codepage to output text to files. If you want to output UTF-8 run "chcp 65001" before. CORE-12122

+33 -37
+19 -15
sdk/lib/conutils/outstream.c
··· 103 103 // if (IsConsoleHandle(Stream->hHandle)) 104 104 if (Stream->IsConsole) 105 105 { 106 - // TODO: Check if (ConStream->Mode == WideText or UTF16Text) ?? 106 + // TODO: Check if (Stream->Mode == WideText or UTF16Text) ?? 107 107 108 108 /* 109 109 * This code is inspired from _cputws, in particular from the fact that, ··· 136 136 * 137 137 * Implementation NOTE: 138 138 * MultiByteToWideChar (resp. WideCharToMultiByte) are equivalent to 139 - * OemToCharBuffW (resp. CharToOemBuffW), but the latters uselessly 140 - * depend on user32.dll, while MultiByteToWideChar and WideCharToMultiByte 141 - * only need kernel32.dll. 139 + * OemToCharBuffW (resp. CharToOemBuffW), but these latter functions 140 + * uselessly depend on user32.dll, while MultiByteToWideChar and 141 + * WideCharToMultiByte only need kernel32.dll. 142 142 */ 143 143 if ((Stream->Mode == WideText) || (Stream->Mode == UTF16Text)) 144 144 { 145 145 #ifndef _UNICODE // UNICODE means that TCHAR == WCHAR == UTF-16 146 - /* Convert from the current process/thread's codepage to UTF-16 */ 147 - WCHAR *buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR)); 146 + /* Convert from the current process/thread's code page to UTF-16 */ 147 + PWCHAR buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR)); 148 148 if (!buffer) 149 149 { 150 150 SetLastError(ERROR_NOT_ENOUGH_MEMORY); ··· 200 200 } 201 201 else if ((Stream->Mode == UTF8Text) || (Stream->Mode == AnsiText)) 202 202 { 203 - CHAR *buffer; 203 + UINT CodePage; 204 + PCHAR buffer; 204 205 205 206 /* 206 - * Resolve the codepage cache if it was not assigned yet 207 - * (only if the stream is in ANSI mode; in UTF8 mode the 208 - * codepage was already set to CP_UTF8). 207 + * Resolve the current code page if it has not been assigned yet 208 + * (we do this only if the stream is in ANSI mode; in UTF8 mode 209 + * the code page is always set to CP_UTF8). Otherwise use the 210 + * current stream's code page. 209 211 */ 210 212 if (/*(Stream->Mode == AnsiText) &&*/ (Stream->CodePage == INVALID_CP)) 211 - Stream->CodePage = GetConsoleOutputCP(); // CP_ACP, CP_OEMCP 213 + CodePage = GetConsoleOutputCP(); // CP_ACP, CP_OEMCP 214 + else 215 + CodePage = Stream->CodePage; 212 216 213 217 #ifdef _UNICODE // UNICODE means that TCHAR == WCHAR == UTF-16 214 - /* Convert from UTF-16 to either UTF-8 or ANSI, using stream codepage */ 218 + /* Convert from UTF-16 to either UTF-8 or ANSI, using the stream code page */ 215 219 // NOTE: MB_LEN_MAX defined either in limits.h or in stdlib.h . 216 220 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * MB_LEN_MAX); 217 221 if (!buffer) ··· 219 223 SetLastError(ERROR_NOT_ENOUGH_MEMORY); 220 224 return 0; 221 225 } 222 - len = WideCharToMultiByte(Stream->CodePage, 0, 226 + len = WideCharToMultiByte(CodePage, 0, 223 227 szStr, len, buffer, len * MB_LEN_MAX, 224 228 NULL, NULL); 225 229 szStr = (PVOID)buffer; 226 230 #else 227 231 /* 228 - * Convert from the current process/thread's codepage to either 229 - * UTF-8 or ANSI, using stream codepage. 232 + * Convert from the current process/thread's code page to either 233 + * UTF-8 or ANSI, using the stream code page. 230 234 * We need to perform a double conversion, by going through UTF-16. 231 235 */ 232 236 // TODO!
+9 -11
sdk/lib/conutils/stream.c
··· 73 73 _O_U16TEXT, // UTF16Text (UTF16 without BOM; translated) 74 74 _O_U8TEXT, // UTF8Text (UTF8 without BOM; translated) 75 75 }; 76 - #endif 77 - 78 - #ifdef USE_CRT 79 76 80 77 /* 81 78 * See http://archives.miloush.net/michkap/archive/2008/03/18/8306597.html ··· 83 80 * for more details. 84 81 */ 85 82 86 - // NOTE: May the translated mode be cached somehow? 83 + // NOTE1: May the translated mode be cached somehow? 87 84 // NOTE2: We may also call IsConsoleHandle to directly set the mode to 88 85 // _O_U16TEXT if it's ok?? 89 86 // NOTE3: _setmode returns the previous mode, or -1 if failure. ··· 99 96 #else /* defined(USE_CRT) */ 100 97 101 98 /* 102 - * We set Stream->CodePage to INVALID_CP (= -1) to signal that the codepage 99 + * We set Stream->CodePage to INVALID_CP (== -1) to signal that the code page 103 100 * is either not assigned (if the mode is Binary, WideText, or UTF16Text), or 104 - * is not cached yet (if the mode is AnsiText). In this latter case the cache 105 - * is resolved inside ConWrite. Finally, if the mode is UTF8Text, the codepage 106 - * cache is set to CP_UTF8. 107 - * The codepage cache can be reset by an explicit call to CON_STREAM_SET_MODE 101 + * is not cached (if the mode is AnsiText). In this latter case the code page 102 + * is resolved inside ConWrite. Finally, if the mode is UTF8Text, the code page 103 + * cache is always set to CP_UTF8. 104 + * The code page cache can be reset by an explicit call to CON_STREAM_SET_MODE 108 105 * (i.e. by calling ConStreamSetMode, or by reinitializing the stream with 109 106 * ConStreamInit(Ex)). 110 107 * 111 - * NOTE: the magic value could not be '0' since it is reserved for CP_ACP. 108 + * NOTE: the reserved values are: 0 (CP_ACP), 1 (CP_OEMCP), 2 (CP_MACCP), 109 + * 3 (CP_THREAD_ACP), 42 (CP_SYMBOL), 65000 (CP_UTF7) and 65001 (CP_UTF8). 112 110 */ 113 111 #define CON_STREAM_SET_MODE(Stream, Mode, CacheCodePage) \ 114 112 do { \ ··· 229 227 return FALSE; 230 228 231 229 /* 232 - * Keep the original stream mode but set the correct file codepage 230 + * Keep the original stream mode but set the correct file code page 233 231 * (will be reset only if Mode == AnsiText). 234 232 */ 235 233 Mode = Stream->Mode;
+4 -10
sdk/lib/conutils/stream.h
··· 113 113 } while(0) 114 114 #endif /* defined(USE_CRT) */ 115 115 116 - #ifdef _UNICODE 117 116 /* 118 - * Use UTF8 by default for file output, because this mode is back-compatible 119 - * with ANSI, and it displays nice on terminals that support UTF8 by default 120 - * (not many terminals support UTF16 on the contrary). 117 + * Use ANSI by default for file output, with no cached code page. 118 + * Note that setting the stream mode to AnsiText and the code page value 119 + * to CP_UTF8 sets the stream to UTF8 mode, and has the same effect as if 120 + * the stream mode UTF8Text had been specified instead. 121 121 */ 122 122 #define ConInitStdStreams() \ 123 - ConInitStdStreamsAndMode(UTF8Text, INVALID_CP) 124 - /* Note that here the cache code page is unused */ 125 - #else 126 - /* Use ANSI by default for file output */ 127 - #define ConInitStdStreams() \ 128 123 ConInitStdStreamsAndMode(AnsiText, INVALID_CP) 129 - #endif /* defined(_UNICODE) */ 130 124 131 125 /* Stream translation modes */ 132 126 BOOL
+1 -1
sdk/lib/conutils/stream_private.h
··· 46 46 * when 'hHandle' refers to a file or a pipe. 47 47 */ 48 48 CON_STREAM_MODE Mode; 49 - UINT CodePage; // Used to convert UTF-16 text to some ANSI codepage. 49 + UINT CodePage; // Used to convert UTF-16 text to some ANSI code page. 50 50 #endif /* defined(USE_CRT) */ 51 51 } CON_STREAM, *PCON_STREAM; 52 52