# HG changeset patch
# User Markus Mützel <markus.muetzel@gmx.de>
# Date 1687543568 -7200
#      Fri Jun 23 20:06:08 2023 +0200
# Node ID 4b6a1315abe8967e2b08370cc4b63862789e809a
# Parent  bd568e6ac986711316ec119975414b2e13fb5027
file editor: Skip encoding conversion if file is empty (bug #64330).

* libgui/src/m-editor/file-editor-tab.cc (file_editor_tab::load_file): If the
file to be opened is empty, skip the encoding conversion attempt and just set
the editor tab content to an empty string.

diff -r bd568e6ac986 -r 4b6a1315abe8 libgui/src/m-editor/file-editor-tab.cc
--- a/libgui/src/m-editor/file-editor-tab.cc	Fri Jun 23 19:44:00 2023 +0200
+++ b/libgui/src/m-editor/file-editor-tab.cc	Fri Jun 23 20:06:08 2023 +0200
@@ -1893,69 +1893,76 @@
   const char *src = text_data.constData ();
   std::size_t srclen = text_data.length ();
 
-  std::size_t length;
-  uint16_t *u16_str;
-
-  // try to convert encoding in strict mode
-  u16_str = octave_u16_conv_from_encoding_strict (encoding.c_str (),
-                                                  src, srclen, &length);
-
-  // check for invalid characters in input file
-  if (! u16_str)
+  if (srclen == 0)
+    m_edit_area->setText (QString ());
+  else
     {
-      // Set read only
-      m_edit_area->setReadOnly (true);
-
-      // convert encoding allowing replacements
-      u16_str = octave_u16_conv_from_encoding (encoding.c_str (),
-                                               src, srclen, &length);
-
+      std::size_t length;
+      uint16_t *u16_str;
+
+      // try to convert encoding in strict mode
+      u16_str = octave_u16_conv_from_encoding_strict (encoding.c_str (),
+                                                      src, srclen, &length);
+
+      // check for invalid characters in input file
       if (! u16_str)
         {
-          // FIXME: Can this ever happen?
-
-          // non-modal error message box
-          QMessageBox *msgBox
-            = new QMessageBox (QMessageBox::Critical,
-                               tr ("Octave Editor"),
-                               tr ("Unable to read file '%1'\n"
-                                   "with selected encoding '%2': %3")
-                                  .arg (file_to_load).arg (m_encoding)
-                                  .arg (std::strerror (errno)),
-                               QMessageBox::Ok, nullptr);
-          show_dialog (msgBox, false);
-          return QString ();
+          // Set read only
+          m_edit_area->setReadOnly (true);
+
+          // convert encoding allowing replacements
+          u16_str = octave_u16_conv_from_encoding (encoding.c_str (),
+                                                   src, srclen, &length);
+
+          if (! u16_str)
+            {
+              // FIXME: Can this ever happen?
+
+              // non-modal error message box
+              QMessageBox *msgBox
+                = new QMessageBox (QMessageBox::Critical,
+                                   tr ("Octave Editor"),
+                                   tr ("Unable to read file '%1'\n"
+                                       "with selected encoding '%2': %3")
+                                      .arg (file_to_load).arg (m_encoding)
+                                      .arg (std::strerror (errno)),
+                                   QMessageBox::Ok, nullptr);
+              show_dialog (msgBox, false);
+              return QString ();
+            }
+
+          // Message box for user decision
+          QString msg = tr ("There were problems reading the file\n"
+                            "%1\n"
+                            "with the selected encoding %2.\n\n"
+                            "Modifying and saving the file might "
+                            "cause data loss!")
+                            .arg (file_to_load).arg (m_encoding);
+          QMessageBox *msg_box = new QMessageBox ();
+          msg_box->setIcon (QMessageBox::Warning);
+          msg_box->setText (msg);
+          msg_box->setWindowTitle (tr ("Octave Editor"));
+          msg_box->addButton (tr ("&Edit anyway"), QMessageBox::YesRole);
+          msg_box->addButton (tr ("Chan&ge encoding"),
+                              QMessageBox::AcceptRole);
+          msg_box->addButton (tr ("&Close"), QMessageBox::RejectRole);
+
+          connect (msg_box, &QMessageBox::buttonClicked,
+                   this, &file_editor_tab::handle_decode_warning_answer);
+
+          msg_box->setWindowModality (Qt::WindowModal);
+          msg_box->setAttribute (Qt::WA_DeleteOnClose);
+          msg_box->show ();
         }
 
-      // Message box for user decision
-      QString msg = tr ("There were problems reading the file\n"
-                        "%1\n"
-                        "with the selected encoding %2.\n\n"
-                        "Modifying and saving the file might "
-                        "cause data loss!")
-                        .arg (file_to_load).arg (m_encoding);
-      QMessageBox *msg_box = new QMessageBox ();
-      msg_box->setIcon (QMessageBox::Warning);
-      msg_box->setText (msg);
-      msg_box->setWindowTitle (tr ("Octave Editor"));
-      msg_box->addButton (tr ("&Edit anyway"), QMessageBox::YesRole);
-      msg_box->addButton (tr ("Chan&ge encoding"), QMessageBox::AcceptRole);
-      msg_box->addButton (tr ("&Close"), QMessageBox::RejectRole);
-
-      connect (msg_box, &QMessageBox::buttonClicked,
-               this, &file_editor_tab::handle_decode_warning_answer);
-
-      msg_box->setWindowModality (Qt::WindowModal);
-      msg_box->setAttribute (Qt::WA_DeleteOnClose);
-      msg_box->show ();
+      unwind_action free_u16_str ([=] () { ::free (u16_str); });
+
+      QString text
+        = QString::fromUtf16 (reinterpret_cast<char16_t *> (u16_str), length);
+
+      m_edit_area->setText (text);
     }
 
-  unwind_action free_u16_str ([=] () { ::free (u16_str); });
-
-  QString text
-    = QString::fromUtf16 (reinterpret_cast<char16_t *> (u16_str), length);
-
-  m_edit_area->setText (text);
   m_edit_area->setEolMode (detect_eol_mode ());
 
   m_copy_available = false;  // no selection yet available
