Back to index

glibc  2.9
wctob.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996-2000, 2002, 2003, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1996.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <dlfcn.h>
00021 #include <gconv.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <wchar.h>
00025 #include <wcsmbsload.h>
00026 
00027 #include <sysdep.h>
00028 
00029 
00030 int
00031 wctob (c)
00032      wint_t c;
00033 {
00034   unsigned char buf[MB_LEN_MAX];
00035   struct __gconv_step_data data;
00036   wchar_t inbuf[1];
00037   wchar_t *inptr = inbuf;
00038   size_t dummy;
00039   int status;
00040   const struct gconv_fcts *fcts;
00041 
00042   if (c == WEOF)
00043     return EOF;
00044 
00045   /* We know that only ASCII compatible encodings are used for the
00046      locale and that the wide character encoding is ISO 10646.  */
00047   if (c >= L'\0' && c <= L'\x7f')
00048     return (int) c;
00049 
00050   /* Tell where we want the result.  */
00051   data.__outbuf = buf;
00052   data.__outbufend = buf + MB_LEN_MAX;
00053   data.__invocation_counter = 0;
00054   data.__internal_use = 1;
00055   data.__flags = __GCONV_IS_LAST;
00056   data.__statep = &data.__state;
00057   data.__trans = NULL;
00058 
00059   /* Make sure we start in the initial state.  */
00060   memset (&data.__state, '\0', sizeof (mbstate_t));
00061 
00062   /* Get the conversion functions.  */
00063   fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
00064 
00065   /* Create the input string.  */
00066   inbuf[0] = c;
00067 
00068   const unsigned char *argptr = (const unsigned char *) inptr;
00069   __gconv_fct fct = fcts->tomb->__fct;
00070 #ifdef PTR_DEMANGLE
00071   if (fcts->tomb->__shlib_handle != NULL)
00072     PTR_DEMANGLE (fct);
00073 #endif
00074   status = DL_CALL_FCT (fct,
00075                      (fcts->tomb, &data, &argptr,
00076                       argptr + sizeof (inbuf[0]), NULL, &dummy, 0, 1));
00077 
00078   /* The conversion failed or the output is too long.  */
00079   if ((status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
00080        && status != __GCONV_EMPTY_INPUT)
00081       || data.__outbuf != (unsigned char *) (buf + 1))
00082     return EOF;
00083 
00084   return buf[0];
00085 }