GNU Octave  8.1.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-2023 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_u8_conv_to_encoding_strict (const char *tocode, const uint8_t *src,
61  size_t srclen, size_t *lengthp)
62 {
63  return u8_conv_to_encoding (tocode, iconveh_error,
64  src, srclen, NULL, NULL, lengthp);
65 }
66 
67 char *
68 octave_u32_conv_to_encoding_strict (const char *tocode, const uint32_t *src,
69  size_t srclen, size_t *lengthp)
70 {
71  return u32_conv_to_encoding (tocode, iconveh_error,
72  src, srclen, NULL, NULL, lengthp);
73 }
74 
75 uint8_t *
77  (const char *fromcode, const char *src, size_t srclen,
78  size_t *offsets, size_t *lengthp)
79 {
80  return u8_conv_from_encoding (fromcode, iconveh_question_mark,
81  src, srclen, offsets, NULL, lengthp);
82 }
83 
84 char *
86  (const char *tocode, const uint8_t *src, size_t srclen,
87  size_t *offsets, size_t *lengthp)
88 {
89  return u8_conv_to_encoding (tocode, iconveh_question_mark,
90  src, srclen, offsets, NULL, lengthp);
91 }
92 
93 char *
94 u8_from_wchar (const wchar_t *wc)
95 {
96  // Convert wide char array to multibyte UTF-8 char array
97  // The memory at the returned pointer must be freed after use.
98 
99  size_t srclen = wcslen (wc) * sizeof (wchar_t);
100  const char *src = (const char *) wc;
101 
102  size_t length = 0;
103  uint8_t *mbchar = u8_conv_from_encoding ("wchar_t", iconveh_question_mark,
104  src, srclen, NULL, NULL, &length);
105 
106  // result might not be 0 terminated
107  char *retval = malloc (length + 1);
108  if (retval)
109  {
110  memcpy (retval, mbchar, length);
111  free ((void *) mbchar);
112  retval[length] = 0; // 0 terminate string
113  }
114  else
115  free ((void *) mbchar);
116 
117  return retval;
118 }
119 
120 wchar_t *
121 u8_to_wchar (const char *u8)
122 {
123  // Convert multibyte UTF-8 char array to wide char array
124  // The memory at the returned pointer must be freed after use.
125 
126  size_t srclen = strlen (u8);
127  const uint8_t *src = (const uint8_t *) u8;
128 
129  size_t length = 0;
130 
131  char *wchar = u8_conv_to_encoding ("wchar_t", iconveh_question_mark,
132  src, srclen, NULL, NULL, &length);
133  // result might not be 0 terminated
134  wchar_t *retval = malloc (length + 1 * sizeof (wchar_t));
135  if (retval)
136  {
137  memcpy (retval, wchar, length);
138  free ((void *) wchar);
139  retval[length / sizeof (wchar_t)] = 0; // 0 terminate string
140  }
141 
142  else
143  free ((void *) wchar);
144 
145  return retval;
146 }
T::size_type strlen(const typename T::value_type *str)
Definition: oct-string.cc:85
void * malloc(unsigned)
void free(void *)
uint8_t * octave_u8_conv_from_encoding_offsets(const char *fromcode, const char *src, size_t srclen, size_t *offsets, size_t *lengthp)
char * octave_u8_conv_to_encoding_offsets(const char *tocode, const uint8_t *src, size_t srclen, size_t *offsets, size_t *lengthp)
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)
char * octave_u8_conv_to_encoding_strict(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)