# HG changeset patch
# User Markus Mützel <markus.muetzel@gmx.de>
# Date 1497384276 -7200
#      Tue Jun 13 22:04:36 2017 +0200
# Node ID 7349f86079be81120f096f0ad187a1d7ca2c4b58
# Parent  d90c36bbd09f81b355c3a975551263c0a0f10f27
Use native2unicode and unicode2native if available (bug #51203).

diff -r d90c36bbd09f -r 7349f86079be inst/oct2ods.m
--- a/inst/oct2ods.m	Tue May 16 23:15:14 2017 +0200
+++ b/inst/oct2ods.m	Tue Jun 13 22:04:36 2017 +0200
@@ -171,10 +171,13 @@
   endif
 
   ## Convert to UTF-8
-  ## FIXME: Replace with the following when unicode2native is available:
-  ##        obj = tidyxml (obj, @(str) unicode2native (str, "UTF-8"));
   if (spsh_opts.convert_utf)
-    c_arr = tidyxml (c_arr, @unicode2utf8);
+    if exist ("native2unicode", "file")
+      conv_fcn = @(str) unicode2native (native2unicode (uint8 (str)), "UTF-8");
+    else
+      conv_fcn = @unicode2utf8;
+    endif
+    c_arr = tidyxml (c_arr, conv_fcn);
   endif
 
   if (strcmp (ods.xtype, "OTK"))
diff -r d90c36bbd09f -r 7349f86079be inst/oct2xls.m
--- a/inst/oct2xls.m	Tue May 16 23:15:14 2017 +0200
+++ b/inst/oct2xls.m	Tue Jun 13 22:04:36 2017 +0200
@@ -174,10 +174,13 @@
   endif
 
   ## Convert to UTF-8
-  ## FIXME: Replace with the following when unicode2native is available:
-  ##        obj = tidyxml (obj, @(str) unicode2native (str, "UTF-8"));
   if (! strcmp (xls.xtype, "COM") && (spsh_opts.convert_utf))
-    obj = tidyxml (obj, @unicode2utf8);
+    if exist ("native2unicode", "file")
+      conv_fcn = @(str) unicode2native (native2unicode (uint8 (str)), "UTF-8");
+    else
+      conv_fcn = @unicode2utf8;
+    endif
+    obj = tidyxml (obj, conv_fcn);
   endif
   
   ## Select interface to be used
diff -r d90c36bbd09f -r 7349f86079be inst/ods2oct.m
--- a/inst/ods2oct.m	Tue May 16 23:15:14 2017 +0200
+++ b/inst/ods2oct.m	Tue Jun 13 22:04:36 2017 +0200
@@ -208,10 +208,13 @@
 
   ## Convert from UTF-8 and strip characters that are not supported by Octave
   ## (any chars < 32 or > 255).
-  ## FIXME: Replace with the following when native2unicode is available:
-  ##        rawarr = tidyxml (rawarr, @(str) native2unicode (str, "UTF-8"));
   if (spsh_opts.convert_utf)
-    rawarr = tidyxml (rawarr, @utf82unicode);
+    if exist ("native2unicode", "file")
+      conv_fcn = @(str) unicode2native (native2unicode (str, "UTF-8"));
+    else
+      conv_fcn = @utf82unicode;
+    endif
+    rawarr = tidyxml (rawarr, conv_fcn);
   endif
 
   rstatus = ! isempty (rawarr);
diff -r d90c36bbd09f -r 7349f86079be inst/tidyxml.m
--- a/inst/tidyxml.m	Tue May 16 23:15:14 2017 +0200
+++ b/inst/tidyxml.m	Tue Jun 13 22:04:36 2017 +0200
@@ -44,9 +44,18 @@
     if (isempty (conv_fcn))
       ustr = uint8 (istr);
     else
-      [ustr, error_flag] = conv_fcn (istr);
-      if (error_flag)
-        warning ("Encoding conversion failed; some characters might be lost");
+      if nargout (conv_fcn) > 1
+        [ustr, error_flag] = conv_fcn (istr);
+        if (error_flag)
+          warning ("Encoding conversion failed; some characters might be lost");
+        endif
+      else
+        try
+          ustr = conv_fcn (istr);
+        catch err
+          warning ("Encoding conversion failed; some characters might be lost");
+          ustr = istr;
+        end_try_catch
       endif
     endif
     ostr = char (ustr(ustr > 31 & ustr < 256));
diff -r d90c36bbd09f -r 7349f86079be inst/xls2oct.m
--- a/inst/xls2oct.m	Tue May 16 23:15:14 2017 +0200
+++ b/inst/xls2oct.m	Tue Jun 13 22:04:36 2017 +0200
@@ -221,10 +221,13 @@
 
   ## Convert from UTF-8 and strip characters that are not supported by Octave
   ## (any chars < 32 or > 255).
-  ## FIXME: Replace with the following when native2unicode is available:
-  ##        rawarr = tidyxml (rawarr, @(str) native2unicode (str, "UTF-8"));
   if (! strcmp (xls.xtype, "COM") && (spsh_opts.convert_utf))
-    rawarr = tidyxml (rawarr, @utf82unicode);
+    if exist ("native2unicode", "file")
+      conv_fcn = @(str) unicode2native (native2unicode (str, "UTF-8"));
+    else
+      conv_fcn = @utf82unicode;
+    endif
+    rawarr = tidyxml (rawarr, conv_fcn);
   endif
 
   ## Optionally strip empty outer rows and columns & keep track of original data location
