GNU Octave 7.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-2022 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
43uint8_t *
44octave_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
51char *
52octave_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
59char *
60octave_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
67char *
68octave_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
75char *
76u8_from_wchar (const wchar_t *wc)
77{
78 // Convert wide char array to multibyte UTF-8 char array
79 // The memory at the returned pointer must be freed after use.
80
81 size_t srclen = wcslen (wc) * sizeof (wchar_t);
82 const char *src = (const char *) wc;
83
84 size_t length = 0;
85 uint8_t *mbchar = u8_conv_from_encoding ("wchar_t", iconveh_question_mark,
86 src, srclen, NULL, NULL, &length);
87
88 // result might not be 0 terminated
89 char *retval = malloc (length + 1);
90 if (retval)
91 {
92 memcpy (retval, mbchar, length);
93 free ((void *) mbchar);
94 retval[length] = 0; // 0 terminate string
95 }
96 else
97 free ((void *) mbchar);
98
99 return retval;
100}
101
102wchar_t *
103u8_to_wchar (const char *u8)
104{
105 // Convert multibyte UTF-8 char array to wide char array
106 // The memory at the returned pointer must be freed after use.
107
108 size_t srclen = strlen (u8);
109 const uint8_t *src = (const uint8_t *) u8;
110
111 size_t length = 0;
112
113 char *wchar = u8_conv_to_encoding ("wchar_t", iconveh_question_mark,
114 src, srclen, NULL, NULL, &length);
115 // result might not be 0 terminated
116 wchar_t *retval = malloc (length + 1 * sizeof (wchar_t));
117 if (retval)
118 {
119 memcpy (retval, wchar, length);
120 free ((void *) wchar);
121 retval[length / sizeof (wchar_t)] = 0; // 0 terminate string
122 }
123
124 else
125 free ((void *) wchar);
126
127 return retval;
128}
T::size_type strlen(const typename T::value_type *str)
Definition: oct-string.cc:85
void * malloc(unsigned)
void free(void *)
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 * octave_u8_conv_to_encoding(const char *tocode, const uint8_t *src, size_t srclen, size_t *lengthp)
uint8_t * octave_u8_conv_from_encoding(const char *fromcode, const char *src, size_t srclen, size_t *lengthp)
char * u8_from_wchar(const wchar_t *wc)
char * octave_u32_conv_to_encoding_strict(const char *tocode, const uint32_t *src, size_t srclen, size_t *lengthp)