changeset 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 59422a6fbd91
children ae0aa570c907 ec1f34091635
files src/main-cli.cc src/main-gui.cc
diffstat 2 files changed, 62 insertions(+), 3 deletions(-) [+]
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;
 }
--- 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 <vector>
 #  include <locale>
 #  include <codecvt>
+#  include <windows.h>
+#  include <versionhelpers.h>
 #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;
 }