Mercurial > octave
diff src/main-cli.cc @ 31629:ea0b06534a37 stable
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.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Sat, 03 Dec 2022 13:41:42 +0100 |
parents | 796f54d4ddbf |
children | 597f3ee61a48 |
line wrap: on
line diff
--- 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 <vector> # include <locale> # include <codecvt> +# include <windows.h> +# include <versionhelpers.h> #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; }