Fossil SCM

When translating UTF8 text for display on the console, use the codepage obtained from GetConsoleCP(), not the CP_ACP code page that is used for system calls.

drh 2011-05-13 15:46 windows-i18n
Commit 55b32701a573b63509701aab405c2d424ce0ac62
2 files changed +50 -4 +1 -2
+50 -4
--- src/file.c
+++ src/file.c
@@ -695,14 +695,18 @@
695695
/**************************************************************************
696696
** The following routines translate between MBCS and UTF8 on windows.
697697
** Since everything is always UTF8 on unix, these routines are no-ops
698698
** there.
699699
*/
700
+#ifdef _WIN32
701
+# include <windows.h>
702
+#endif
700703
701704
/*
702
-** Translate MBCS to UTF8. Return a pointer. Call fossil_mbcs_free()
703
-** to deallocate any memory used to store the returned pointer when done.
705
+** Translate MBCS to UTF8. Return a pointer to the translated text.
706
+** Call fossil_mbcs_free() to deallocate any memory used to store the
707
+** returned pointer when done.
704708
*/
705709
char *fossil_mbcs_to_utf8(const char *zMbcs){
706710
#ifdef _WIN32
707711
extern char *sqlite3_win32_mbcs_to_utf8(const char*);
708712
return sqlite3_win32_mbcs_to_utf8(zMbcs);
@@ -710,17 +714,59 @@
710714
return (char*)zMbcs; /* No-op on unix */
711715
#endif
712716
}
713717
714718
/*
715
-** Translate UTF8 to MBCS. Return a pointer. Call fossil_mbcs_free()
716
-** to deallocate any memory used to store the returned pointer when done.
719
+** Translate UTF8 to MBCS for use in system calls. Return a pointer to the
720
+** translated text.. Call fossil_mbcs_free() to deallocate any memory
721
+** used to store the returned pointer when done.
717722
*/
718723
char *fossil_utf8_to_mbcs(const char *zUtf8){
719724
#ifdef _WIN32
720725
extern char *sqlite3_win32_utf8_to_mbcs(const char*);
721726
return sqlite3_win32_utf8_to_mbcs(zUtf8);
727
+#else
728
+ return (char*)zUtf8; /* No-op on unix */
729
+#endif
730
+}
731
+
732
+/*
733
+** Translate UTF8 to MBCS for display on the console. Return a pointer to the
734
+** translated text.. Call fossil_mbcs_free() to deallocate any memory
735
+** used to store the returned pointer when done.
736
+*/
737
+char *fossil_utf8_to_console(const char *zUtf8){
738
+#ifdef _WIN32
739
+ int nChar, nByte;
740
+ WCHAR *zUnicode; /* Unicode version of zUtf8 */
741
+ char *zConsole; /* Console version of zUtf8 */
742
+ int codepage; /* Console code page */
743
+
744
+ nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, NULL, 0);
745
+ zUnicode = malloc( nChar*sizeof(zUnicode[0]) );
746
+ if( zUnicode==0 ){
747
+ return 0;
748
+ }
749
+ nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, zUnicode, nChar);
750
+ if( nChar==0 ){
751
+ free(zUnicode);
752
+ return 0;
753
+ }
754
+ codepage = GetConsoleCP();
755
+ nByte = WideCharToMultiByte(codepage, 0, zUnicode, -1, 0, 0, 0, 0);
756
+ zConsole = malloc( nByte );
757
+ if( zConsole==0 ){
758
+ free(zUnicode);
759
+ return 0;
760
+ }
761
+ nByte = WideCharToMultiByte(codepage, 0, zUnicode, -1, zConsole, nByte, 0, 0);
762
+ free(zUnicode);
763
+ if( nByte == 0 ){
764
+ free(zConsole);
765
+ zConsole = 0;
766
+ }
767
+ return zConsole;
722768
#else
723769
return (char*)zUtf8; /* No-op on unix */
724770
#endif
725771
}
726772
727773
--- src/file.c
+++ src/file.c
@@ -695,14 +695,18 @@
695 /**************************************************************************
696 ** The following routines translate between MBCS and UTF8 on windows.
697 ** Since everything is always UTF8 on unix, these routines are no-ops
698 ** there.
699 */
 
 
 
700
701 /*
702 ** Translate MBCS to UTF8. Return a pointer. Call fossil_mbcs_free()
703 ** to deallocate any memory used to store the returned pointer when done.
 
704 */
705 char *fossil_mbcs_to_utf8(const char *zMbcs){
706 #ifdef _WIN32
707 extern char *sqlite3_win32_mbcs_to_utf8(const char*);
708 return sqlite3_win32_mbcs_to_utf8(zMbcs);
@@ -710,17 +714,59 @@
710 return (char*)zMbcs; /* No-op on unix */
711 #endif
712 }
713
714 /*
715 ** Translate UTF8 to MBCS. Return a pointer. Call fossil_mbcs_free()
716 ** to deallocate any memory used to store the returned pointer when done.
 
717 */
718 char *fossil_utf8_to_mbcs(const char *zUtf8){
719 #ifdef _WIN32
720 extern char *sqlite3_win32_utf8_to_mbcs(const char*);
721 return sqlite3_win32_utf8_to_mbcs(zUtf8);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
722 #else
723 return (char*)zUtf8; /* No-op on unix */
724 #endif
725 }
726
727
--- src/file.c
+++ src/file.c
@@ -695,14 +695,18 @@
695 /**************************************************************************
696 ** The following routines translate between MBCS and UTF8 on windows.
697 ** Since everything is always UTF8 on unix, these routines are no-ops
698 ** there.
699 */
700 #ifdef _WIN32
701 # include <windows.h>
702 #endif
703
704 /*
705 ** Translate MBCS to UTF8. Return a pointer to the translated text.
706 ** Call fossil_mbcs_free() to deallocate any memory used to store the
707 ** returned pointer when done.
708 */
709 char *fossil_mbcs_to_utf8(const char *zMbcs){
710 #ifdef _WIN32
711 extern char *sqlite3_win32_mbcs_to_utf8(const char*);
712 return sqlite3_win32_mbcs_to_utf8(zMbcs);
@@ -710,17 +714,59 @@
714 return (char*)zMbcs; /* No-op on unix */
715 #endif
716 }
717
718 /*
719 ** Translate UTF8 to MBCS for use in system calls. Return a pointer to the
720 ** translated text.. Call fossil_mbcs_free() to deallocate any memory
721 ** used to store the returned pointer when done.
722 */
723 char *fossil_utf8_to_mbcs(const char *zUtf8){
724 #ifdef _WIN32
725 extern char *sqlite3_win32_utf8_to_mbcs(const char*);
726 return sqlite3_win32_utf8_to_mbcs(zUtf8);
727 #else
728 return (char*)zUtf8; /* No-op on unix */
729 #endif
730 }
731
732 /*
733 ** Translate UTF8 to MBCS for display on the console. Return a pointer to the
734 ** translated text.. Call fossil_mbcs_free() to deallocate any memory
735 ** used to store the returned pointer when done.
736 */
737 char *fossil_utf8_to_console(const char *zUtf8){
738 #ifdef _WIN32
739 int nChar, nByte;
740 WCHAR *zUnicode; /* Unicode version of zUtf8 */
741 char *zConsole; /* Console version of zUtf8 */
742 int codepage; /* Console code page */
743
744 nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, NULL, 0);
745 zUnicode = malloc( nChar*sizeof(zUnicode[0]) );
746 if( zUnicode==0 ){
747 return 0;
748 }
749 nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, zUnicode, nChar);
750 if( nChar==0 ){
751 free(zUnicode);
752 return 0;
753 }
754 codepage = GetConsoleCP();
755 nByte = WideCharToMultiByte(codepage, 0, zUnicode, -1, 0, 0, 0, 0);
756 zConsole = malloc( nByte );
757 if( zConsole==0 ){
758 free(zUnicode);
759 return 0;
760 }
761 nByte = WideCharToMultiByte(codepage, 0, zUnicode, -1, zConsole, nByte, 0, 0);
762 free(zUnicode);
763 if( nByte == 0 ){
764 free(zConsole);
765 zConsole = 0;
766 }
767 return zConsole;
768 #else
769 return (char*)zUtf8; /* No-op on unix */
770 #endif
771 }
772
773
+1 -2
--- src/printf.c
+++ src/printf.c
@@ -807,21 +807,20 @@
807807
** if the output is going to the screen. If output is redirected into
808808
** a file, no translation occurs. No translation ever occurs on unix.
809809
*/
810810
void fossil_puts(const char *z, int toStdErr){
811811
#if defined(_WIN32)
812
- extern char *sqlite3_win32_utf8_to_mbcs(const char*);
813812
static int once = 1;
814813
static int istty[2];
815814
char *zToFree = 0;
816815
if( once ){
817816
istty[0] = _isatty(fileno(stdout));
818817
istty[1] = _isatty(fileno(stderr));
819818
once = 0;
820819
}
821820
assert( toStdErr==0 || toStdErr==1 );
822
- if( istty[toStdErr] ) z = zToFree = sqlite3_win32_utf8_to_mbcs(z);
821
+ if( istty[toStdErr] ) z = zToFree = fossil_utf8_to_console(z);
823822
fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout);
824823
free(zToFree);
825824
#else
826825
fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout);
827826
#endif
828827
--- src/printf.c
+++ src/printf.c
@@ -807,21 +807,20 @@
807 ** if the output is going to the screen. If output is redirected into
808 ** a file, no translation occurs. No translation ever occurs on unix.
809 */
810 void fossil_puts(const char *z, int toStdErr){
811 #if defined(_WIN32)
812 extern char *sqlite3_win32_utf8_to_mbcs(const char*);
813 static int once = 1;
814 static int istty[2];
815 char *zToFree = 0;
816 if( once ){
817 istty[0] = _isatty(fileno(stdout));
818 istty[1] = _isatty(fileno(stderr));
819 once = 0;
820 }
821 assert( toStdErr==0 || toStdErr==1 );
822 if( istty[toStdErr] ) z = zToFree = sqlite3_win32_utf8_to_mbcs(z);
823 fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout);
824 free(zToFree);
825 #else
826 fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout);
827 #endif
828
--- src/printf.c
+++ src/printf.c
@@ -807,21 +807,20 @@
807 ** if the output is going to the screen. If output is redirected into
808 ** a file, no translation occurs. No translation ever occurs on unix.
809 */
810 void fossil_puts(const char *z, int toStdErr){
811 #if defined(_WIN32)
 
812 static int once = 1;
813 static int istty[2];
814 char *zToFree = 0;
815 if( once ){
816 istty[0] = _isatty(fileno(stdout));
817 istty[1] = _isatty(fileno(stderr));
818 once = 0;
819 }
820 assert( toStdErr==0 || toStdErr==1 );
821 if( istty[toStdErr] ) z = zToFree = fossil_utf8_to_console(z);
822 fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout);
823 free(zToFree);
824 #else
825 fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout);
826 #endif
827

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button