changeset 25514:c63f67d87b4a

Add C++ functions to convert between UTF-8 string and wstring (bug #49118). * lo-sysdep.[cc,h]: Add functions "u8_to_wstring" and "u8_from_wstring" for conversion between utf-8 strings and wide character strings.
author Markus Mützel <markus.muetzel@gmx.de>
date Thu, 28 Jun 2018 14:39:46 +0200
parents 7fb40efda31f
children 8945a6a6c0eb
files liboctave/system/lo-sysdep.cc liboctave/system/lo-sysdep.h
diffstat 2 files changed, 39 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/system/lo-sysdep.cc	Wed Jun 27 21:01:58 2018 +0200
+++ b/liboctave/system/lo-sysdep.cc	Thu Jun 28 14:39:46 2018 +0200
@@ -29,6 +29,7 @@
 #include "file-ops.h"
 #include "lo-error.h"
 #include "lo-sysdep.h"
+#include "uniconv-wrappers.h"
 #include "unistd-wrappers.h"
 
 namespace octave
@@ -67,5 +68,39 @@
 
       return octave_chdir_wrapper (path.c_str ());
     }
+
+    std::wstring
+    u8_to_wstring (const std::string& utf8_string)
+    {
+      wchar_t *wchar = nullptr;
+
+      wchar = u8_to_wchar (utf8_string.c_str ());
+
+      std::wstring retval = L"";
+      if (wchar != nullptr)
+        {
+          retval = std::wstring (wchar);
+          free (static_cast<void *> (wchar));
+        }
+
+      return retval;
+    }
+
+    std::string
+    u8_from_wstring (const std::wstring& wchar_string)
+    {
+      char *mbchar = nullptr;
+
+      mbchar = u8_from_wchar (wchar_string.c_str ());
+
+      std::string retval = "";
+      if (mbchar != nullptr)
+        {
+          retval = std::string (mbchar);
+          free (static_cast<void *> (mbchar));
+        }
+
+      return retval;
+    }
   }
 }
--- a/liboctave/system/lo-sysdep.h	Wed Jun 27 21:01:58 2018 +0200
+++ b/liboctave/system/lo-sysdep.h	Thu Jun 28 14:39:46 2018 +0200
@@ -40,6 +40,10 @@
     extern std::string getcwd (void);
 
     extern int chdir (const std::string&);
+
+    extern std::wstring u8_to_wstring (const std::string&);
+
+    extern std::string u8_from_wstring (const std::wstring&);
   }
 }