changeset 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 45cf2ada3e39
children 13c62f51e830
files libgui/src/m-editor/file-editor-tab.cc liboctave/wrappers/uniconv-wrappers.c liboctave/wrappers/uniconv-wrappers.h
diffstat 3 files changed, 39 insertions(+), 1 deletions(-) [+]
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"),
--- a/liboctave/wrappers/uniconv-wrappers.c	Mon Dec 31 18:07:09 2018 +0100
+++ b/liboctave/wrappers/uniconv-wrappers.c	Mon Dec 31 17:05:34 2018 +0100
@@ -54,6 +54,14 @@
 }
 
 char *
+octave_u32_conv_to_encoding_strict (const char *tocode, const uint32_t *src,
+                            size_t srclen, size_t *lengthp)
+{
+  return u32_conv_to_encoding (tocode, iconveh_error,
+                              src, srclen, NULL, NULL, lengthp);
+}
+
+char *
 u8_from_wchar (const wchar_t *wc)
 {
   // Convert wide char array to multibyte UTF-8 char array
--- a/liboctave/wrappers/uniconv-wrappers.h	Mon Dec 31 18:07:09 2018 +0100
+++ b/liboctave/wrappers/uniconv-wrappers.h	Mon Dec 31 17:05:34 2018 +0100
@@ -47,6 +47,10 @@
                             size_t srclen, size_t *lengthp);
 
 extern char *
+octave_u32_conv_to_encoding_strict (const char *tocode, const uint32_t *src,
+                                    size_t srclen, size_t *lengthp);
+
+extern char *
 u8_from_wchar (const wchar_t *wc);
 
 extern wchar_t *