Fossil SCM
write unicode to console, when possible.
Commit
7fd74e72f8ab8a0799b7b90aa758c894e6138bb2
Parent
a830168d3fe49e6…
2 files changed
+6
-23
+1
-1
+6
-23
| --- src/file.c | ||
| +++ src/file.c | ||
| @@ -1118,19 +1118,18 @@ | ||
| 1118 | 1118 | } |
| 1119 | 1119 | |
| 1120 | 1120 | /* |
| 1121 | 1121 | ** Display UTF8 on the console. Return the number of |
| 1122 | 1122 | ** Characters written. If stdout or stderr is redirected |
| 1123 | -** to a file, so it is not a console, -1 is returned and | |
| 1124 | -** nothing is written. | |
| 1123 | +** to a file, -1 is returned and ** nothing is written | |
| 1124 | +** to the console. | |
| 1125 | 1125 | */ |
| 1126 | 1126 | int fossil_utf8_to_console(const char *zUtf8, int nByte, int toStdErr){ |
| 1127 | 1127 | #ifdef _WIN32 |
| 1128 | 1128 | int nChar; |
| 1129 | - WCHAR *zUnicode; /* Unicode version of zUtf8 */ | |
| 1130 | - char *zConsole; /* Console version of zUtf8 */ | |
| 1131 | - int codepage; /* Console code page */ | |
| 1129 | + wchar_t *zUnicode; /* Unicode version of zUtf8 */ | |
| 1130 | + DWORD dummy; | |
| 1132 | 1131 | |
| 1133 | 1132 | static int once = 1; |
| 1134 | 1133 | static int istty[2]; |
| 1135 | 1134 | if( once ){ |
| 1136 | 1135 | istty[0] = _isatty(fileno(stdout)); |
| @@ -1150,28 +1149,12 @@ | ||
| 1150 | 1149 | nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, zUnicode, nChar); |
| 1151 | 1150 | if( nChar==0 ){ |
| 1152 | 1151 | free(zUnicode); |
| 1153 | 1152 | return 0; |
| 1154 | 1153 | } |
| 1155 | - zUnicode[nChar] = '\0'; | |
| 1156 | - codepage = GetConsoleCP(); | |
| 1157 | - nByte = WideCharToMultiByte(codepage, 0, zUnicode, nChar, 0, 0, 0, 0); | |
| 1158 | - zConsole = malloc( nByte + 1); | |
| 1159 | - if( zConsole==0 ){ | |
| 1160 | - free(zUnicode); | |
| 1161 | - return 0; | |
| 1162 | - } | |
| 1163 | - nByte = WideCharToMultiByte(codepage, 0, zUnicode, nChar, zConsole, nByte, 0, 0); | |
| 1164 | - zConsole[nByte] = '\0'; | |
| 1165 | - free(zUnicode); | |
| 1166 | - if( nByte == 0 ){ | |
| 1167 | - free(zConsole); | |
| 1168 | - zConsole = 0; | |
| 1169 | - return 0; | |
| 1170 | - } | |
| 1171 | - fwrite(zConsole, 1, nByte, toStdErr ? stderr : stdout); | |
| 1172 | - fflush(toStdErr ? stderr : stdout); | |
| 1154 | + zUnicode[nChar] = '\0';; | |
| 1155 | + WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE + toStdErr), zUnicode, nChar, &dummy, 0); | |
| 1173 | 1156 | return nChar; |
| 1174 | 1157 | #else |
| 1175 | 1158 | return -1; /* No-op on unix */ |
| 1176 | 1159 | #endif |
| 1177 | 1160 | } |
| 1178 | 1161 |
| --- src/file.c | |
| +++ src/file.c | |
| @@ -1118,19 +1118,18 @@ | |
| 1118 | } |
| 1119 | |
| 1120 | /* |
| 1121 | ** Display UTF8 on the console. Return the number of |
| 1122 | ** Characters written. If stdout or stderr is redirected |
| 1123 | ** to a file, so it is not a console, -1 is returned and |
| 1124 | ** nothing is written. |
| 1125 | */ |
| 1126 | int fossil_utf8_to_console(const char *zUtf8, int nByte, int toStdErr){ |
| 1127 | #ifdef _WIN32 |
| 1128 | int nChar; |
| 1129 | WCHAR *zUnicode; /* Unicode version of zUtf8 */ |
| 1130 | char *zConsole; /* Console version of zUtf8 */ |
| 1131 | int codepage; /* Console code page */ |
| 1132 | |
| 1133 | static int once = 1; |
| 1134 | static int istty[2]; |
| 1135 | if( once ){ |
| 1136 | istty[0] = _isatty(fileno(stdout)); |
| @@ -1150,28 +1149,12 @@ | |
| 1150 | nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, zUnicode, nChar); |
| 1151 | if( nChar==0 ){ |
| 1152 | free(zUnicode); |
| 1153 | return 0; |
| 1154 | } |
| 1155 | zUnicode[nChar] = '\0'; |
| 1156 | codepage = GetConsoleCP(); |
| 1157 | nByte = WideCharToMultiByte(codepage, 0, zUnicode, nChar, 0, 0, 0, 0); |
| 1158 | zConsole = malloc( nByte + 1); |
| 1159 | if( zConsole==0 ){ |
| 1160 | free(zUnicode); |
| 1161 | return 0; |
| 1162 | } |
| 1163 | nByte = WideCharToMultiByte(codepage, 0, zUnicode, nChar, zConsole, nByte, 0, 0); |
| 1164 | zConsole[nByte] = '\0'; |
| 1165 | free(zUnicode); |
| 1166 | if( nByte == 0 ){ |
| 1167 | free(zConsole); |
| 1168 | zConsole = 0; |
| 1169 | return 0; |
| 1170 | } |
| 1171 | fwrite(zConsole, 1, nByte, toStdErr ? stderr : stdout); |
| 1172 | fflush(toStdErr ? stderr : stdout); |
| 1173 | return nChar; |
| 1174 | #else |
| 1175 | return -1; /* No-op on unix */ |
| 1176 | #endif |
| 1177 | } |
| 1178 |
| --- src/file.c | |
| +++ src/file.c | |
| @@ -1118,19 +1118,18 @@ | |
| 1118 | } |
| 1119 | |
| 1120 | /* |
| 1121 | ** Display UTF8 on the console. Return the number of |
| 1122 | ** Characters written. If stdout or stderr is redirected |
| 1123 | ** to a file, -1 is returned and ** nothing is written |
| 1124 | ** to the console. |
| 1125 | */ |
| 1126 | int fossil_utf8_to_console(const char *zUtf8, int nByte, int toStdErr){ |
| 1127 | #ifdef _WIN32 |
| 1128 | int nChar; |
| 1129 | wchar_t *zUnicode; /* Unicode version of zUtf8 */ |
| 1130 | DWORD dummy; |
| 1131 | |
| 1132 | static int once = 1; |
| 1133 | static int istty[2]; |
| 1134 | if( once ){ |
| 1135 | istty[0] = _isatty(fileno(stdout)); |
| @@ -1150,28 +1149,12 @@ | |
| 1149 | nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, zUnicode, nChar); |
| 1150 | if( nChar==0 ){ |
| 1151 | free(zUnicode); |
| 1152 | return 0; |
| 1153 | } |
| 1154 | zUnicode[nChar] = '\0';; |
| 1155 | WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE + toStdErr), zUnicode, nChar, &dummy, 0); |
| 1156 | return nChar; |
| 1157 | #else |
| 1158 | return -1; /* No-op on unix */ |
| 1159 | #endif |
| 1160 | } |
| 1161 |
+1
-1
| --- src/printf.c | ||
| +++ src/printf.c | ||
| @@ -815,11 +815,11 @@ | ||
| 815 | 815 | ** a file, no translation occurs. No translation ever occurs on unix. |
| 816 | 816 | */ |
| 817 | 817 | void fossil_puts(const char *z, int toStdErr){ |
| 818 | 818 | #if defined(_WIN32) |
| 819 | 819 | if( fossil_utf8_to_console(z, strlen(z), toStdErr) >= 0 ){ |
| 820 | - return; | |
| 820 | + return; | |
| 821 | 821 | } |
| 822 | 822 | #endif |
| 823 | 823 | assert( toStdErr==0 || toStdErr==1 ); |
| 824 | 824 | fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout); |
| 825 | 825 | fflush(toStdErr ? stderr : stdout); |
| 826 | 826 |
| --- src/printf.c | |
| +++ src/printf.c | |
| @@ -815,11 +815,11 @@ | |
| 815 | ** a file, no translation occurs. No translation ever occurs on unix. |
| 816 | */ |
| 817 | void fossil_puts(const char *z, int toStdErr){ |
| 818 | #if defined(_WIN32) |
| 819 | if( fossil_utf8_to_console(z, strlen(z), toStdErr) >= 0 ){ |
| 820 | return; |
| 821 | } |
| 822 | #endif |
| 823 | assert( toStdErr==0 || toStdErr==1 ); |
| 824 | fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout); |
| 825 | fflush(toStdErr ? stderr : stdout); |
| 826 |
| --- src/printf.c | |
| +++ src/printf.c | |
| @@ -815,11 +815,11 @@ | |
| 815 | ** a file, no translation occurs. No translation ever occurs on unix. |
| 816 | */ |
| 817 | void fossil_puts(const char *z, int toStdErr){ |
| 818 | #if defined(_WIN32) |
| 819 | if( fossil_utf8_to_console(z, strlen(z), toStdErr) >= 0 ){ |
| 820 | return; |
| 821 | } |
| 822 | #endif |
| 823 | assert( toStdErr==0 || toStdErr==1 ); |
| 824 | fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout); |
| 825 | fflush(toStdErr ? stderr : stdout); |
| 826 |