changeset 25512:7335d44f34b4

Add conversion functions between UTF-8 and wchar_t (bug #49118). * uniconv-wrappers.[c,h]: Add functions "u8_to_wchar" and "u8_from_wchar" to convert character arrays between UTF-8 and wchar_t.
author Markus Mützel <markus.muetzel@gmx.de>
date Wed, 27 Jun 2018 20:56:37 +0200
parents 49d3c6344afe
children 7fb40efda31f
files liboctave/wrappers/uniconv-wrappers.c liboctave/wrappers/uniconv-wrappers.h
diffstat 2 files changed, 55 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/wrappers/uniconv-wrappers.c	Wed Jun 27 14:00:34 2018 -0400
+++ b/liboctave/wrappers/uniconv-wrappers.c	Wed Jun 27 20:56:37 2018 +0200
@@ -29,6 +29,10 @@
 #  include "config.h"
 #endif
 
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+
 #include "uniconv.h"
 
 #include "uniconv-wrappers.h"
@@ -41,10 +45,54 @@
                                 src, srclen, NULL, NULL, lengthp);
 }
 
-extern char *
+char *
 octave_u8_conv_to_encoding (const char *tocode, const uint8_t *src,
                             size_t srclen, size_t *lengthp)
 {
   return u8_conv_to_encoding (tocode, iconveh_question_mark,
                               src, srclen, NULL, NULL, lengthp);
 }
+
+char *
+u8_from_wchar (const wchar_t *wc)
+{
+  // Convert wide char array to multibyte UTF-8 char array
+  // The memory at the returned pointer must be freed after use.
+
+  size_t srclen = wcslen (wc) * sizeof (wchar_t);
+  const char *src = (const char *) wc;
+
+  size_t length = 0;
+  uint8_t *mbchar = u8_conv_from_encoding ("wchar_t", iconveh_question_mark,
+                                           src, srclen, NULL, NULL, &length);
+
+  // result might not be 0 terminated
+  char *retval = malloc (length + 1);
+  memcpy (retval, mbchar, length);
+  free ((void *) mbchar);
+  retval[length] = 0; // 0 terminate string
+
+  return retval;
+}
+
+wchar_t *
+u8_to_wchar (const char *u8)
+{
+  // Convert multibyte UTF-8 char array to wide char array
+  // The memory at the returned pointer must be freed after use.
+
+  size_t srclen = strlen (u8);
+  const uint8_t *src = (const uint8_t *) u8;
+
+  size_t length = 0;
+
+  char *wchar = u8_conv_to_encoding ("wchar_t", iconveh_question_mark,
+                                     src, srclen, NULL, NULL, &length);
+  // result might not be 0 terminated
+  wchar_t *retval = malloc (length + 1 * sizeof (wchar_t));
+  memcpy (retval, wchar, length);
+  free ((void *) wchar);
+  retval[length / sizeof (wchar_t)] = 0; // 0 terminate string
+
+  return retval;
+}
--- a/liboctave/wrappers/uniconv-wrappers.h	Wed Jun 27 14:00:34 2018 -0400
+++ b/liboctave/wrappers/uniconv-wrappers.h	Wed Jun 27 20:56:37 2018 +0200
@@ -46,6 +46,12 @@
 octave_u8_conv_to_encoding (const char *tocode, const uint8_t *src,
                             size_t srclen, size_t *lengthp);
 
+extern char *
+u8_from_wchar (const wchar_t *wc);
+
+extern wchar_t *
+u8_to_wchar (const char *u8_char);
+
 #if defined __cplusplus
 }
 #endif