GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
uniconv-wrappers.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2017-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 // The conversion functions are provided by gnulib. We don't include
27 // gnulib headers directly in Octave's C++ source files to avoid
28 // problems that may be caused by the way that gnulib overrides standard
29 // library functions.
30 
31 #if defined (HAVE_CONFIG_H)
32 # include "config.h"
33 #endif
34 
35 #include <stdlib.h>
36 #include <string.h>
37 #include <wchar.h>
38 
39 #include "uniconv.h"
40 
41 #include "uniconv-wrappers.h"
42 
43 uint8_t *
44 octave_u8_conv_from_encoding (const char *fromcode, const char *src,
45  size_t srclen, size_t *lengthp)
46 {
47  return u8_conv_from_encoding (fromcode, iconveh_question_mark,
48  src, srclen, NULL, NULL, lengthp);
49 }
50 
51 char *
52 octave_u8_conv_to_encoding (const char *tocode, const uint8_t *src,
53  size_t srclen, size_t *lengthp)
54 {
55  return u8_conv_to_encoding (tocode, iconveh_question_mark,
56  src, srclen, NULL, NULL, lengthp);
57 }
58 
59 char *
60 octave_u32_conv_to_encoding_strict (const char *tocode, const uint32_t *src,
61  size_t srclen, size_t *lengthp)
62 {
63  return u32_conv_to_encoding (tocode, iconveh_error,
64  src, srclen, NULL, NULL, lengthp);
65 }
66 
67 char *
68 u8_from_wchar (const wchar_t *wc)
69 {
70  // Convert wide char array to multibyte UTF-8 char array
71  // The memory at the returned pointer must be freed after use.
72 
73  size_t srclen = wcslen (wc) * sizeof (wchar_t);
74  const char *src = (const char *) wc;
75 
76  size_t length = 0;
77  uint8_t *mbchar = u8_conv_from_encoding ("wchar_t", iconveh_question_mark,
78  src, srclen, NULL, NULL, &length);
79 
80  // result might not be 0 terminated
81  char *retval = malloc (length + 1);
82  if (retval)
83  {
84  memcpy (retval, mbchar, length);
85  free ((void *) mbchar);
86  retval[length] = 0; // 0 terminate string
87  }
88  else
89  free ((void *) mbchar);
90 
91  return retval;
92 }
93 
94 wchar_t *
95 u8_to_wchar (const char *u8)
96 {
97  // Convert multibyte UTF-8 char array to wide char array
98  // The memory at the returned pointer must be freed after use.
99 
100  size_t srclen = strlen (u8);
101  const uint8_t *src = (const uint8_t *) u8;
102 
103  size_t length = 0;
104 
105  char *wchar = u8_conv_to_encoding ("wchar_t", iconveh_question_mark,
106  src, srclen, NULL, NULL, &length);
107  // result might not be 0 terminated
108  wchar_t *retval = malloc (length + 1 * sizeof (wchar_t));
109  if (retval)
110  {
111  memcpy (retval, wchar, length);
112  free ((void *) wchar);
113  retval[length / sizeof (wchar_t)] = 0; // 0 terminate string
114  }
115 
116  else
117  free ((void *) wchar);
118 
119  return retval;
120 }
T::size_type strlen(const typename T::value_type *str)
Definition: oct-string.cc:85
void * malloc(unsigned)
void free(void *)
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
uint8_t * octave_u8_conv_from_encoding(const char *fromcode, const char *src, size_t srclen, size_t *lengthp)
char * octave_u32_conv_to_encoding_strict(const char *tocode, const uint32_t *src, size_t srclen, size_t *lengthp)
char * octave_u8_conv_to_encoding(const char *tocode, const uint8_t *src, size_t srclen, size_t *lengthp)
wchar_t * u8_to_wchar(const char *u8)
char * u8_from_wchar(const wchar_t *wc)