# HG changeset patch # User Markus Mützel # Date 1670071302 -3600 # Node ID ea0b06534a370ad65bd1b8ec0140289eef153585 # Parent 59422a6fbd915d2115b35f8a7e762008083f401d Windows: Set console input and output codepages to UTF-8 if supported. * src/main-cli.cc (main), src/main-gui.cc (main): Temporary set the input and output codepage for the console to UTF-8. That is needed so that non-ASCII characters entered at the CLI are correctly stored as UTF-8 in Octave character arrays, and so that UTF-8 encoded character arrays are correctly displayed in the CLI. diff -r 59422a6fbd91 -r ea0b06534a37 src/main-cli.cc --- a/src/main-cli.cc Fri Dec 02 21:34:15 2022 -0800 +++ b/src/main-cli.cc Sat Dec 03 13:41:42 2022 +0100 @@ -36,6 +36,8 @@ # include # include # include +# include +# include #endif #include "liboctave-build-info.h" @@ -105,6 +107,20 @@ argv[i_arg] = &argv_str[i_arg][0]; argv[argc] = nullptr; + unsigned int old_console_codepage = 0; + unsigned int old_console_output_codepage = 0; + + if (IsWindows7OrGreater ()) + { + // save old console input and output codepages + old_console_codepage = GetConsoleCP (); + old_console_output_codepage = GetConsoleOutputCP (); + + // set console input and output codepages to UTF-8 + SetConsoleCP (65001); + SetConsoleOutputCP (65001); + } + #else int main (int argc, char **argv) @@ -118,5 +134,18 @@ octave::cli_application app (argc, argv); - return app.execute (); + int ret = app.execute (); + +#if defined (OCTAVE_USE_WINDOWS_API) && defined (_UNICODE) + if (IsWindows7OrGreater ()) + { + // restore previous console input and output codepages + if (old_console_codepage) + SetConsoleCP (old_console_codepage); + if (old_console_output_codepage) + SetConsoleOutputCP (old_console_output_codepage); + } +#endif + + return ret; } diff -r 59422a6fbd91 -r ea0b06534a37 src/main-gui.cc --- a/src/main-gui.cc Fri Dec 02 21:34:15 2022 -0800 +++ b/src/main-gui.cc Sat Dec 03 13:41:42 2022 +0100 @@ -36,6 +36,8 @@ # include # include # include +# include +# include #endif #include "liboctave-build-info.h" @@ -118,6 +120,20 @@ argv[i_arg] = &argv_str[i_arg][0]; argv[argc] = nullptr; + unsigned int old_console_codepage = 0; + unsigned int old_console_output_codepage = 0; + + if (IsWindows7OrGreater ()) + { + // save old console input and output codepages + old_console_codepage = GetConsoleCP (); + old_console_output_codepage = GetConsoleOutputCP (); + + // set console input and output codepages to UTF-8 + SetConsoleCP (65001); + SetConsoleOutputCP (65001); + } + #else int main (int argc, char **argv) @@ -127,7 +143,21 @@ octave::sys::env::set_program_name (argv[0]); - octave::qt_application app (argc, argv); + octave::qt_application app ("octave", "octave-gui", OCTAVE_VERSION, + argc, argv); + + int ret = app.execute (); - return app.execute (); +#if defined (OCTAVE_USE_WINDOWS_API) && defined (_UNICODE) + if (IsWindows7OrGreater ()) + { + // restore previous console input and output codepages + if (old_console_codepage) + SetConsoleCP (old_console_codepage); + if (old_console_output_codepage) + SetConsoleOutputCP (old_console_output_codepage); + } +#endif + + return ret; }