diff libgui/src/m-editor/file-editor-tab.cc @ 26331:5f10217b562d stable

Use gnulib to check if encoding is possible (bug #55306). * file-editor-tab.cc (check_valid_codec): Additionally use gnulib to check whether encoding is possible. * uniconv-wrappers.[c/h] (octave_u32_conv_to_encoding_strict): Add new wrapper function.
author Markus Mützel <markus.muetzel@gmx.de>
date Mon, 31 Dec 2018 17:05:34 +0100
parents f73bcf1d0b5a
children fd890481c408
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Mon Dec 31 18:07:09 2018 +0100
+++ b/libgui/src/m-editor/file-editor-tab.cc	Mon Dec 31 17:05:34 2018 +0100
@@ -69,6 +69,7 @@
 #include "marker.h"
 
 #include "file-ops.h"
+#include "uniconv-wrappers.h"
 
 #include "bp-table.h"
 #include "call-stack.h"
@@ -2321,7 +2322,32 @@
         return nullptr;
       }
 
-    if (! codec->canEncode (_edit_area->text ()))
+    QString editor_text = _edit_area->text ();
+    bool can_encode = codec->canEncode (editor_text);
+
+    // We cannot rely on QTextCodec::canEncode because it uses the
+    // ConverterState of convertFromUnicode which isn't updated by some
+    // implementations.
+    if (can_encode)
+      {
+        std::u32string u32_str = editor_text.toStdU32String ();
+        const uint32_t *src = reinterpret_cast<const uint32_t *>
+                              (u32_str.c_str ());
+
+        size_t length;
+        char *res_str =
+          octave_u32_conv_to_encoding_strict (_encoding.toStdString ().c_str (),
+                                              src, u32_str.length (), &length);
+        if (! res_str)
+          {
+            if (errno == EILSEQ)
+              can_encode = false;
+          }
+        else
+          ::free (static_cast<void *> (res_str));
+      }
+
+    if (! can_encode)
       {
         QMessageBox::critical (nullptr,
                                tr ("Octave Editor"),