Simplify char16_t implementation
[glibc.git] / wcsmbs / wcrtomb.c
blob946fdaf47fcfdc9deb402714ead707479048ed2c
1 /* Copyright (C) 1996-1998,2000,2002,2005,2011,2012
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <assert.h>
22 #include <dlfcn.h>
23 #include <errno.h>
24 #include <gconv.h>
25 #include <stdlib.h>
26 #include <wchar.h>
27 #include <wcsmbsload.h>
29 #include <sysdep.h>
31 #ifndef EILSEQ
32 # define EILSEQ EINVAL
33 #endif
36 /* This is the private state used if PS is NULL. */
37 static mbstate_t state;
39 size_t
40 __wcrtomb (char *s, wchar_t wc, mbstate_t *ps)
42 char buf[MB_LEN_MAX];
43 struct __gconv_step_data data;
44 int status;
45 size_t result;
46 size_t dummy;
47 const struct gconv_fcts *fcts;
49 /* Set information for this step. */
50 data.__invocation_counter = 0;
51 data.__internal_use = 1;
52 data.__flags = __GCONV_IS_LAST;
53 data.__statep = ps ?: &state;
54 data.__trans = NULL;
56 /* A first special case is if S is NULL. This means put PS in the
57 initial state. */
58 if (s == NULL)
60 s = buf;
61 wc = L'\0';
64 /* Tell where we want to have the result. */
65 data.__outbuf = (unsigned char *) s;
66 data.__outbufend = (unsigned char *) s + MB_CUR_MAX;
68 /* Get the conversion functions. */
69 fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
70 __gconv_fct fct = fcts->tomb->__fct;
71 #ifdef PTR_DEMANGLE
72 if (fcts->tomb->__shlib_handle != NULL)
73 PTR_DEMANGLE (fct);
74 #endif
76 /* If WC is the NUL character we write into the output buffer the byte
77 sequence necessary for PS to get into the initial state, followed
78 by a NUL byte. */
79 if (wc == L'\0')
81 status = DL_CALL_FCT (fct, (fcts->tomb, &data, NULL, NULL,
82 NULL, &dummy, 1, 1));
84 if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
85 *data.__outbuf++ = '\0';
87 else
89 /* Do a normal conversion. */
90 const unsigned char *inbuf = (const unsigned char *) &wc;
92 status = DL_CALL_FCT (fct,
93 (fcts->tomb, &data, &inbuf,
94 inbuf + sizeof (wchar_t), NULL, &dummy, 0, 1));
97 /* There must not be any problems with the conversion but illegal input
98 characters. The output buffer must be large enough, otherwise the
99 definition of MB_CUR_MAX is not correct. All the other possible
100 errors also must not happen. */
101 assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
102 || status == __GCONV_ILLEGAL_INPUT
103 || status == __GCONV_INCOMPLETE_INPUT
104 || status == __GCONV_FULL_OUTPUT);
106 if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
107 || status == __GCONV_FULL_OUTPUT)
108 result = data.__outbuf - (unsigned char *) s;
109 else
111 result = (size_t) -1;
112 __set_errno (EILSEQ);
115 return result;
117 weak_alias (__wcrtomb, wcrtomb)
118 libc_hidden_weak (wcrtomb)
120 /* There should be no difference between the UTF-32 handling required
121 by c32rtomb and the wchar_t handling which has long since been
122 implemented in wcrtomb. */
123 weak_alias (__wcrtomb, c32rtomb)